From bb58baa09c206553b719dba2bd02b7d5172ac8dd Mon Sep 17 00:00:00 2001 From: koniifer Date: Sat, 30 Nov 2024 23:22:30 +0000 Subject: [PATCH] maybe working --- src/main.ts | 64 ++++++++++++++++++++++++++------- syntaxes/hblang.tmLanguage.json | 2 +- 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/src/main.ts b/src/main.ts index 46af2d6..d2d069e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -13,9 +13,20 @@ function getExecutablePath(): string { : config.get("compilerPath")!; } -async function runCommand(filePath: string, args: string[]): Promise { +function isAbleosRepo(document: vscode.TextDocument): boolean { + const workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri); + if (!workspaceFolder) return false; + return workspaceFolder.name.toLowerCase().includes("ableos"); +} + +async function runCommand( + filePath: string, + args: string[], + cwd: string, +): Promise { + return new Promise((resolve, reject) => { - execFile(getExecutablePath(), args, (error, stdout, stderr) => { + execFile(getExecutablePath(), args, { cwd }, (error, stdout, stderr) => { if (error && error.code === "ENOENT") { vscode.window.showErrorMessage( "hblang compiler not found. Ensure 'hbc' is installed and available in PATH." @@ -36,31 +47,53 @@ async function provideDocumentFormattingEdits( path.dirname(document.uri.fsPath), `temp_${path.basename(document.uri.fsPath)}` ); - await fs.promises.writeFile(tempFilePath, document.getText()); try { - const stdout = await runCommand(tempFilePath, [ - "--fmt-stdout", - tempFilePath, - ]); + await document.save(); + + await fs.promises.writeFile(tempFilePath, document.getText()); + + const args = ["--fmt-stdout", tempFilePath]; + if (isAbleosRepo(document)) { + args.push("--path-resolver", "ableos"); + } + + const workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri); + if (!workspaceFolder) { + throw new Error("Workspace folder not found."); + } + + const stdout = await runCommand(tempFilePath, args, workspaceFolder.uri.fsPath); + const fullRange = new vscode.Range( document.positionAt(0), document.positionAt(document.getText().length) ); - - diagnosticCollection.delete(document.uri); - return [vscode.TextEdit.replace(fullRange, stdout)]; } catch (error) { + if (error instanceof Error) { + vscode.window.showErrorMessage(`Formatting failed: ${error.message}`); + } else { + vscode.window.showErrorMessage(`Formatting failed: Unknown error`); + } return []; } finally { fs.promises.unlink(tempFilePath).catch(console.error); } } + + async function lintDocument(document: vscode.TextDocument) { + const args = [document.uri.fsPath]; + if (isAbleosRepo(document)) { + args.push("--path-resolver", "ableos"); + } + try { - const stderr = await runCommand(document.uri.fsPath, [document.uri.fsPath]); + const workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri); + if (!workspaceFolder) { }; + const stderr = await runCommand(document.uri.fsPath, args, workspaceFolder?.uri.fsPath || document.uri.fsPath); diagnosticCollection.set(document.uri, parseLintingErrors(stderr)); } catch (error) { if (error instanceof Error) { @@ -79,10 +112,14 @@ function parseLintingErrors(stderr: string): vscode.Diagnostic[] { const match = lineText.match(/^([^:]+):(\d+):(\d+):\s*(.+)$/); if (!match) return null; - const [, , lineStr, columnStr, message] = match; + const [abc, , lineStr, columnStr, message] = match; const lineNum = parseInt(lineStr, 10) - 1; const columnNum = parseInt(columnStr, 10) - 1; + const severity = abc.startsWith("(W) ") + ? vscode.DiagnosticSeverity.Warning + : vscode.DiagnosticSeverity.Error; + return new vscode.Diagnostic( new vscode.Range( lineNum, @@ -91,12 +128,13 @@ function parseLintingErrors(stderr: string): vscode.Diagnostic[] { columnNum + message.length ), message, - vscode.DiagnosticSeverity.Error + severity ); }) .filter(Boolean) as vscode.Diagnostic[]; } + export function activate(context: vscode.ExtensionContext) { diagnosticCollection = vscode.languages.createDiagnosticCollection("hblang"); context.subscriptions.push(diagnosticCollection); diff --git a/syntaxes/hblang.tmLanguage.json b/syntaxes/hblang.tmLanguage.json index 8b65d8b..b4d7924 100644 --- a/syntaxes/hblang.tmLanguage.json +++ b/syntaxes/hblang.tmLanguage.json @@ -116,7 +116,7 @@ "patterns": [ { "name": "keyword.control.hblang", - "match": "\\b(fn|loop|break|if|else|return|packed|continue|true|false|struct|idk|die|null)\\b" + "match": "\\b(fn|loop|break|if|else|return|packed|continue|true|false|struct|idk|die|null|defer)\\b" } ] },