Skip to content

Commit

Permalink
linting and cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
cademack committed Jul 16, 2024
1 parent 211c63f commit 1f1dbf2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/language/activate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ export function activateLanguage(context: vscode.ExtensionContext) {
{ scheme: "file", language: "amalgam" },
new AmalgamDefinitionProvider()
)
)
);
}
87 changes: 45 additions & 42 deletions src/language/definitions.ts
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");
}
}
}

0 comments on commit 1f1dbf2

Please sign in to comment.