UNPKG

973 BJavaScriptView Raw
1const path = require('path');
2const execFileSync = require('child_process').execFileSync;
3const vscode = require('vscode');
4
5function execCLI(input) {
6 const cli = path.resolve(__dirname, 'node_modules/es-beautifier/lib/cli.js');
7 return execFileSync('node', [cli], { input, encoding: 'utf8' });
8}
9
10function beautify(document) {
11 try {
12 const text = document.getText();
13 const beautified = execCLI(text);
14 const start = document.positionAt(0);
15 const end = document.positionAt(text.length);
16 const range = new vscode.Range(start, end);
17 return [vscode.TextEdit.replace(range, beautified)];
18 } catch (e) {
19 console.error('es-beautifier failed:', e);
20 vscode.window.showInformationMessage('es-beautifier failed. See the console log in the DevTools.');
21 return [];
22 }
23}
24
25exports.activate = function activate() {
26 vscode.languages.registerDocumentFormattingEditProvider('es-beautifier', {
27 provideDocumentFormattingEdits: beautify,
28 });
29};