forked from junstyle/vscode-php-cs-fixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
402 lines (370 loc) · 15.8 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
'use strict';
const {
html_beautify
} = require('./js-beautify/beautify-html');
const vscode = require('vscode');
const {
commands,
workspace,
window,
languages,
Range,
Position
} = vscode;
const fs = require('fs');
const os = require('os');
const cp = require('child_process');
const TmpDir = os.tmpdir();
let autoFixing = false;
class PHPCSFixer {
constructor() {
this.loadSettings();
}
loadSettings() {
let config = workspace.getConfiguration('php-cs-fixer');
this.onsave = config.get('onsave', false);
this.autoFixByBracket = config.get('autoFixByBracket', true);
this.autoFixBySemicolon = config.get('autoFixBySemicolon', false);
this.executablePath = config.get('executablePath', process.platform === "win32" ? 'php-cs-fixer.bat' : 'php-cs-fixer');
if (process.platform == "win32" && config.has('executablePathWindows') && config.get('executablePathWindows').length > 0) {
this.executablePath = config.get('executablePathWindows');
}
if (workspace.rootPath != undefined) {
this.executablePath = this.executablePath.replace('${workspaceRoot}', workspace.rootPath);
}
this.executablePath = this.executablePath.replace(/^~\//, os.homedir() + '/');
this.rules = config.get('rules', '@PSR2');
if (typeof (this.rules) == 'object') {
this.rules = JSON.stringify(this.rules);
}
this.config = config.get('config', '.php_cs').replace(/^~\//, os.homedir() + '/');
this.formatHtml = config.get('formatHtml', false);
this.documentFormattingProvider = config.get('documentFormattingProvider', true);
this.allowRisky = config.get('allowRisky', false);
if (this.executablePath.endsWith(".phar")) {
this.pharPath = this.executablePath.replace(/^php[^ ]* /i, '');
this.executablePath = workspace.getConfiguration('php').get('validate.executablePath', 'php');
if (this.executablePath == null || this.executablePath.length == 0) {
this.executablePath = 'php';
}
} else {
this.pharPath = null;
}
}
getArgs(fileName) {
let args = ['fix', '--using-cache=no', fileName];
if (this.pharPath != null) {
args.unshift(this.pharPath);
}
let useConfig = false;
if (this.config.length > 0) {
let files = [];
let r = workspace.rootPath;
if (r == undefined) {
files = [this.config];
} else {
files = [this.config, r + '/.vscode/' + this.config, r + '/' + this.config];
}
for (let i = 0, len = files.length; i < len; i++) {
let c = files[i];
if (fs.existsSync(c)) {
args.push('--config=' + c);
useConfig = true;
break;
}
}
}
if (!useConfig && this.rules) {
args.push('--rules=' + this.rules);
}
if (this.allowRisky) {
args.push('--allow-risky=yes');
}
console.log(args);
return args;
}
format(text) {
autoFixing = true;
let fileName = TmpDir + '/temp-' + Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10) + '.php';
fs.writeFileSync(fileName, text);
let exec = cp.spawn(this.executablePath, this.getArgs(fileName));
let promise = new Promise((resolve, reject) => {
exec.on("error", (err) => {
console.log(err);
if (err.code == 'ENOENT') {
reject();
autoFixing = false;
window.showErrorMessage('PHP CS Fixer: ' + err.message + ". executablePath not found.");
}
});
exec.on("exit", (code) => {
if (code == 0) {
let fixed = fs.readFileSync(fileName, 'utf-8');
if (fixed.length > 0) {
resolve(fixed);
} else {
reject();
}
} else {
let msgs = {
1: 'PHP CS Fixer: php general error.',
16: 'PHP CS Fixer: Configuration error of the application.',
32: 'PHP CS Fixer: Configuration error of a Fixer.',
64: 'PHP CS Fixer: Exception raised within the application.'
};
window.showErrorMessage(msgs[code]);
reject();
}
fs.unlink(fileName, function (err) { });
autoFixing = false;
});
});
exec.stdout.on('data', (buffer) => {
console.log(buffer.toString());
});
exec.stderr.on('data', (buffer) => {
console.log(buffer.toString());
});
exec.on('close', (code) => {
// console.log(code);
});
return promise;
}
doAutoFixByBracket(event) {
if (event.contentChanges.length == 0) return;
let pressedKey = event.contentChanges[0].text;
// console.log(pressedKey);
if (!/^\s*\}$/.test(pressedKey)) {
return;
}
let editor = window.activeTextEditor;
let document = editor.document;
let originalStart = editor.selection.start;
commands.executeCommand("editor.action.jumpToBracket").then(() => {
let start = editor.selection.start;
let offsetStart0 = document.offsetAt(originalStart);
let offsetStart1 = document.offsetAt(start);
if (offsetStart0 == offsetStart1) {
return;
}
let nextChar = document.getText(new Range(start, start.translate(0, 1)));
if (offsetStart0 - offsetStart1 < 3 || nextChar != '{') {
// jumpToBracket to wrong match bracket, do nothing
commands.executeCommand("cursorUndo");
return;
}
let line = document.lineAt(start);
let code = "<?php\n";
let dealFun = (fixed) => {
return fixed.replace(/^<\?php\r?\n/, '').replace(/\s*$/, '');
};
let searchIndex = -1;
if (/^\s*\{\s*$/.test(line.text)) {
// check previous line
let preline = document.lineAt(line.lineNumber - 1);
searchIndex = preline.text.search(/((if|for|foreach|while|switch|^\s*function\s+\w+|^\s*function\s*)\s*\(.+?\)|(class|trait|interface)\s+[\w ]+|do|try)\s*$/i);
if (searchIndex > -1) {
line = preline;
}
} else {
searchIndex = line.text.search(/((if|for|foreach|while|switch|^\s*function\s+\w+|^\s*function\s*)\s*\(.+?\)|(class|trait|interface)\s+[\w ]+|do|try)\s*\{\s*$/i);
}
if (searchIndex > -1) {
start = line.range.start;
} else {
// indent + if(1)
code += line.text.match(/^(\s*)\S+/)[1] + "if(1)";
dealFun = (fixed) => {
let match = fixed.match(/^<\?php\s+?if\s*\(\s*1\s*\)\s*(\{[\s\S]+?\})\s*$/i);
if (match != null) {
fixed = match[1];
} else {
fixed = '';
}
return fixed;
};
}
commands.executeCommand("cursorUndo").then(() => {
let end = editor.selection.start;
let range = new Range(start, end);
let originalText = code + document.getText(range);
this.format(originalText).then((text) => {
if (text != originalText) {
if (dealFun) text = dealFun(text);
editor.edit((builder) => {
builder.replace(range, text);
}).then(() => {
if (editor.selections.length > 0) {
commands.executeCommand("cancelSelection");
}
});
}
});
});
});
}
doAutoFixBySemicolon(event) {
if (event.contentChanges.length == 0) return;
let pressedKey = event.contentChanges[0].text;
// console.log(pressedKey);
if (pressedKey != ';') {
return;
}
let editor = window.activeTextEditor;
let line = editor.document.lineAt(editor.selection.start);
if (line.text.length < 5) {
return;
}
let dealFun = (fixed) => {
return fixed.replace(/^<\?php\r?\n/, '').replace(/\s*$/, '');
};
let range = line.range;
let originalText = '<?php\n' + line.text;
this.format(originalText).then((text) => {
if (text != originalText) {
if (dealFun) text = dealFun(text);
editor.edit((builder) => {
builder.replace(range, text);
}).then(() => {
if (editor.selections.length > 0) {
commands.executeCommand("cancelSelection");
}
});
}
});
}
beautifyHtml(text, options) {
if (this.formatHtml) {
function getFormatOption(options, key, dflt) {
if (options && Object.prototype.hasOwnProperty.call(options, key)) {
let value = options[key];
if (value !== null) {
return value;
}
}
return dflt;
}
function getTagsFormatOption(options, key, dflt) {
let list = getFormatOption(options, key, null);
if (typeof list === 'string') {
if (list.length > 0) {
return list.split(',').map(t => t.trim().toLowerCase());
}
return [];
}
return dflt;
}
let htmlOptions = {
indent_size: options.insertSpaces ? options.tabSize : 1,
indent_char: options.insertSpaces ? ' ' : '\t',
wrap_line_length: getFormatOption(options, 'wrapLineLength', 120),
unformatted: getTagsFormatOption(options, 'unformatted', [
// HTLM void elements - aka self-closing tags - aka singletons
// https://www.w3.org/html/wg/drafts/html/master/syntax.html#void-elements
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen',
'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr',
// NOTE: Optional tags - are not understood.
// https://www.w3.org/TR/html5/syntax.html#optional-tags
// The rules for optional tags are too complex for a simple list
// Also, the content of these tags should still be indented in many cases.
// 'li' is a good exmple.
// Doctype and xml elements
'!doctype', '?xml',
// ?php tag
'?php', '?=',
// other tags that were in this list, keeping just in case
'basefont', 'isindex'
]),
content_unformatted: getTagsFormatOption(options, 'contentUnformatted', void 0),
indent_inner_html: getFormatOption(options, 'indentInnerHtml', false),
preserve_newlines: getFormatOption(options, 'preserveNewLines', false),
max_preserve_newlines: getFormatOption(options, 'maxPreserveNewLines', void 0),
indent_handlebars: getFormatOption(options, 'indentHandlebars', false),
end_with_newline: getFormatOption(options, 'endWithNewline', false),
extra_liners: getTagsFormatOption(options, 'extraLiners', void 0),
wrap_attributes: getFormatOption(options, 'wrapAttributes', 'auto')
};
return html_beautify(text, htmlOptions);
} else {
return text;
}
}
}
exports.activate = (context) => {
let pcf = new PHPCSFixer();
context.subscriptions.push(workspace.onWillSaveTextDocument((event) => {
if (event.document.languageId == 'php' && pcf.onsave && workspace.getConfiguration('editor').get('formatOnSave') == false) {
event.waitUntil(commands.executeCommand("editor.action.formatDocument"));
}
}));
context.subscriptions.push(commands.registerTextEditorCommand('php-cs-fixer.fix', (textEditor) => {
if (textEditor.document.languageId == 'php') {
commands.executeCommand("editor.action.formatDocument");
}
}));
context.subscriptions.push(workspace.onDidChangeTextDocument((event) => {
if (event.document.languageId == 'php' && autoFixing == false) {
if (pcf.autoFixByBracket) {
pcf.doAutoFixByBracket(event);
}
if (pcf.autoFixBySemicolon) {
pcf.doAutoFixBySemicolon(event);
}
}
}));
context.subscriptions.push(workspace.onDidChangeConfiguration(() => {
pcf.loadSettings();
}));
if (pcf.documentFormattingProvider) {
context.subscriptions.push(languages.registerDocumentFormattingEditProvider('php', {
provideDocumentFormattingEdits: (document, options, token) => {
autoFixing = false;
return new Promise((resolve, reject) => {
let originalText = document.getText();
let lastLine = document.lineAt(document.lineCount - 1);
let range = new Range(new Position(0, 0), lastLine.range.end);
let htmlOptions = Object.assign(options, workspace.getConfiguration('html').get('format'));
let originalText2 = pcf.beautifyHtml(originalText, htmlOptions);
pcf.format(originalText2).then((text) => {
if (text != originalText) {
resolve([new vscode.TextEdit(range, text)]);
} else {
reject();
}
}).catch(err => {
reject();
});
});
}
}));
context.subscriptions.push(languages.registerDocumentRangeFormattingEditProvider('php', {
provideDocumentRangeFormattingEdits: (document, range, options, token) => {
autoFixing = false;
return new Promise((resolve, reject) => {
let originalText = document.getText(range);
if (originalText.replace(/\s+/g, '').length == 0) {
reject();
return;
}
let addPHPTag = false;
if (originalText.search(/^\s*<\?php/i) == -1) {
originalText = "<?php\n" + originalText;
addPHPTag = true;
}
pcf.format(originalText).then((text) => {
if (addPHPTag) {
text = text.replace(/^<\?php\r?\n/, '');
}
if (text != originalText) {
resolve([new vscode.TextEdit(range, text)]);
} else {
reject();
}
}).catch(err => {
reject();
});
});
}
}));
}
};