good enough formatter
This commit is contained in:
parent
9098604bf0
commit
4b5bb991f7
102
src/main.ts
102
src/main.ts
|
@ -1,66 +1,62 @@
|
||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import { spawn } from 'child_process';
|
import { spawn } from 'child_process';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as os from 'os';
|
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
|
|
||||||
export function activate(context: vscode.ExtensionContext) {
|
vscode.languages.registerDocumentFormattingEditProvider('hblang', {
|
||||||
// Register a document formatting edit provider for the language
|
async provideDocumentFormattingEdits(document: vscode.TextDocument): Promise<vscode.TextEdit[]> {
|
||||||
context.subscriptions.push(
|
try {
|
||||||
vscode.languages.registerDocumentFormattingEditProvider('hblang', {
|
const filePath = document.uri.fsPath;
|
||||||
async provideDocumentFormattingEdits(document: vscode.TextDocument): Promise<vscode.TextEdit[]> {
|
const tempFilePath = path.join(path.dirname(filePath), `temp_${path.basename(filePath)}`);
|
||||||
|
|
||||||
try {
|
await fs.promises.writeFile(tempFilePath, document.getText());
|
||||||
const formattedText = await formatDocument(document.fileName);
|
|
||||||
|
|
||||||
const edit = new vscode.TextEdit(
|
return new Promise((resolve, reject) => {
|
||||||
new vscode.Range(0, 0, document.lineCount, 0),
|
const hbcProcess = spawn('hbc', ['--fmt-stdout', tempFilePath]);
|
||||||
formattedText
|
|
||||||
);
|
|
||||||
|
|
||||||
return [edit];
|
let stdout = '';
|
||||||
} catch (error: unknown) {
|
let stderr = '';
|
||||||
if (error instanceof Error) {
|
|
||||||
vscode.window.showErrorMessage(`Formatting failed: ${error.message}`);
|
hbcProcess.stdout.on('data', (data) => {
|
||||||
|
stdout += data.toString();
|
||||||
|
});
|
||||||
|
|
||||||
|
hbcProcess.stderr.on('data', (data) => {
|
||||||
|
stderr += data.toString();
|
||||||
|
});
|
||||||
|
|
||||||
|
hbcProcess.on('close', (code) => {
|
||||||
|
fs.unlink(tempFilePath, (err) => {
|
||||||
|
if (err) {
|
||||||
|
console.error('Error removing temp file:', err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (code === 0) {
|
||||||
|
const fullRange = new vscode.Range(
|
||||||
|
document.positionAt(0),
|
||||||
|
document.positionAt(document.getText().length)
|
||||||
|
);
|
||||||
|
const edit = vscode.TextEdit.replace(fullRange, stdout);
|
||||||
|
resolve([edit]);
|
||||||
} else {
|
} else {
|
||||||
vscode.window.showErrorMessage(`Formatting failed: ${String(error)}`);
|
vscode.window.showErrorMessage(`hbc formatting failed: ${stderr}`);
|
||||||
|
reject(new Error(stderr));
|
||||||
}
|
}
|
||||||
return []; // Return an empty array if formatting fails
|
});
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function deactivate() { }
|
hbcProcess.on('error', (err) => {
|
||||||
|
vscode.window.showErrorMessage(`Failed to start hbc: ${err.message}`);
|
||||||
async function formatDocument(path: string): Promise<string> {
|
reject(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
return new Promise((resolve, reject) => {
|
if (error instanceof Error) {
|
||||||
const child = spawn('hbc', ['--fmt-stdout', path], { shell: true });
|
vscode.window.showErrorMessage(`Error formatting document: ${error.message}`);
|
||||||
|
|
||||||
let formattedText = '';
|
|
||||||
|
|
||||||
// Capture the output
|
|
||||||
child.stdout.on('data', (data) => {
|
|
||||||
formattedText += data.toString();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Handle errors
|
|
||||||
child.stderr.on('data', (data) => {
|
|
||||||
reject(new Error(data.toString()));
|
|
||||||
});
|
|
||||||
|
|
||||||
// Handle process exit
|
|
||||||
child.on('exit', (code) => {
|
|
||||||
|
|
||||||
if (code === 0) {
|
|
||||||
resolve(formattedText);
|
|
||||||
} else {
|
} else {
|
||||||
reject(new Error(`Formatter exited with code ${code}`));
|
vscode.window.showErrorMessage('An unknown error occurred while formatting the document.');
|
||||||
}
|
}
|
||||||
});
|
return [];
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in a new issue