|
| 1 | +const vscode = require('vscode'); |
| 2 | +const path = require('path'); |
| 3 | +const cp = require('child_process'); |
| 4 | + |
| 5 | +function fixDocument(document) { |
| 6 | + if (document.languageId !== 'php') { |
| 7 | + return; |
| 8 | + } |
| 9 | + |
| 10 | + let toolPath = getConfig('toolPath'); |
| 11 | + let filename = document.fileName; |
| 12 | + let args = ['fix']; |
| 13 | + let opts = { cwd: path.dirname(filename) }; |
| 14 | + |
| 15 | + if (!getConfig('useCache')) { |
| 16 | + args.push('--using-cache=no'); |
| 17 | + } |
| 18 | + |
| 19 | + let rules = getConfig('rules'); |
| 20 | + if (rules) { |
| 21 | + args.push('--rules=' + rules); |
| 22 | + } |
| 23 | + |
| 24 | + cp.execFile(toolPath, [...args, filename], opts, function (err) { |
| 25 | + if (err && err.code === 'ENOENT') { |
| 26 | + vscode.window.showErrorMessage('Unable to find the php-cs-fixer tool.'); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + if (err) { |
| 31 | + vscode.window.showErrorMessage('There was an error while running php-cs-fixer. Check the Developer Tools console for more information.'); |
| 32 | + |
| 33 | + console.log(err); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + document.save(); |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +function getConfig(key) { |
| 42 | + return vscode.workspace.getConfiguration('vscode-php-cs-fixer').get(key); |
| 43 | +} |
| 44 | + |
| 45 | +function activate(context) { |
| 46 | + context.subscriptions.push(vscode.commands.registerTextEditorCommand('vscode-php-cs-fixer.fix', function (textEditor) { |
| 47 | + fixDocument(textEditor.document); |
| 48 | + })); |
| 49 | + |
| 50 | + vscode.workspace.onDidSaveTextDocument(function (document) { |
| 51 | + if (!getConfig('fixOnSave')) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + fixDocument(document); |
| 56 | + }); |
| 57 | +} |
| 58 | +exports.activate = activate; |
| 59 | + |
| 60 | +function deactivate() { |
| 61 | +} |
| 62 | +exports.deactivate = deactivate; |
0 commit comments