-
Notifications
You must be signed in to change notification settings - Fork 4
/
extension.js
192 lines (156 loc) · 5.58 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
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 phpCmd = getConfig('phpCmd')
let filename = document.fileName
let args = []
let opts = {
cwd: path.dirname(filename),
shell: true,
}
if (getConfig('ignoreEnv')) {
opts.env = { PHP_CS_FIXER_IGNORE_ENV: 1 }
}
if (getFixerFromComposer()) {
toolPath = getFixerFromComposer()
}
if (!toolPath) {
toolPath = path.resolve(__dirname, 'php-cs-fixer')
}
if (!phpCmd) {
phpCmd = 'php'
}
args.push(toolPath)
args.push('fix')
if (!getConfig('useCache')) {
args.push('--using-cache=no')
}
if (getConfig('allowRisky')) {
args.push('--allow-risky=yes')
}
let config = getConfig('config')
if (getPreset()) {
args.push('--config=' + getPreset())
} else 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 if (projectConfigFile()) {
args.push('--config=' + projectConfigFile())
} else {
let rules = getConfig('rules')
if (rules) {
args.push(`--rules='${rules}'`)
}
}
const tmpFile = tmp.fileSync()
fs.writeFileSync(tmpFile.name, document.getText(null))
return new Promise(function (resolve) {
cp.execFile(phpCmd, [...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 getProjectRoot() {
return vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri).uri.fsPath
}
function getFixerFromComposer() {
let composerPath = getProjectRoot() + path.sep + 'vendor' + path.sep + 'bin' + path.sep + 'php-cs-fixer'
if (fs.existsSync(composerPath)) {
return composerPath
}
return null
}
function projectConfigFile() {
if (fs.existsSync(getProjectRoot() + path.sep + '.php-cs-fixer.php')) {
return getProjectRoot() + path.sep + '.php-cs-fixer.php'
}
if (fs.existsSync(getProjectRoot() + path.sep + '.php-cs-fixer.dist.php')) {
return getProjectRoot() + path.sep + '.php-cs-fixer.dist.php'
}
return null
}
function getPreset() {
const preset = getConfig('preset')
if (['laravel', 'per', 'psr12', 'symfony'].includes(preset.toLowerCase())) {
return path.resolve(__dirname, `presets/${preset.toLowerCase()}.php`)
} else if (preset) {
vscode.window.showErrorMessage(`Unable to find preset: ${preset}.`)
}
}
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('php-cs-fixer').get(key)
}
function activate(context) {
context.subscriptions.push(
vscode.commands.registerTextEditorCommand('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')) {
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