1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-07-30 12:07:46 +00:00
alejandra/integrations/vscode/extension.js
2022-02-12 19:18:21 -05:00

72 lines
2 KiB
JavaScript

const { execFile } = require("child_process");
const vscode = require("vscode");
const activate = (_) => {
const outputChannel = vscode.window.createOutputChannel("Alejandra");
vscode.languages.registerDocumentFormattingEditProvider("nix", {
provideDocumentFormattingEdits(document, _) {
const config = {
alejandra: vscode.workspace.getConfiguration("alejandra"),
};
return new Promise((resolve, reject) => {
try {
outputChannel.appendLine(
`Running Alejandra with settings: ${JSON.stringify(config)}`
);
const process = execFile(
config.alejandra.program,
[],
{},
(error, stdout, stderr) => {
if (error) {
outputChannel.appendLine(`error: ${error}`);
outputChannel.appendLine(`stderr: ${stderr}`);
vscode.window.showErrorMessage(
`While executing Alejandra with settings: ` +
`${JSON.stringify(config)}, ` +
`${error}`
);
reject(error);
}
const documentRange = new vscode.Range(
document.lineAt(0).range.start,
document.lineAt(
document.lineCount - 1
).rangeIncludingLineBreak.end
);
resolve([new vscode.TextEdit(documentRange, stdout)]);
}
);
const documentText = document.getText();
outputChannel.appendLine(
`Feeding ${documentText.length} of input to stdin`
);
process.stdin.write(documentText);
process.stdin.end();
} catch (error) {
vscode.window.showErrorMessage(
`While executing Alejandra with settings: ` +
`${JSON.stringify(config)} ` +
`${error}`
);
reject(error);
}
});
},
});
};
const deactivate = () => {};
module.exports = {
activate,
deactivate,
};