-
Notifications
You must be signed in to change notification settings - Fork 14
/
SublimePhpCsFixer.py
380 lines (274 loc) · 10.2 KB
/
SublimePhpCsFixer.py
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
import sublime
import sublime_plugin
import os
import tempfile
import subprocess
import re
import json
def load_settings():
return sublime.load_settings("SublimePhpCsFixer.sublime-settings")
def setting_enabled(name):
return load_settings().get(name)
def is_windows():
return sublime.platform() == "windows"
def is_executable_file(file_path):
return os.path.isfile(file_path) and os.access(file_path, os.X_OK)
def is_readable_file(file_path):
return os.path.isfile(file_path) and os.access(file_path, os.R_OK)
def which(program):
"""Code from: https://stackoverflow.com/a/377028/584639"""
fpath, fname = os.path.split(program)
if fpath:
if is_executable_file(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_executable_file(exe_file):
return exe_file
return None
def fixer_possible_paths():
paths = []
if is_windows():
executable_name = "php-cs-fixer.bat"
else:
executable_name = "php-cs-fixer"
project_data = sublime.active_window().project_data()
if project_data:
project_path = project_data['folders'][0]['path']
paths.append(os.path.join(project_path, "vendor", "bin", executable_name))
if "COMPOSER_HOME" in os.environ:
paths.append(os.path.join(
os.environ["COMPOSER_HOME"], "vendor", "bin", executable_name))
if "APPDATA" in os.environ:
paths.append(os.path.join(
os.environ["APPDATA"], "composer", "bin", executable_name))
if "HOME" in os.environ:
paths.append(os.path.join(
os.environ["HOME"],
".composer",
"vendor",
"bin",
executable_name))
paths.append(os.path.join(
os.environ["HOME"],
".config",
"composer",
"vendor",
"bin",
executable_name))
paths.append(which(executable_name))
return paths
def create_process_for_platform(cmd):
if is_windows():
# We need to hide the console window
si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
else:
si = None
return subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
bufsize=0,
startupinfo=si)
def get_project_folder(file):
project_data = sublime.active_window().project_data()
if not project_data:
return None
project_folders = project_data.get('folders', [])
project_paths = (p['path'] for p in project_folders)
for path in project_paths:
if file.startswith(path):
return path
return None
class Logger:
def __init__(self, settings):
self.settings = settings
def console(self, msg):
print("PHP CS Fixer: {0}".format(msg))
def debug(self, msg):
if self.settings.get("debug"):
print("PHP CS Fixer (DEBUG): {0}".format(msg))
class FormatterSettings:
def __init__(self, settings):
self.settings = settings
self.variables = self.get_active_window_variables()
def get(self, key):
return self.settings.get(key)
def get_expanded(self, key):
return self.expand(self.settings.get(key))
def expand(self, value):
return sublime.expand_variables(value, self.variables)
def get_active_window_variables(self):
variables = sublime.active_window().extract_variables()
if 'file' in variables:
folder = get_project_folder(variables['file'])
if folder:
variables['folder'] = folder
return variables
class FixerProcess:
def __init__(self, settings: FormatterSettings, logger: Logger):
self.logger = logger
self.settings = settings
def run(self, tmp_file):
cmd = self.create_cmd(tmp_file)
self.logger.debug("Using cmd:")
self.logger.debug(cmd)
p = create_process_for_platform(cmd)
output, err = p.communicate()
if p.returncode != 0:
self.logger.console("There was an error formatting the view")
self.logger.console("Command: {0}".format(cmd))
self.logger.console("Error output: {0}".format(err))
def create_cmd(self, tmp_file):
config = self.config_param()
rules = self.rules_param()
allow_risky = self.allow_risky_param()
if rules and config:
self.logger.console("rules and config are both present, rules prevails")
config = None
return list(filter(None, [
self.settings.get_expanded('php_path'),
self.get_configured_php_cs_fixer_path(),
"fix",
rules,
config,
allow_risky,
"--using-cache=no",
tmp_file,
]))
def config_param(self):
configs = self.settings.get("config")
if not configs:
return None
if not type(configs) is list:
configs = [configs]
for config in configs:
config_path = self.settings.expand(config)
self.logger.debug("Looking for config: " + config_path)
if is_readable_file(config_path):
self.logger.console("Using config: " + config_path)
return '--config=' + config_path
self.logger.debug("Not using config")
return None
def rules_param(self):
rules = self.settings.get('rules')
if not rules:
return None
if isinstance(rules, list):
rules = ",".join(rules)
if isinstance(rules, dict):
rules = json.dumps(rules)
if isinstance(rules, str):
self.logger.console("Using rules: " + rules)
return "--rules=" + rules
return None
def allow_risky_param(self):
if self.settings.get("allow_risky"):
return "--allow-risky=yes"
return None
def get_configured_php_cs_fixer_path(self):
path = self.settings.get_expanded('path')
if not path:
path = self.locate_php_cs_fixer()
if not path:
raise ExecutableNotFoundException("Couldn't find php-cs-fixer")
if not is_executable_file(path):
raise ExecutableNotFoundException(
"Couldn't execute file: {0}".format(path))
return path
def locate_php_cs_fixer(self):
paths = fixer_possible_paths()
for path in paths:
self.logger.debug("looking for php-cs-fixer at: " + path)
if is_executable_file(path):
self.logger.console("autodetected: " + path)
return path
self.logger.console("php-cs-fixer file not found")
class ViewFormatter:
def __init__(self, settings: FormatterSettings, logger):
self.settings = settings
self.logger = logger
def format(self, contents):
self.logger.console("Formatting view...")
return self.format_contents(contents)
def format_contents(self, contents):
"""
Write the contents in a temporary file, format it with php-cs-fixer and returns the formatted contents.
For supporting ST2 and ST3, I do use the following, because it's compatible in python 2/3 and seems
to work properly.
- file.write(contents.encode(encoding))
- file.read().decode(encoding)
:param contents:
:return:
"""
fd, tmp_file = tempfile.mkstemp()
encoding = self.settings.get("encoding")
with open(tmp_file, 'wb') as file:
file.write(contents.encode(encoding))
file.close()
try:
self.format_file(tmp_file)
with open(tmp_file, 'rb') as file:
content = file.read().decode(encoding)
file.close()
finally:
os.close(fd)
os.remove(tmp_file)
return content
def format_file(self, tmp_file):
fixer = FixerProcess(self.settings, self.logger)
fixer.run(tmp_file)
class SublimePhpCsFixCommand(sublime_plugin.TextCommand):
def __init__(self, view):
sublime_plugin.TextCommand.__init__(self, view)
self.settings = load_settings()
self.logger = Logger(self.settings)
def is_enabled(self):
return self.is_supported_scope()
def run(self, edit):
try:
self.format(edit)
except ExecutableNotFoundException as e:
self.logger.console(str(e))
def format(self, edit):
region = sublime.Region(0, self.view.size())
contents = self.view.substr(region)
if not contents:
self.logger.console("Done. No contents")
return
formatter = ViewFormatter(
FormatterSettings(self.settings), self.logger)
new_contents = formatter.format(contents)
if new_contents and new_contents != contents:
self.view.replace(edit, region, new_contents)
self.logger.console("Done. View formatted")
else:
self.logger.console("Done. No changes")
def is_supported_scope(self):
scopes = self.view.scope_name(self.view.sel()[0].begin())
return 'embedding.php' in scopes and not self.is_excluded()
def is_excluded(self):
if not self.settings.has('exclude'):
return False
exclude = self.settings.get('exclude')
file_name = self.view.file_name()
if not type(exclude) is list:
exclude = [exclude]
for pattern in exclude:
if re.match(pattern, file_name) is not None:
self.logger.console(
file_name + ' is excluded via pattern: ' + pattern)
return True
return False
class SublimePhpCsFixListener(sublime_plugin.EventListener):
def on_pre_save(self, view):
if setting_enabled('on_save'):
view.run_command('sublime_php_cs_fix')
def on_load(self, view):
if setting_enabled('on_load'):
view.run_command('sublime_php_cs_fix')
class ExecutableNotFoundException(BaseException):
pass