This repository was archived by the owner on Jun 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommands.py
More file actions
75 lines (60 loc) · 1.92 KB
/
Commands.py
File metadata and controls
75 lines (60 loc) · 1.92 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
import sublime
import sublime_plugin
import unicodedata
def visual_width(view, pt, tabsize):
width = 0
while pt>0 and view.rowcol(pt)[1]>0:
pt -= 1
c = view.substr(pt)
if c == "\t":
width += tabsize
elif ord(c) < 255:
width += 1
else:
c = unicodedata.east_asian_width(c)
width += c in ("F", "W") and 2 or 1
return width
class ColAlignCommand(sublime_plugin.TextCommand):
def run(self, edit, follow="\n ,([{:"):
view = self.view
origin = []
maxwidth = 0
offset = 0
tabSize = view.settings().get('tab_size')
regions = view.sel()
for region in view.sel():
lines = view.lines(region)
if len(lines)!=1:
view.run_command("indent")
continue
pos = region.begin()
while view.substr(pos) == " ": pos+=1
while pos > 0:
if view.substr(pos - 1) in follow:
break
pos -= 1
col = view.rowcol(pos)[1]
if col == 0 or view.substr(pos-1)=="\t":
origin.append((pos, -1))
continue
width = visual_width(view, pos, tabSize)
origin.append((pos, width))
if maxwidth < width:
maxwidth = width + width % 2
origin.sort()
count = 0
for (pos, width) in origin:
if(width == -1):
view.insert(edit, pos+offset, "\t")
offset+=1
continue
width = maxwidth - width
if width>0:
view.insert(edit, pos+offset, " " * width)
count+=1
offset+=width
if count == 0:
for (pos, width) in origin:
if(width != -1):
view.insert(edit, pos+offset, " ")
offset+=1