import { EventEmitter } from 'events'; import * as path from 'path'; import { CompletionItem, CompletionItemKind, Hover, Position, Range } from 'vscode-languageserver'; import { diagnosticMessages } from '../DiagnosticMessages'; import { FunctionScope } from '../FunctionScope'; import { Callable, Diagnostic, File, FileReference, FunctionCall } from '../interfaces'; import { Program } from '../Program'; import util from '../util'; export class XmlFile { constructor( public pathAbsolute: string, /** * The absolute path to the file, relative to the pkg */ public pkgPath: string, public program: Program ) { this.extension = path.extname(pathAbsolute).toLowerCase(); //allow unlimited listeners this.emitter.setMaxListeners(0); } public parentNameRange: Range; /** * The extension for this file */ public extension: string; public ownScriptImports = [] as FileReference[]; public getDiagnostics() { return [...this.parseDiagnistics]; } /** * The range of the entire file */ public fileRange: Range; public parseDiagnistics = [] as Diagnostic[]; //TODO implement the xml CDATA parsing, which would populate this list public callables = [] as Callable[]; //TODO implement the xml CDATA parsing, which would populate this list public functionCalls = [] as FunctionCall[]; public functionScopes = [] as FunctionScope[]; /** * The name of the component that this component extends */ public parentName: string; /** * The name of the component declared in this xml file */ public componentName: string; /** * Indicates if this file was processed by the program yet. */ public wasProcessed = false; public async parse(fileContents: string) { if (this.wasProcessed) { throw new Error(`File was already processed. Create a new file instead. ${this.pathAbsolute}`); } //split the text into lines let lines = util.getLines(fileContents); this.parentNameRange = this.findExtendsPosition(fileContents); //create a range of the entire file this.fileRange = Range.create(0, 0, lines.length, lines[lines.length - 1].length - 1); let parsedXml; try { parsedXml = await util.parseXml(fileContents); if (parsedXml && parsedXml.component) { if (parsedXml.component.$) { this.componentName = parsedXml.component.$.name; this.parentName = parsedXml.component.$.extends; } let componentRange: Range; //find the range for the component element's opening tag for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) { let match = /(.*)( { return x.lineIndex === position.line && //column between start and end position.character >= x.columnIndexBegin && position.character <= x.columnIndexEnd; }); return scriptImport; } private getScriptImportCompletions(scriptImport: FileReference) { let result = [] as CompletionItem[]; //get a list of all scripts currently being imported let currentImports = this.ownScriptImports.map((x) => x.pkgPath); //restrict to only .brs files for (let key in this.program.files) { let file = this.program.files[key]; if ( //is a brightscript file (file.extension === '.bs' || file.extension === '.brs') && //not already referenced in this file currentImports.indexOf(file.pkgPath) === -1 ) { //the text range to replace if the user selects this result let range = { start: { character: scriptImport.columnIndexBegin, line: scriptImport.lineIndex }, end: { character: scriptImport.columnIndexEnd, line: scriptImport.lineIndex } } as Range; //add the relative path let relativePath = util.getRelativePath(this.pkgPath, file.pkgPath).replace(/\\/g, '/'); let pkgPath = 'pkg:/' + file.pkgPath.replace(/\\/g, '/'); result.push({ label: relativePath, detail: file.pathAbsolute, kind: CompletionItemKind.File, textEdit: { newText: relativePath, range: range } }); //add the absolute path result.push({ label: pkgPath, detail: file.pathAbsolute, kind: CompletionItemKind.File, textEdit: { newText: pkgPath, range: range } }); } } return result; } /** * Scan the xml and find the range of the parent component's name in the `extends="ParentComponentName"` attribute of the component */ public findExtendsPosition(fullText: string) { let regexp = /.*]*((extends\s*=\s*")(\w*)")/gms; let match = regexp.exec(fullText); if (match) { let extendsText = match[1]; // `extends="something"` let extendsToFirstQuote = match[2]; // `extends="` let componentName = match[3]; // `something` let lines = util.getLines(match[0]); for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) { let line = lines[lineIndex]; let extendsIdx = line.indexOf(extendsText); //we found the line index if (extendsIdx > -1) { let colStartIndex = extendsIdx + extendsToFirstQuote.length; let colEndIndex = colStartIndex + componentName.length; return Range.create(lineIndex, colStartIndex, lineIndex, colEndIndex); } } } } public emitter = new EventEmitter(); public on(name: 'attach-parent', callback: (data: XmlFile) => void); public on(name: 'detach-parent', callback: () => void); public on(name: string, callback: (data: any) => void) { this.emitter.on(name, callback); return () => { this.emitter.removeListener(name, callback); }; } protected emit(name: 'attach-parent', data: XmlFile); protected emit(name: 'detach-parent'); protected emit(name: string, data?: any) { this.emitter.emit(name, data); } public parent: XmlFile; /** * Components can extend another component. * This method attaches the parent component to this component, where * this component can listen for script import changes on the parent. * @param parent */ public attachParent(parent: XmlFile) { //detach any existing parent this.detachParent(); this.parent = parent; this.emit('attach-parent', parent); } public detachParent() { if (this.parent) { this.parent = undefined; this.emit('detach-parent'); } } public getHover(position: Position): Hover { //TODO implement // let result = {} as Hover; return null; } public getFunctionScopeAtPosition(position: Position, functionScopes?: FunctionScope[]): FunctionScope { //TODO implement return null; } public dispose() { this.emitter.removeAllListeners(); } }