-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtclfmt.py
401 lines (330 loc) · 10.5 KB
/
tclfmt.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sublime
import sublime_plugin
import os
import io
gEmpty = 0
curFile = ""
gIndentSize = 4
gIndent = 0
gWrapLines = []
gStoreLines = {
"set": [],
"pkg": [],
"cfg": [],
"key": [],
}
gFormatBaseIndex = {
"set": 1,
"pkg": 2,
"cfg": 2,
"key": 0,
}
class LineAttr:
def __init__(self):
self.empty = 0
self.set = 0
self.blockStart = 0
self.blockMid = 0
self.blockEnd = 0
self.wrap = 0
self.package = 0
self.comment = 0
self.structCfg = 0
self.switch = 0
self.switchKey = 0
def lineTrim(line):
newLine = line.strip()
if len(newLine) != 0 and newLine[0] == "#":
# No change for the comment line
return newLine
else:
words = newLine.split()
newLine = " ".join(words)
return newLine
def paddingGen(cnt):
result = ""
for _ in range(cnt):
result += " "
return result
def blockInfoGet(lines):
lblock = 0
rblock = 0
lbracket = 0
rbracket = 0
for _, line in enumerate(lines):
lblock += line.count("{")
rblock += line.count("}")
lbracket += line.count("[")
rbracket += line.count("]")
return lblock, rblock, lbracket,rbracket
def wrapLinesStatus(lines):
warpLineStatus = ""
firstLine = lines[0]
if firstLine[0] == "#" and firstLine[-1] == "\\":
return warpLineStatus
lblock, rblock, lbracket,rbracket = blockInfoGet(lines)
if lblock > rblock:
warpLineStatus = "blockStart"
elif lblock < rblock:
warpLineStatus = "blockEnd"
elif lbracket > rbracket:
warpLineStatus = "bracketStart"
elif lbracket < rbracket:
warpLineStatus = "bracketEnd"
return warpLineStatus
def wrapLinesReformat():
# Reformat
formattedLines = []
maxWordsLen = 0
maxLen0 = 0
maxLen1 = 0
maxLenOther = 0
for i, line in enumerate(gWrapLines):
words = line.split()
if i == 0 or len(words) < 2:
continue
if maxWordsLen < len(words):
maxWordsLen = len(words)
if maxLen0 < len(words[0]):
maxLen0 = len(words[0])
if maxLen1 < len(words[1]) and len(words) > 2:
maxLen1 = len(words[1])
if maxLenOther < len(line):
maxLenOther = len(line)
maxLenOther2 = 0
for i, line in enumerate(gWrapLines):
words = line.split()
if i == 0:
# No change
formattedLines.append(line)
else:
newLine = ""
pad0 = 0
pad1 = 0
padding = paddingGen(gIndentSize)
if len(words) == 0:
continue
elif len(words) == 1:
if len(line) == 1 and line[0] == "]":
padding = ""
newLine = "%s%s" % (padding, line)
# elif maxWordsLen > 3 or "expr " in line:
elif len(words) > 3 or "expr " in line:
# It's hard to align these lines, so skip formatting
surPadding = paddingGen(maxLenOther - len(line))
newLine = line.replace("\\", surPadding+"\\")
newLine = "%s%s" % (padding, newLine)
else:
pad0 = paddingGen(maxLen0-len(words[0]))
pad1 = paddingGen(maxLen1-len(words[1]))
rest = " ".join(words[2:])
newLine = "%s%s %s%s%s %s" % (padding, words[0], pad0, words[1], pad1, rest)
newLine = newLine.rstrip()
formattedLines.append(newLine)
if maxLenOther2 < len(newLine):
maxLenOther2 = len(newLine)
# Align the first line with others
if abs(maxLenOther2 - len(formattedLines[0])) < 10 :
maxLen = maxLenOther2 if maxLenOther2 > len(formattedLines[0]) else len(formattedLines[0])
for i, line in enumerate(formattedLines):
if line[-1] == "\\":
surPadding = paddingGen(maxLen - len(line))
formattedLines[i] = line.replace("\\", surPadding+"\\")
gWrapLines.clear()
return formattedLines
def linesReformat(storeLines, index):
formattedLines = []
maxLen = 0
for line in storeLines:
words = line.split()
if maxLen < len(words[index]):
maxLen = len(words[index])
for line in storeLines:
words = line.split()
padding = paddingGen(maxLen-len(words[index]))
prefix = " ".join(words[:index+1])
surfix = " ".join(words[index+1:])
newLine = "%s%s %s" % (prefix, padding, surfix)
newLine = newLine.rstrip()
formattedLines.append(newLine)
return formattedLines
def prePopWraps(f, padding):
if len(gWrapLines) != 0:
newLines = wrapLinesReformat()
for line in newLines:
f.write("%s%s\n" % (padding, line))
return
def storedLinesPopOthers(f, exclude):
for key in gStoreLines:
if key == exclude:
continue
else:
storedLinesPop(f,key)
def storedLinesPop(f, key):
global gStoreLines
index = gFormatBaseIndex[key]
padding = paddingGen(gIndent)
if len(gStoreLines[key]) != 0:
newLines = linesReformat(gStoreLines[key], index)
for line in newLines:
f.write("%s%s\n" % (padding, line))
gStoreLines[key].clear()
return
def lineMark(line):
global cur
# cur = LineAttr()
words = line.split()
# For empty lines
if len(line) == 0:
cur.empty = 1
return
# For comment lines
if line[0] == "#" and line[-1] != "\\":
cur.comment = 1
return
# For reformating the Package Require
if "package require" in line:
cur.package = 1
return
# For reformating the single line SETs
if line[0:4] == "set " and line[-1] != "\\" and line[-1] != "[":
cur.set = 1
return
# For reformating the "configure -"
if len(words) >= 3 and words[0][0] == "$" and words[1] == "configure" and words[2][0] == "-":
cur.structCfg = 1
return
# For reformating the "xxxx -" in the SWITCH{}
if len(words) == 2 and words[1] == "-":
cur.switchKey = 1
return
# For marking the block
if line[0] == "}" and line[-1] == "{":
cur.blockMid = 1
elif line.count("{") > line.count("}"):
cur.blockStart = 1
if "switch" in line:
cur.switch = 1
elif line.count("{") < line.count("}"):
cur.blockEnd = 1
# For Multilines starting with [
if line[-1] == ("["):
cur.blockStart = 1
if line[-1] == ("]") and len(line) == 1:
cur.blockEnd = 1
# For Multilines starting with \
if line[-1] == "\\":
cur.wrap = 1
def linePrint(f,line):
global cur, pre, gEmpty, gIndent, gStoreLines
padding = paddingGen(gIndent)
# Reformat the empty lines
if cur.empty == 1:
gEmpty += 1
if gEmpty != 1:
return
else:
# Print the line at the end
gEmpty = 0
# Reformat the stored lines, then print them out.
key = ""
if cur.set == 1 :
key = "set"
elif cur.package == 1:
key = "pkg"
elif cur.switchKey == 1:
key = "key"
elif cur.structCfg == 1:
key = "cfg"
else:
key = "all"
storedLinesPopOthers(f, key)
if key != "all":
gStoreLines[key].append(line)
return
# Reformat the multilines with \, then print them out.
if cur.wrap == 1:
gWrapLines.append(line)
return
elif pre.wrap == 1 and cur.wrap == 0:
# Handle the last line
gWrapLines.append(line)
status = wrapLinesStatus(gWrapLines)
prePopWraps(f, padding)
if status == "blockEnd" or status == "bracketEnd":
gIndent -= gIndentSize
elif status == "blockStart" or status == "bracketStart":
gIndent += gIndentSize
return
# Recalculate the padding
if cur.blockStart == 1:
f.write("%s%s\n" % (padding,line))
gIndent += gIndentSize
elif cur.blockMid == 1:
gIndent -= gIndentSize
padding = paddingGen(gIndent)
f.write("%s%s\n" % (padding,line))
gIndent += gIndentSize
elif cur.blockEnd == 1:
gIndent -= gIndentSize
padding = paddingGen(gIndent)
f.write("%s%s\n" % (padding,line))
else:
if len(line) == 0:
f.write("\n")
else:
padding = paddingGen(gIndent)
f.write("%s%s\n" % (padding,line))
def tclfmtRun():
global cur, pre, curFile, gEmpty, gIndent, gStoreLines
gEmpty = 0
gIndent = 0
cur = LineAttr()
pre = LineAttr()
gStoreLines = {
"set": [],
"pkg": [],
"cfg": [],
"key": [],
}
target = curFile
saveto = settings.get("save_to")
if saveto != "":
path = os.path.dirname(curFile)
target = path+"/"+saveto
with io.StringIO() as output:
# Open current file for reading
with io.open(curFile, mode="r", encoding="utf-8") as rFid:
for line in rFid:
line = lineTrim(line)
lineMark(line)
linePrint(output, line)
pre = cur
cur = LineAttr()
# Write the formatted lines back
with io.open(target, mode='w', encoding="utf-8", newline='') as wFid:
wFid.write(output.getvalue())
# For Sublime plugin
settings = None
def plugin_loaded():
global settings
settings = sublime.load_settings('Tclfmt.sublime-settings')
def isTclSource(view):
# Return True if the given view contains TCL source code.
return view.score_selector(0, 'source.tcl') != 0
# Sublime
class TclfmtCommand(sublime_plugin.TextCommand):
def is_enabled(self):
return isTclSource(self.view)
def run(self, edit):
tclfmtRun()
class TclfmtListener(sublime_plugin.EventListener):
def on_pre_save(self, view):
if not settings.get('format_on_save', True):
return
sublime.set_timeout(lambda: view.run_command("tclfmt"), 0)
def on_activated(self, view):
global curFile
curFile = view.file_name()