-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodeformater.py
More file actions
257 lines (207 loc) · 8.08 KB
/
codeformater.py
File metadata and controls
257 lines (207 loc) · 8.08 KB
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
import sublime
import sublime_plugin
import subprocess
import os
import threading
import stat
import locale
def plugin_loaded():
def changeRegistry():
import winreg as wr
keyVal = r'Software\CodeGear\BDS\6.0\Jedi\JCF\General'
try:
key = wr.OpenKey(wr.HKEY_CURRENT_USER, keyVal, 0,
wr.KEY_ALL_ACCESS)
except:
key = wr.CreateKey(wr.HKEY_CURRENT_USER, keyVal)
wr.SetValueEx(key, "FormatConfigFileName", 0, wr.REG_SZ,
sublime.packages_path() + "\Delphi-IDE\JCFSettings.cfg")
wr.CloseKey(key)
changeRegistry()
def exeIsRunning(exe):
def path_leaf(path):
import ntpath
head, tail = ntpath.split(path)
return tail or ntpath.basename(head)
filename = path_leaf(exe)
cmd = 'WMIC PROCESS get Caption'
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
for line in proc.stdout:
lin = line.split()
try:
if lin[0].decode("utf-8") == filename:
return True
except:
pass
return False
class TFormatter(object):
"""docstring for TFormatter"""
def formatCode(self, view):
default_formatter = sublime.packages_path() + "\Delphi-IDE\JCF.exe"
print('formatCode')
s = sublime.load_settings("delphi-ide.sublime-settings")
path_formatter = s.get("path_formatter", default_formatter)
if view.get_status('formatter'):
sublime.message_dialog('File is already formatting.')
return
if exeIsRunning(path_formatter):
sublime.message_dialog('Executable is already running.')
return
view.erase_regions('codeerror')
path = view.file_name()
if (path is None):
return
auto_format = s.get("auto_format", False)
if not auto_format:
return
filename, file_extension = os.path.splitext(path)
if not file_extension in ['.pas', '.dpr', '.pp']:
return
validate_encode = s.get("validate_encode", False)
if validate_encode:
encode = s.get("encode", 'Western (Windows 1252)')
if not (view.encoding() == encode):
sublime.message_dialog('Encode: ' + view.encoding())
return
vssyntax = view.settings().get('syntax')
if not (((vssyntax.find('Pascal.tmLanguage') > 0) or
(vssyntax.find('Pascal.sublime-syntax') > 0)) or
((vssyntax.find('delphi.tmLanguage') > 0) or
(vssyntax.find('delphi.sublime-syntax') > 0)
)):
return
if isReadonly(path):
return
use_default_formatter = default_formatter == path_formatter
ThreadFormatter = TFormatterThread(
view, path_formatter, path, use_default_formatter)
ThreadFormatter.start()
def isReadonly(p_path):
try:
fileAttrs = os.stat(p_path)
fileAtt = fileAttrs[0]
return not fileAtt & stat.S_IWRITE
except WindowsError:
pass
class TFormatterThread(threading.Thread):
def __init__(self, view, exe, param, default):
super(TFormatterThread, self).__init__()
self.view = view
self.exe = exe
self.param = param
self.formatter = self.getFormatterObject(default)
def run(self):
try:
self.view.set_status('formatter', 'Formatter: Formatting...')
self.formatter.run_command(self.exe, self.param)
except FileNotFoundError:
raise
finally:
self.view.erase_status('formatter')
def getFormatterObject(self, default):
if default:
return self.DefaultFormatter(self.view)
else:
return self.OtherFormatter(self.view)
class OtherFormatter(object):
"""docstring for OtherFormatter"""
def __init__(self, view):
self.view = view
def run_command(self, exe, param):
s = sublime.load_settings("delphi-ide.sublime-settings")
Other_params = s.get("other_params", '')
command = ('"' + exe + '" ' + Other_params + ' "' + param + '"')
startup_info = subprocess.STARTUPINFO()
startup_info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
p = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
startupinfo=startup_info
)
(out, err) = p.communicate()
out = (out.decode(locale.getpreferredencoding())
if not out is None else None)
err = (err.decode(locale.getpreferredencoding())
if not err is None else None)
msglist = []
msg = out.split('\r\n')
for s in msg:
if s.find(param) == 0:
msglist.append(s[len(param):].lstrip())
else:
msglist.append(s.lstrip())
else:
out_msg = '\n'.join(msglist)
if out_msg:
sublime.message_dialog(out_msg)
class DefaultFormatter(object):
"""docstring for DefaultFormatter"""
def __init__(self, view):
self.view = view
def run_command(self, exe, param):
default_param = ' -F -inplace -clarify '
command = ('"' + exe + '"' + default_param + '"' + param + '"')
startup_info = subprocess.STARTUPINFO()
startup_info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
p = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
startupinfo=startup_info
)
(out, err) = p.communicate()
out = (out.decode(locale.getpreferredencoding())
if not out is None else None)
msglist = []
if p.returncode == 0:
msg = out.split('\r\n')
for s in msg:
if s.find(param) == 0:
msglist.append(s[len(param):].lstrip())
else:
msglist.append(s.lstrip())
else:
out_msg = '\n'.join(msglist)
elif p.returncode == 1:
err = (err.decode(locale.getpreferredencoding())
if not err is None else None)
err = err.split('\r\n')
out_msg = '\n'.join(err)
elif p.returncode == 7:
msg = out.split('\r\n')
for s in msg:
if s.find('(') >= 0:
region = s[s.find('(') + 1:s.find(')')]
region = region.split(',')
if s.find(param) == 0:
msglist.append(s[len(param):].lstrip())
else:
msglist.append(s.lstrip())
else:
out_msg = '\n'.join(msglist)
style = (sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE |
sublime.DRAW_SQUIGGLY_UNDERLINE)
sview = self.view
pt = sview.text_point(int(region[0]) - 1, int(region[1]))
word = sview.word(pt)
sview.add_regions('codeerror', [word], 'invalid',
'Packages/Delphi-IDE/warning.png', style)
sview.show(word)
else:
msg = out.split('\r\n')
for s in msg:
if s.find(param) == 0:
msglist.append(s[len(param):].lstrip())
else:
msglist.append(s.lstrip())
else:
out_msg = '\n'.join(msglist)
if out_msg:
sublime.message_dialog(out_msg)
class TEventListener(sublime_plugin.EventListener):
def on_pre_save(self, view):
Formatter = TFormatter()
Formatter.formatCode(view)