-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
127 lines (123 loc) · 2.76 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const vscode = require('vscode');
const beautify = require('js-beautify');
const prettier = require('prettier');
const jsBeautifyCfg = {
brace_style: "expand",
break_chained_methods: true,
comma_first: false,
e4x: false,
end_with_newline: false,
indent_char: "\t",
indent_empty_lines: false,
indent_inner_html: false,
indent_scripts: "normal",
indent_size: "1",
jslint_happy: false,
keep_array_indentation: false,
max_preserve_newlines: "-1",
preserve_newlines: false,
space_before_conditional: true,
unescape_strings: false,
wrap_line_length: "0"
};
const prettierCfg = {
php:
{
parser: 'php',
tabWidth: 4,
useTabs: true,
}
};
function applyFormat(doc, formatted)
{
const allTextRange = new vscode.Range(doc.positionAt(0), doc.positionAt(doc.getText()
.length));
return [vscode.TextEdit.replace(allTextRange, formatted)];
}
function formatJSBeautify(doc, lang)
{
const text = doc.getText();
let formatted = '';
switch (lang)
{
case 'html':
formatted = beautify.html(text, jsBeautifyCfg);
break;
case 'css':
formatted = beautify.css(text, jsBeautifyCfg);
break;
case 'js':
case 'json':
formatted = beautify.js(text, jsBeautifyCfg);
break;
default:
return [];
}
return applyFormat(doc, formatted);
}
async function formatPrettier(doc, cfg)
{
const text = doc.getText();
const formatted = await prettier.format(text, cfg);
return applyFormat(doc, formatted);
}
async function activate(ctx)
{
const formatters = {
'css': (doc) => formatJSBeautify(doc, 'css'),
'html': (doc) => formatJSBeautify(doc, 'html'),
'javascript': (doc) => formatJSBeautify(doc, 'js'),
'json': (doc) => formatJSBeautify(doc, 'json'),
'php': (doc) => formatPrettier(doc, prettierCfg.php),
};
function registerFormatter(lang, formatter)
{
const provider = vscode.languages.registerDocumentFormattingEditProvider(lang,
{
async provideDocumentFormattingEdits(doc)
{
if (formatter)
{
return await formatter(doc);
}
return [];
}
});
ctx.subscriptions.push(provider);
}
for (const lang in formatters)
{
registerFormatter(lang, formatters[lang]);
}
const cmd = vscode.commands.registerCommand('fortifyFormatter.formatDocument', async () =>
{
const editor = vscode.window.activeTextEditor;
if (!editor)
{
return;
}
const doc = editor.document;
const lang = doc.languageId;
if (formatters[lang])
{
const edits = await formatters[lang](doc);
const edit = new vscode.WorkspaceEdit();
edits.forEach(e =>
{
edit.replace(doc.uri, e.range, e.newText);
});
await vscode.workspace.applyEdit(edit);
}
else
{
await vscode.commands.executeCommand('editor.action.formatDocument');
}
});
ctx.subscriptions.push(cmd);
}
function deactivate()
{}
module.exports = {
activate,
deactivate
};