diff --git a/src/language/activate.ts b/src/language/activate.ts index fa18203..4492a81 100644 --- a/src/language/activate.ts +++ b/src/language/activate.ts @@ -20,5 +20,5 @@ export function activateLanguage(context: vscode.ExtensionContext) { { scheme: "file", language: "amalgam" }, new AmalgamDefinitionProvider() ) - ) + ); } diff --git a/src/language/definitions.ts b/src/language/definitions.ts index 8fc10d3..93bd8b0 100644 --- a/src/language/definitions.ts +++ b/src/language/definitions.ts @@ -1,6 +1,6 @@ import * as vscode from "vscode"; -import * as fs from 'fs'; -import * as path from 'path'; +import * as fs from "fs"; +import * as path from "path"; export class AmalgamDefinitionProvider implements vscode.DefinitionProvider { async provideDefinition( @@ -8,45 +8,48 @@ export class AmalgamDefinitionProvider implements vscode.DefinitionProvider { position: vscode.Position, token: vscode.CancellationToken ): Promise { - const wordRange = document.getWordRangeAtPosition(position); - if (!wordRange) { - return undefined; - } - let word = document.getText(wordRange); + const wordRange = document.getWordRangeAtPosition(position); + if (!wordRange) { + return undefined; + } + let word = document.getText(wordRange); - // Check for exclamation point before the word - if (wordRange.start.character > 0) { - const charBeforeWord = document.getText(new vscode.Range( - new vscode.Position(wordRange.start.line, wordRange.start.character - 1), - wordRange.start - )); - if (charBeforeWord === '!') { - word = '!' + word; - } + // Check for exclamation point before the word + if (wordRange.start.character > 0) { + const charBeforeWord = document.getText( + new vscode.Range(new vscode.Position(wordRange.start.line, wordRange.start.character - 1), wordRange.start) + ); + if (charBeforeWord === "!") { + word = "!" + word; } - const definitionPattern = new RegExp(`#${word}`); + } + const definitionPattern = new RegExp(`#${word}`); - const workspaceFolders = vscode.workspace.workspaceFolders; - if (!workspaceFolders) { - return undefined; - } + const workspaceFolders = vscode.workspace.workspaceFolders; + if (!workspaceFolders) { + return undefined; + } - const rootPath = workspaceFolders[0].uri.fsPath; - const files = await this.getAllAmlgFiles(rootPath); + const rootPath = workspaceFolders[0].uri.fsPath; + const files = await this.getAllAmlgFiles(rootPath); - for (const file of files) { - const content = await this.readFile(file); - const lines = content.split('\n'); - for (let i = 0; i < lines.length; i++) { - if (definitionPattern.test(lines[i])) { - const definitionUri = vscode.Uri.file(file); - const definitionPosition = new vscode.Position(i, lines[i].indexOf(`#${word}`)); - return new vscode.Location(definitionUri, definitionPosition); - } - } + for (const file of files) { + if (token.isCancellationRequested) { + return undefined; } - return undefined; + const content = await this.readFile(file); + const lines = content.split("\n"); + for (let i = 0; i < lines.length; i++) { + if (definitionPattern.test(lines[i])) { + const definitionUri = vscode.Uri.file(file); + const definitionPosition = new vscode.Position(i, lines[i].indexOf(`#${word}`)); + return new vscode.Location(definitionUri, definitionPosition); + } + } + } + + return undefined; } private async getAllAmlgFiles(dir: string): Promise { @@ -54,18 +57,18 @@ export class AmalgamDefinitionProvider implements vscode.DefinitionProvider { const entries = await fs.promises.readdir(dir, { withFileTypes: true }); for (const entry of entries) { - const fullPath = path.join(dir, entry.name); - if (entry.isDirectory()) { - files.push(...await this.getAllAmlgFiles(fullPath)); - } else if (entry.isFile() && path.extname(entry.name) === '.amlg') { - files.push(fullPath); - } + const fullPath = path.join(dir, entry.name); + if (entry.isDirectory()) { + files.push(...(await this.getAllAmlgFiles(fullPath))); + } else if (entry.isFile() && path.extname(entry.name) === ".amlg") { + files.push(fullPath); + } } return files; } private async readFile(filePath: string): Promise { - return fs.promises.readFile(filePath, 'utf-8'); + return fs.promises.readFile(filePath, "utf-8"); } -} \ No newline at end of file +}