This repository has been archived by the owner on Aug 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
extension.js
128 lines (104 loc) · 4.04 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
const vscode = require('vscode');
const path = require('path');
const fs = require('fs');
const tmp = require('tmp');
const cp = require('child_process');
function formatDocument(document) {
if (document.languageId !== 'php') {
return;
}
let toolPath = getConfig('toolPath');
let filename = document.fileName;
let args = [];
let opts = { cwd: path.dirname(filename) };
if (!toolPath) {
toolPath = vscode.extensions.getExtension('fterrag.vscode-php-cs-fixer').extensionPath + '/php-cs-fixer';
}
args.push(toolPath);
args.push('fix');
if (!getConfig('useCache')) {
args.push('--using-cache=no');
}
if (getConfig('allowRisky')) {
args.push('--allow-risky=yes');
}
if (getConfig('intersection')) {
args.push('--path-mode=intersection');
}
let config = getConfig('config');
if (config) {
// Support config file with relative path
if (!path.isAbsolute(config)) {
let currentPath = opts.cwd;
let triedPaths = [currentPath];
while (!fs.existsSync(currentPath + path.sep + config)) {
let lastPath = currentPath;
currentPath = path.dirname(currentPath);
if (lastPath == currentPath) {
vscode.window.showErrorMessage(`Unable to find ${config} file in ${triedPaths.join(", ")}`);
return;
} else {
triedPaths.push(currentPath);
}
}
config = currentPath + path.sep + config;
}
args.push('--config=' + config);
} else {
let rules = getConfig('rules');
if (rules) {
args.push('--rules=' + rules);
}
}
const tmpFile = tmp.fileSync();
fs.writeFileSync(tmpFile.name, document.getText(null));
// console.log('php-cs-fixer temporary file: ' + tmpFile.name);
return new Promise(function(resolve) {
cp.execFile('php', [...args, tmpFile.name], opts, function (err) {
if (err) {
tmpFile.removeCallback();
if (err.code === 'ENOENT') {
vscode.window.showErrorMessage('Unable to find the php-cs-fixer tool.');
throw err;
}
vscode.window.showErrorMessage('There was an error while running php-cs-fixer. Check the Developer Tools console for more information.');
throw err;
}
const text = fs.readFileSync(tmpFile.name, 'utf-8');
tmpFile.removeCallback();
resolve(text);
});
});
}
function registerDocumentProvider(document, options) {
return new Promise(function(resolve, reject) {
formatDocument(document).then(function(text) {
const range = new vscode.Range(new vscode.Position(0, 0), document.lineAt(document.lineCount - 1).range.end);
resolve([new vscode.TextEdit(range, text)]);
}).catch(function(err) {
reject();
});
});
}
function getConfig(key) {
return vscode.workspace.getConfiguration('vscode-php-cs-fixer').get(key);
}
function activate(context) {
context.subscriptions.push(vscode.commands.registerTextEditorCommand('vscode-php-cs-fixer.fix', function (textEditor) {
vscode.commands.executeCommand('editor.action.formatDocument');
}));
context.subscriptions.push(vscode.workspace.onWillSaveTextDocument(function(event) {
if (event.document.languageId === 'php' && getConfig('fixOnSave') && vscode.workspace.getConfiguration('editor', null).get('formatOnSave') == false) {
event.waitUntil(vscode.commands.executeCommand('editor.action.formatDocument'));
}
}));
context.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider('php', {
provideDocumentFormattingEdits: function(document, options) {
return registerDocumentProvider(document, options);
}
}));
}
exports.activate = activate;
function deactivate() {
}
exports.deactivate = deactivate;