-
Notifications
You must be signed in to change notification settings - Fork 180
/
extension.js
executable file
·243 lines (223 loc) · 7.87 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
'use strict';
const vscode = require('vscode'),
beautify = require('js-beautify'),
options = require('./options'),
minimatch = require('minimatch'),
path = require('path');
const dumpError = e => {
if (0 && e) {
// console.log('beautify err:', e);
}
};
const getBeautifyType = () => {
return vscode.window.showQuickPick(
[
{ label: 'JS', description: 'Does JavaScript and JSON' },
{ label: 'CSS', description: 'Does CSS and SCSS' },
{ label: 'HTML' }
], {
matchOnDescription: true,
placeHolder: 'Couldn\'t determine type to beautify, please choose.'
})
.then(choice => {
if (!choice || !choice.label) throw 'no beautify type selected';
return choice.label.toLowerCase();
});
};
const removeNewLineEndForPartial = (config, isPartial) => {
if (isPartial) { config.end_with_newline = false; }
return Promise.resolve(config);
};
const beautifyDocRanges = (doc, ranges, type, formattingOptions, isPartial) => {
if (!doc) {
vscode.window.showInformationMessage(
'Beautify can\'t get the file information because the editor won\'t supply it. (File probably too large)');
throw '';
}
return Promise.resolve(type ? type : getBeautifyType())
.then(type => options(doc, type, formattingOptions)
.then(config => removeNewLineEndForPartial(config, isPartial))
.then(config => Promise.all(ranges.map(range =>
beautify[type](doc.getText(range), config)))));
};
const documentEdit = (range, newText) => [vscode.TextEdit.replace(range, newText)];
const extendRange = (doc, rng) => {
let end = rng.end;
if (end.character === 0) end = end.translate(-1, Number.MAX_VALUE);
else end = end.translate(0, Number.MAX_VALUE);
const r = new vscode.Range(new vscode.Position(rng.start.line, 0), end);
return doc.validateRange(r);
};
const fullRange = doc => doc.validateRange(new vscode.Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE));
const getWorkspaceRoot = doc => {
if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0) return;
if (!doc || doc.isUntitled) return vscode.workspace.workspaceFolders[0].uri.fsPath;
const folder = vscode.workspace.getWorkspaceFolder(doc.uri);
if (!folder) return;
return folder.uri.fsPath;
};
// gets bound
function fullEdit(type, doc, formattingOptions) {
let name = doc.fileName;
let base = getWorkspaceRoot(doc) || vscode.workspace.rootPath || '';
let ignore = vscode.workspace.getConfiguration('beautify', doc.uri)
.ignore;
if (!Array.isArray(ignore)) ignore = [ignore];
if (base && name.startsWith(base)) name = path.relative(base, name);
if (ignore.some(glob => minimatch(name, glob))) return [];
const rng = fullRange(doc);
return beautifyDocRanges(doc, [rng], type, formattingOptions)
.then(newText => documentEdit(rng, newText[0]), dumpError);
}
function rangeEdit(type, doc, rng, formattingOptions) {
// Fixes bug #106
rng = extendRange(doc, rng);
return beautifyDocRanges(doc, [rng], type, formattingOptions, true)
.then(newText => documentEdit(rng, newText[0]), dumpError);
}
const register = (type, selector, partial) => {
if (partial) return vscode.languages.registerDocumentRangeFormattingEditProvider(selector, {
provideDocumentRangeFormattingEdits: rangeEdit.bind(0, type)
});
else return vscode.languages.registerDocumentFormattingEditProvider(selector, {
provideDocumentFormattingEdits: fullEdit.bind(0, type)
});
};
class Formatters {
constructor() {
this.available = {
js: beautify.js,
css: beautify.css,
html: beautify.html
};
this.configTypes = {
type: 1,
ext: 1,
filename: 1
};
this.handlers = {};
}
onFileOpen(doc) {
for (let a in this.handlers) {
if (vscode.languages.match(this.handlers[a].selector, doc)) {
// drop and re-register this one
this.handlers[a].full.dispose();
this.handlers[a].partial.dispose();
this.handlers[a].full = register(a, this.handlers[a].selector);
this.handlers[a].partial = register(a, this.handlers[a].selector, true);
return a;
}
}
}
configure() {
let beautifyCfg = vscode.workspace.getConfiguration('beautify');
let cfg = beautifyCfg.language;
let js = beautifyCfg.JSFiles;
let css = beautifyCfg.CSSFiles;
let html = beautifyCfg.HTMLFiles;
if (js || css || html) {
cfg = {};
if (js) cfg.js = { ext: js };
if (css) cfg.css = { ext: css };
if (html) cfg.html = { ext: html };
vscode.window.showInformationMessage(
'`beautify.*Files` setting is deprecated. please use `beautify.language` instead. Open settings ->',
'Global', 'Workspace')
.then(open => {
if (open) vscode.commands.executeCommand(`workbench.action.open${open}Settings`);
}, dumpError);
}
cfg = cfg || {};
this.dispose();
for (let a in cfg) {
if (!(a in this.available)) continue;
// dispose of the current
let selector = [];
if (Array.isArray(cfg[a])) {
selector = cfg[a].map(language => ({ language, scheme: 'file' }));
} else {
for (let b in cfg[a]) {
let adder;
switch (b) {
case 'type':
adder = cfg[a][b].map(language => ({ language, scheme: 'file' }));
break;
case 'ext':
adder = [{ pattern: `**/*.{${cfg[a][b].join(',')}}`, scheme: 'file' }];
break;
case 'filename':
adder = [{ pattern: `**/{${cfg[a][b].join(',')}}`, scheme: 'file' }];
break;
default:
continue;
}
selector = selector.concat(adder);
}
}
selector = selector.concat(selector.map(s => {
const ss = Object.create(s);
ss.scheme = 'untitled';
return ss;
}));
if (selector.length) {
this.handlers[a] = {
selector,
full: register(a, selector),
partial: register(a, selector, true)
};
} else {
delete this.handlers[a];
}
}
}
getFormat(doc) {
for (let a in this.handlers) {
if (vscode.languages.match(this.handlers[a].selector, doc)) return a;
}
}
dispose() {
for (let a in this.handlers) {
this.handlers[a].full.dispose();
this.handlers[a].partial.dispose();
}
this.handlers = {};
}
}
const formatters = new Formatters();
formatters.configure();
const applyEdits = (editor, ranges, edits) => {
if (ranges.length !== edits.length) {
vscode.window.showInformationMessage(
'Beautify ranges didn\'t get back the right number of edits');
throw '';
}
return editor.edit(editorEdit => {
for (let i = 0; i < ranges.length; i++) {
editorEdit.replace(ranges[i], edits[i]);
}
});
};
const formatActiveDocument = ranged => {
const active = vscode.window.activeTextEditor;
if (!active || !active.document) return;
const type = formatters.getFormat(active.document);
let ranges = [];
if (ranged && active.selection) {
ranges = active.selections.filter(selection => !selection.isEmpty)
.map(range => extendRange(active.document, range));
}
if (ranges.length === 0)
ranges = [fullRange(active.document)];
if (ranges.length) {
return beautifyDocRanges(active.document, ranges, type, null, ranged)
.then(edits => applyEdits(active, ranges, edits), dumpError);
} else return Promise.resolve();
};
//register on activation
exports.activate = (context) => {
let sub = context.subscriptions;
sub.push(vscode.commands.registerCommand('HookyQR.beautify', formatActiveDocument.bind(0, true)));
sub.push(vscode.commands.registerCommand('HookyQR.beautifyFile', formatActiveDocument.bind(0, false)));
sub.push(vscode.workspace.onDidChangeConfiguration(formatters.configure.bind(formatters)));
sub.push(vscode.workspace.onDidOpenTextDocument(formatters.onFileOpen.bind(formatters)));
};