-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfalign.py
More file actions
199 lines (173 loc) · 6.03 KB
/
Copy pathfalign.py
File metadata and controls
199 lines (173 loc) · 6.03 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
# coding=utf-8
import sublime
import sublime_plugin
import re
def Settings():
return sublime.load_settings("FAlign.sublime-settings")
class FalignCommand(sublime_plugin.TextCommand):
def get_indent_text(self, view, indent):
if int(view.settings().get("translate_tabs_to_spaces", False)):
return ' '*(int(self.tab_size)*indent)
else:
return '\t'*indent
def get_line_feature(self, line_string):
space_count = 0
index = 0
for c in line_string:
if c == " ":
space_count += 1
elif c == "\t":
space_count += self.tab_size
else:
line_string = line_string[index:]
break
index += 1
# 缩进层级
indent_level = int(space_count/self.tab_size)
# 替换行中的 tab
line_string = line_string.replace("\t", " "*self.tab_size)
# 关键字列表
key_list = []
pos = 0
while True:
match = self.alignment_chars_pattern.search(line_string, pos)
if not match : break
pos = match.end()
key_list.append({"key":match.group(),"pos":pos})
return indent_level, key_list if len(key_list) > 0 else None, line_string
def get_key_word(self, key):
return (" " if self.fa_alignment_chars[key]["left_space"] else "") + key + (" " if self.fa_alignment_chars[key]["right_space"] else "")
def get_line_text(self, view, index):
return view.substr(view.line(view.text_point(index, 0)))
def run(self, edit):
view = self.view
selection = view.sel()
self.tab_size = int(view.settings().get("tab_size", 8))
# 展开配置
fa_alignment_chars = Settings().get("fa_alignment_chars",[])
self.fa_alignment_chars = {}
for v in fa_alignment_chars:
for vv in v["prefixes"]:
self.fa_alignment_chars[vv] = {
"alignment": v["alignment"],
"left_space": v["left_space"],
"right_space": v["right_space"],
}
# 编译匹配正则
words = []
pattern = re.compile(r"\w")
for v in self.fa_alignment_chars:
if pattern.search(v):
words.append('(?<=\W)'+v+'(?=\W)')
else:
words.append(v)
self.alignment_chars_pattern = re.compile(r"({0})".format("|".join(words)))
# 当前行的缩进,内容
main_row = view.rowcol(view.lines(selection[0])[0].a)[0]
main_indent_level, main_keys, main_string = self.get_line_feature(self.get_line_text(view, main_row))
if not main_keys:
return
# 相似行的数据
smiller_lines_data ={}
line_count, _ = view.rowcol(view.size())
for direction in [-1,1]:
row_index = main_row + direction
while row_index >= 0 and row_index < line_count + 1:
indent, keys, string = self.get_line_feature(self.get_line_text(view, row_index))
if indent != main_indent_level :
break
if not keys or keys[0]["key"] != main_keys[0]["key"]:
break
smiller_lines_data[row_index] = {"text":string, "keyword":keys}
row_index += direction
if not smiller_lines_data:
return
else:
smiller_lines_data[main_row] = {"text":main_string, "keyword":main_keys}
# 删除已经对齐的keyword
while True:
is_same_pos = True
for row_id in smiller_lines_data:
row_data = smiller_lines_data[row_id]
if row_data:
if row_data["keyword"][0]["key"] != main_keys[0]["key"]:
smiller_lines_data[row_id] = False
elif row_data["keyword"][0]["pos"] != main_keys[0]["pos"]:
is_same_pos = False
if is_same_pos:
for row_id in smiller_lines_data:
row_data = smiller_lines_data[row_id]
if row_data:
del row_data["keyword"][0]
if len(row_data["keyword"]) <= 0:
smiller_lines_data[row_id] = False
if not smiller_lines_data[main_row]:
return
else:
break
# 重新整理这些行
keyword = smiller_lines_data[main_row]["keyword"][0]["key"]
align_keyword = self.get_key_word(keyword)
row_region = []
new_smiller_lines_data = {}
for direction in [-1,1]:
row_id = main_row + direction
while True:
if not row_id in smiller_lines_data:
break
row_data = smiller_lines_data[row_id]
if row_data:
new_smiller_lines_data[row_id] = {"text":row_data["text"], "pos":row_data["keyword"][0]["pos"]}
else:
break
row_id += direction
row_region.append(row_id+(-direction))
if len(new_smiller_lines_data) <= 0:
return
else:
new_smiller_lines_data[main_row] = {
"text":smiller_lines_data[main_row]["text"],
"pos":smiller_lines_data[main_row]["keyword"][0]["pos"]
}
# 去除关键字周围的空白
for row_id in new_smiller_lines_data:
text = new_smiller_lines_data[row_id]["text"]
pos = new_smiller_lines_data[row_id]["pos"]
region = [0,len(text)]
index = 0
for ids in [range(pos-len(keyword)-1,0,-1),range(pos,len(text))]:
for i in ids:
if text[i] != " ":
region[index] = i
break
index = index + 1
text = text[:region[0]+1]+align_keyword+text[region[1]:]
pos = region[0] + len(align_keyword) +1
new_smiller_lines_data[row_id] = {"text":text, "pos":pos}
# 对齐行
pos_max = 0
for row_index in new_smiller_lines_data:
pos_max = max(pos_max, new_smiller_lines_data[row_index]["pos"])
for row_index in new_smiller_lines_data:
line = new_smiller_lines_data[row_index]["text"]
pos = new_smiller_lines_data[row_index]["pos"]
dis = pos_max - pos
if dis != 0:
if self.fa_alignment_chars[keyword]["alignment"] == "left":
new_smiller_lines_data[row_index]["text"] = line[:pos] + " "*(dis) + line[pos:]
else:
new_smiller_lines_data[row_index]["text"] = line[:pos-len(align_keyword)] + " "*(dis) + line[pos-len(align_keyword):]
# 拼接行
aligned_lines = [""]
for row in range(row_region[0],row_region[1]):
aligned_lines.append(new_smiller_lines_data[row]["text"]+"\n")
aligned_lines.append(new_smiller_lines_data[row_region[1]]["text"])
# 替换文本
view.replace(
edit,
sublime.Region(
view.text_point(row_region[0],0),
view.line(view.text_point(row_region[1],0)).b,
),
self.get_indent_text(view, main_indent_level).join(aligned_lines)
)