-
Notifications
You must be signed in to change notification settings - Fork 20
/
EsFormatter.py
302 lines (253 loc) · 11.1 KB
/
EsFormatter.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
import sublime, sublime_plugin, subprocess, threading, json, re, platform, sys, os
ON_WINDOWS = platform.system() is 'Windows'
ST2 = sys.version_info < (3, 0)
class NodeCheck:
'''This class check whether esformatter is installed and available in the path.
The check is done only once when mightWork() is called for the first time.
Being a tri-state class it's better not accessing it's properties but only call mightWork()'''
def __init__(self):
self.works = False
self.nodeName = "esformatter.cmd" if ON_WINDOWS else "esformatter"
self.cwd = "."
def mightWork(self, path, cwd):
if (path):
self.nodeName = path
if (cwd):
self.cwd = cwd
self.tryWithSelfName()
if (self.works is False):
sublime.error_message("It looks like esformatter is not installed.\nPlease make sure that it is installed globally or in project node_modules folder")
return self.works
def tryWithSelfName(self):
try:
# call node version with whatever path is defined in nodeName
esformatter_executable = findExecutablePath(self.cwd)
if (esformatter_executable):
subprocess.Popen(["node", esformatter_executable, "--version"], bufsize=1, stdin=None, stdout=None, stderr=None, startupinfo=getStartupInfo())
else:
subprocess.Popen([self.nodeName, "--version"], bufsize=1, stdin=None, stdout=None, stderr=None, startupinfo=getStartupInfo())
self.works = True
except OSError as e:
self.works = False
def findExecutablePath(folder):
target = os.path.join(folder, 'node_modules', 'esformatter', 'bin', 'esformatter')
if (os.path.isfile(target)):
return target
else:
parent = os.path.abspath(os.path.join(folder, os.pardir))
if (parent != folder):
return findExecutablePath(parent)
else:
return None
def findLocalConfigPath(folder):
settings = sublime.load_settings("EsFormatter.sublime-settings")
configNames = settings.get("esformatter_config_file")
for configName in configNames:
target = os.path.join(folder, configName)
if (os.path.isfile(target)):
return target
parent = os.path.abspath(os.path.join(folder, os.pardir))
if (parent != folder):
return findLocalConfigPath(parent)
else:
return None
NODE = NodeCheck()
# I don't really like this, but formatting is async, so I must
# save the file again after it's been formatted (auto_format)
# This flag prevents loops
AM_I_FORMATTING_AFTER_SAVE = False
if not ON_WINDOWS:
# Extend Path to catch Node installed via HomeBrew
os.environ['PATH'] += ':/usr/local/bin'
def getNpmGlobalRoot():
# determine NPM global root
try:
return subprocess.check_output(["npm", "root", "-g"]).rstrip().decode('utf-8')
except:
# NPM not installed or not accessible
return None
# Extend NODE_PATH to make globally installed esformatter requirable
npmRoot = getNpmGlobalRoot()
if npmRoot:
if hasattr(os.environ, 'NODE_PATH'):
os.environ['NODE_PATH'] += os.pathsep + npmRoot
else:
os.environ['NODE_PATH'] = npmRoot
class EsformatterCommand(sublime_plugin.TextCommand):
def run(self, edit, save=False, ignoreSelection=False):
# Settings for formatting
settings = sublime.load_settings("EsFormatter.sublime-settings")
cwd = os.path.dirname(getFilePath(self.view))
if (NODE.mightWork(settings.get("esformatter_path"), cwd) == False):
return
if (ignoreSelection or len(self.view.sel()) == 1 and self.view.sel()[0].empty()):
# Only one caret and no text selected, format the whole file
textContent = self.view.substr(sublime.Region(0, self.view.size()))
thread = NodeCall(textContent, getFilePath(self.view))
thread.start()
self.handle_thread(thread, lambda: self.replaceFile(thread, save))
else:
# Format each and every selection block
threads = []
for selection in self.view.sel():
# Take everything from the beginning to the end of line
region = self.view.line(selection)
textContent = self.view.substr(region)
thread = NodeCall(textContent, getFilePath(self.view), len(threads), region)
threads.append(thread)
thread.start()
self.handle_threads(threads, lambda process, lastError: self.handleSyntaxErrors(process, lastError))
def replaceFile(self, thread, save=False):
'''Replace the entire file content with the formatted text.'''
sublime.status_message("File formatted")
if thread.code == thread.result.encode('utf-8'):
return
self.view.run_command("esformat_update_content", {"text": thread.result})
if (save):
self.view.run_command("save")
def handleSyntaxErrors(self, threads=None, lastError=None):
'''When formatting whole lines there might be a syntax error because we select
the whole line content. In that case, fall-back to the user selection.'''
if (lastError is None and threads is not None):
self.replaceSelections(threads, None)
else:
# Format each and every selection block
threads = []
for selection in self.view.sel():
# Take only the user selection
textContent = self.view.substr(selection)
thread = NodeCall(textContent, getFilePath(self.view), len(threads), selection)
threads.append(thread)
thread.start()
self.handle_threads(threads, lambda process, lastError: self.replaceSelections(process, lastError))
def replaceSelections(self, threads, lastError):
'''Replace the content of a list of selections.
This is called when there are multiple cursors or a selection of text'''
if (lastError):
sublime.error_message("Error (2):" + lastError)
else:
# Modify the selections from top to bottom to account for different text length
offset = 0
regions = []
for thread in sorted(threads, key=lambda t: t.region.begin()):
if thread.code == thread.result.encode('utf-8'):
continue
if offset:
region = [thread.region.begin() + offset, thread.region.end() + offset, thread.result]
else:
region = [thread.region.begin(), thread.region.end(), thread.result]
offset += len(thread.result) - len(thread.code)
regions.append(region)
self.view.run_command("esformat_update_content", {"regions": regions})
sublime.status_message("Selections formatted")
def handle_thread(self, thread, callback):
if thread.is_alive():
sublime.set_timeout(lambda: self.handle_thread(thread, callback), 100)
elif thread.result is not False:
callback()
else:
sublime.error_message("Error (1):" + thread.error)
def handle_threads(self, threads, callback, process=False, lastError=None):
next_threads = []
if process is False:
process = []
for thread in threads:
if thread.is_alive():
next_threads.append(thread)
continue
if thread.result is False:
# This thread failed
lastError = thread.error
continue
# Thread completed correctly
process.append(thread)
if len(next_threads):
# Some more threads to wait
sublime.set_timeout(lambda: self.handle_threads(next_threads, callback, process, lastError), 100)
else:
callback(process, lastError)
class NodeCall(threading.Thread):
def __init__(self, code, path, id=0, region=None):
self.code = code.encode('utf-8')
self.cwd = os.path.dirname(path)
self.region = region
self.result = None
threading.Thread.__init__(self)
def readResult(self, stdout):
if ST2:
return stdout.decode('utf-8')
else:
return str(stdout, encoding='utf-8')
def run(self):
try:
sublime.status_message("Formatting file...")
esformatter_executable = findExecutablePath(self.cwd)
if (esformatter_executable):
cmd = ["node", esformatter_executable]
else:
cmd = ["esformatter.cmd" if ON_WINDOWS else "esformatter"]
esformatter_config_file = findLocalConfigPath(self.cwd)
if (esformatter_config_file):
cmd.append("--config")
cmd.append(esformatter_config_file)
process = subprocess.Popen(
cmd,
bufsize=160*len(self.code),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
startupinfo=getStartupInfo())
stdout, stderr = process.communicate(self.code)
if stderr:
self.result = False
self.error = self.readResult(stderr)
else:
self.result = self.readResult(stdout)
if self.region:
self.result = re.sub(r'(\r|\r\n|\n)\Z', '', self.result)
except Exception as e:
self.result = False
self.error = str(e)
def getStartupInfo():
if ON_WINDOWS:
info = subprocess.STARTUPINFO()
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = subprocess.SW_HIDE
return info
return None
def getFilePath(view):
path = view.file_name()
if (path):
return str(path)
else:
return ''
class EsformatUpdateContent(sublime_plugin.TextCommand):
def run(self, edit, text=None, regions=None):
if text:
self.view.replace(edit, sublime.Region(0, self.view.size()), text)
else:
for region in regions:
self.view.replace(edit, sublime.Region(region[0], region[1]), region[2])
class EsformatEventListener(sublime_plugin.EventListener):
def on_pre_save(self, view):
global AM_I_FORMATTING_AFTER_SAVE
if AM_I_FORMATTING_AFTER_SAVE:
AM_I_FORMATTING_AFTER_SAVE = False
return
AM_I_FORMATTING_AFTER_SAVE = True
settings = sublime.load_settings("EsFormatter.sublime-settings")
if (settings.get("format_on_save") and self.isJavascript(view)):
view.window().run_command("esformatter", {
"save": True,
"ignoreSelection": True
})
def isJavascript(self, view):
# Check the file extension
name = view.file_name()
if (name and os.path.splitext(name)[1][1:] in ["js"]):
return True
# If it has no name (?) or it's not a JS, check the syntax
syntax = view.settings().get("syntax")
if (syntax and "javascript" in syntax.split("/")[-1].lower()):
return True
return False