maybe working

This commit is contained in:
koniifer 2024-11-30 23:22:30 +00:00
parent fa7a4bd245
commit bb58baa09c
2 changed files with 52 additions and 14 deletions

View file

@ -13,9 +13,20 @@ function getExecutablePath(): string {
: config.get<string>("compilerPath")!; : config.get<string>("compilerPath")!;
} }
async function runCommand(filePath: string, args: string[]): Promise<string> { 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<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
execFile(getExecutablePath(), args, (error, stdout, stderr) => { execFile(getExecutablePath(), args, { cwd }, (error, stdout, stderr) => {
if (error && error.code === "ENOENT") { if (error && error.code === "ENOENT") {
vscode.window.showErrorMessage( vscode.window.showErrorMessage(
"hblang compiler not found. Ensure 'hbc' is installed and available in PATH." "hblang compiler not found. Ensure 'hbc' is installed and available in PATH."
@ -36,31 +47,53 @@ async function provideDocumentFormattingEdits(
path.dirname(document.uri.fsPath), path.dirname(document.uri.fsPath),
`temp_${path.basename(document.uri.fsPath)}` `temp_${path.basename(document.uri.fsPath)}`
); );
await fs.promises.writeFile(tempFilePath, document.getText());
try { try {
const stdout = await runCommand(tempFilePath, [ await document.save();
"--fmt-stdout",
tempFilePath, 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( const fullRange = new vscode.Range(
document.positionAt(0), document.positionAt(0),
document.positionAt(document.getText().length) document.positionAt(document.getText().length)
); );
diagnosticCollection.delete(document.uri);
return [vscode.TextEdit.replace(fullRange, stdout)]; return [vscode.TextEdit.replace(fullRange, stdout)];
} catch (error) { } catch (error) {
if (error instanceof Error) {
vscode.window.showErrorMessage(`Formatting failed: ${error.message}`);
} else {
vscode.window.showErrorMessage(`Formatting failed: Unknown error`);
}
return []; return [];
} finally { } finally {
fs.promises.unlink(tempFilePath).catch(console.error); fs.promises.unlink(tempFilePath).catch(console.error);
} }
} }
async function lintDocument(document: vscode.TextDocument) { async function lintDocument(document: vscode.TextDocument) {
const args = [document.uri.fsPath];
if (isAbleosRepo(document)) {
args.push("--path-resolver", "ableos");
}
try { 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)); diagnosticCollection.set(document.uri, parseLintingErrors(stderr));
} catch (error) { } catch (error) {
if (error instanceof Error) { if (error instanceof Error) {
@ -79,10 +112,14 @@ function parseLintingErrors(stderr: string): vscode.Diagnostic[] {
const match = lineText.match(/^([^:]+):(\d+):(\d+):\s*(.+)$/); const match = lineText.match(/^([^:]+):(\d+):(\d+):\s*(.+)$/);
if (!match) return null; if (!match) return null;
const [, , lineStr, columnStr, message] = match; const [abc, , lineStr, columnStr, message] = match;
const lineNum = parseInt(lineStr, 10) - 1; const lineNum = parseInt(lineStr, 10) - 1;
const columnNum = parseInt(columnStr, 10) - 1; const columnNum = parseInt(columnStr, 10) - 1;
const severity = abc.startsWith("(W) ")
? vscode.DiagnosticSeverity.Warning
: vscode.DiagnosticSeverity.Error;
return new vscode.Diagnostic( return new vscode.Diagnostic(
new vscode.Range( new vscode.Range(
lineNum, lineNum,
@ -91,12 +128,13 @@ function parseLintingErrors(stderr: string): vscode.Diagnostic[] {
columnNum + message.length columnNum + message.length
), ),
message, message,
vscode.DiagnosticSeverity.Error severity
); );
}) })
.filter(Boolean) as vscode.Diagnostic[]; .filter(Boolean) as vscode.Diagnostic[];
} }
export function activate(context: vscode.ExtensionContext) { export function activate(context: vscode.ExtensionContext) {
diagnosticCollection = vscode.languages.createDiagnosticCollection("hblang"); diagnosticCollection = vscode.languages.createDiagnosticCollection("hblang");
context.subscriptions.push(diagnosticCollection); context.subscriptions.push(diagnosticCollection);

View file

@ -116,7 +116,7 @@
"patterns": [ "patterns": [
{ {
"name": "keyword.control.hblang", "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"
} }
] ]
}, },