-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
46 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,74 @@ | ||
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( | ||
document: vscode.TextDocument, | ||
position: vscode.Position, | ||
token: vscode.CancellationToken | ||
): Promise<vscode.Definition | vscode.LocationLink[] | undefined> { | ||
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<string[]> { | ||
const files: string[] = []; | ||
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<string> { | ||
return fs.promises.readFile(filePath, 'utf-8'); | ||
return fs.promises.readFile(filePath, "utf-8"); | ||
} | ||
} | ||
} |