-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
140 lines (116 loc) · 4.69 KB
/
main.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
import re
import sublime
import sublime_plugin
import jieba
"""没有生命,地球的脸面就会失去,变得像月球般木然。
"""
regex = r'[\u4E00-\u9FA5\s]+'
chinese_regex = re.compile(r'[^\x00-\x7F]+')
find_results_regex = re.compile(r'^\s*\d+:')
def expand_word(view, point: int, forward: bool) -> sublime.Region:
region = view.word(point)
if not forward and region.begin() >= point:
# region = region.cover(view.word(point - 1))
region = sublime.Region(view.word(point - 1).begin(), point)
if forward and region.end() <= point:
# region = region.cover(view.word(point + 1))
region = sublime.Region(point, view.word(point + 1).end())
content = view.substr(region)
if chinese_regex.search(content):
words = jieba.cut(content)
start_point = region.begin()
if forward:
for word in words:
end_point = start_point + len(word)
if start_point >= point or end_point > point:
region = sublime.Region(start_point, end_point)
break
start_point = end_point
else:
for word in words:
end_point = start_point + len(word)
if end_point >= point:
region = sublime.Region(end_point, start_point)
break
start_point = end_point
else:
if not forward:
region.a, region.b = region.b, region.a
# print("%s[%s][%s]" % (region, forward, view.substr(region)))
return region
class ChineseTokenizerDeleteWord(sublime_plugin.TextCommand):
def run(self, edit, forward=False):
regions, view = [], self.view
if forward:
for r in view.sel():
region = expand_word(view, r.begin(), True)
if r.empty() and region.a < r.a:
region.a = r.a
regions.append(region)
else:
for r in view.sel():
region = expand_word(view, r.end(), False)
if r.empty() and region.a > r.a:
region.a = r.a
regions.append(region)
for r in reversed(regions):
view.erase(edit, r)
class ChineseTokenizerMove(sublime_plugin.TextCommand):
def run(self, edit, forward=True, extend=False):
regions, view = [], self.view
if forward:
for r in view.sel():
region = expand_word(view, r.b, True)
if not extend:
region = region.end()
else:
region.a = r.a
if region.b <= r.b:
region.b = r.b + 1
regions.append(region)
else:
for r in view.sel():
region = expand_word(view, r.b, False)
if not extend:
region = region.begin()
else:
region.a = r.a
if region.b >= r.b:
region.b = r.b - 1
regions.append(region)
view.sel().clear()
view.sel().add_all(regions)
class ChineseTokenizerAddSelection(sublime_plugin.TextCommand):
selection = sublime.Region(-1, -1)
def run(self, edit):
self.view.sel().add(self.selection)
class ChineseTokenizerListener(sublime_plugin.EventListener):
def on_text_command(self, view, name, args):
if name == 'drag_select' and args.get('by', '') == 'words':
event = args["event"]
point = view.window_to_text((event["x"], event["y"]))
# Skip: Output Panel, Find Results
element = view.element()
if element in ('exec:output', 'output:output'):
return None
if element == 'find_in_files:output':
line = view.line(point)
start_pt = line.begin()
end_pt = min(line.end(), start_pt + 20)
if (view.match_selector(start_pt, 'entity.name.filename') or
find_results_regex.match(
view.substr(sublime.Region(start_pt, end_pt)))):
return None
neaset_region = view.sel()[0]
regions = []
for r in view.sel():
regions.append(r)
if r.empty() and neaset_region.a < r.a and r.a <= point:
neaset_region = r
if neaset_region.empty():
point = neaset_region.a
selection = expand_word(view, point, True)
ChineseTokenizerAddSelection.selection = selection
return ("chinese_tokenizer_add_selection", {})
def plugin_loaded():
sublime.set_timeout_async(jieba.initialize, 0)