-
Notifications
You must be signed in to change notification settings - Fork 21
/
extension.js
53 lines (43 loc) · 1.92 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
var vscode = require('vscode');
var beautify = require('./src/index');
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function activate(context) {
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
/**
* format all code
*/
var format = vscode.commands.registerTextEditorCommand('vueBeautify.format', function (textEditor) {
// only for language of vue
if (textEditor.document.languageId !== 'vue' || !/\.vue$/i.test(textEditor.document.fileName)) {
return;
}
// textEditor option
var editorInsertSpace = textEditor.options.insertSpaces;
var editorTabSize = textEditor.options.tabSize;
var isRootIndent = vscode.workspace.getConfiguration('vueBeautify').isRootIndent;
// editor text
var text = textEditor.document.getText();
// beautify code
var code = beautify(text, !editorInsertSpace, editorTabSize, isRootIndent);
// edit
textEditor.edit(function (editBuilder) {
var document = textEditor.document;
var lastLine = document.lineAt(document.lineCount - 1);
var start = new vscode.Position(0, 0);
var end = new vscode.Position(document.lineCount - 1, lastLine.text.length);
var range = new vscode.Range(start, end);
editBuilder.replace(range, code);
});
});
context.subscriptions.push(format);
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {
}
exports.deactivate = deactivate;