-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathunicodecomplete.py
320 lines (265 loc) · 12.2 KB
/
unicodecomplete.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
import sublime
import sublime_plugin
import re
from sys import version
if int(sublime.version()) < 3000:
from mathsymbols import *
else:
from UnicodeMath.mathsymbols import *
PyV3 = version[0] == "3"
UNICODE_SYMBOL_RE = re.compile(r'(?:\\)(?P<symbol>[^\s\\\.,]+)')
UNICODE_RE = re.compile(r'(?:\\)(?:(?P<symbol>[^\s\\\.,]+)|(?:\\(?P<prefix>[^\s\\\.,]+)\\(?P<chars>[^\s\\\.,]+)))')
UNICODE_SYMBOL_PREFIX_RE = re.compile(r'(?:\\)(?P<symbol>[^\\]+)$')
UNICODE_PREFIX_RE = re.compile(r'(?:\\)(?:(?P<symbol>[^\\]+)|(?:\\(?P<prefix>[^\s\\\.,]+)\\(?P<chars>[^\s\\\.,]+ ?)))$')
SYNTAX_RE = re.compile(r'(.*?)/(?P<name>[^/]+)\.(?:tmLanguage|sublime-syntax)')
def log(message):
print(u'UnicodeMath: {0}'.format(message))
def get_line_contents(view, location):
"""
Returns the contents of the line at the given location
"""
return view.substr(sublime.Region(view.line(location).a, location))
def is_script(s):
"""
Subscript _... or superscript ^...
"""
return s.startswith('_') or s.startswith('^')
def get_script(s):
return (s[0], list(s[1:]))
def enabled(name, default=True):
return get_settings().get(name, default)
def replacement(m, instant=False):
"""
Returns the conversion for regex match m (with groups 'symbol', 'prefix'
and 'chars'), None if no conversion is possible.
If instant=True, restricts to conversions suitable for instant insertion
"""
symbol = m.groupdict().get('symbol')
prefix = m.groupdict().get('prefix')
chars = m.groupdict().get('chars')
if symbol is not None:
# Accept explicit symbol names; in instant mode, refuse \^... and \_...
# (which are left to subs/supers) and ambigous prefixes
rep = symbol_by_name(symbol)
if rep and (not instant or (not is_script(symbol) and symbol_by_prefix(symbol, unique=True))):
return rep
# Convert unambiguous prefixes
if enabled('accept_prefixes'):
rep = symbol_by_prefix(symbol, unique=True)
if rep:
return rep
# Convert subscript and superscripts, but not in instant mode (it would
# convert immediately at \^ or \_)
if enabled('convert_sub_super') and is_script(symbol) and (not instant or symbol.endswith(" ")):
script_char, chars = get_script(symbol.strip())
reps = [symbol_by_name(script_char + ch) for ch in chars]
if all(reps):
return ''.join(reps)
# Convert Unicode codes
if enabled('convert_codes'):
rep = symbol_by_code(u'\\' + symbol)
if rep:
return rep
# In instant mode, accept symbols when followed by an invalid character.
# For instance, when typing "x" in "\alphax", recognize that "\alpha"
# was completed, and replace it.
if instant and symbol is not None and len(symbol) > 1 and not is_script(symbol):
prefix, suffix = symbol[:-1], symbol[-1]
rep = symbol_by_name(prefix)
comp_full = symbol_by_prefix(symbol, unique=False)
if rep and not comp_full:
rep = symbol_by_name(prefix)
if rep:
return rep + suffix
# Substitute prefix combinations (\\prefix\...)
if prefix is not None and (not instant or chars and chars.endswith(" ")):
reps = [symbol_by_name(prefix + ch) for ch in chars.strip()]
if all(reps):
return ''.join(reps)
def can_convert(view, instant=False):
"""
Determines if there are any regions, where symbol can be converted
Used not to call command when it will not convert anything, because such call
modified edit, which lead to call of on_modified recursively
Some times (is it sublime bug?) on_modified called twice on every change, which makes
hard to detect whether this on_modified was called as result of previous call of command
If instant=True, only allows conversions suitables for automatic insertion
"""
prefix_re = UNICODE_PREFIX_RE if enabled('convert_list') else UNICODE_SYMBOL_PREFIX_RE
for r in view.sel():
if r.a == r.b:
line = get_line_contents(view, r.a)
m = prefix_re.search(line)
if m and replacement(m, instant) is not None:
return True
return False
def syntax_allowed(view):
"""
Returns whether syntax in view is not in ignore list
"""
syntax_in_view = SYNTAX_RE.match(view.settings().get('syntax'))
if syntax_in_view and syntax_in_view.group('name').lower() in get_settings().get('ignore_syntax', []):
return False
return True
def find_rev(view, r):
# Go through all prefixes starting from longest
# Returns prefix length and its names + possibly code
# Order:
# - name - may not present
# - synonyms... - may not presend
# - code - always present
max_len = max(map(lambda v: len(v), maths.direct.values()))
prefix = get_line_contents(view, r.end())
for i in reversed(range(1, max_len + 1)):
cur_pref = prefix[-i:]
if len(cur_pref) < i:
continue
names = list(map(lambda n: u'\\' + n, names_by_symbol(cur_pref)))
if names or i == 1: # For prefix 1 (one symbol) there always exists code
names.append(u''.join([code_by_symbol(c) for c in cur_pref]))
if names:
region = sublime.Region(r.end() - i, r.end())
return (region, names)
return (r, [])
class UnicodeMathComplete(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
if not syntax_allowed(view):
return
prefix_re = UNICODE_PREFIX_RE if enabled('convert_list') else UNICODE_SYMBOL_PREFIX_RE
line = get_line_contents(view, locations[0])
m = prefix_re.search(line)
if not m:
return
symbol = m.groupdict().get('symbol')
pre = m.groupdict().get('prefix')
chars = m.groupdict().get('chars')
# returns completions
if pre is not None:
def drop_prefix(pr, s):
return s[len(pr):]
pref = '\\\\' + pre + '\\' + ''.join(chars)
completions = [(pref + drop_prefix(pre, k) + '\t' + maths.direct[k], '\\' + pref + drop_prefix(pre, k)) for k in maths.direct.keys() if k.startswith(pre)]
completions.extend([(pref + drop_prefix(pre, k) + '\t' + maths.direct[synonyms.direct[k]], '\\' + pref + drop_prefix(pre, k)) for k in synonyms.direct.keys() if k.startswith(pre)])
else:
completions = [('\\' + k + '\t' + maths.direct[k], maths.direct[k]) for k in maths.direct.keys() if k.startswith(symbol)]
completions.extend([('\\' + k + '\t' + maths.direct[synonyms.direct[k]], maths.direct[synonyms.direct[k]]) for k in synonyms.direct.keys() if k.startswith(symbol)])
return sorted(completions, key=lambda k: k[0])
def on_query_context(self, view, key, operator, operand, match_all):
if key == 'unicode_math_syntax_allowed':
return syntax_allowed(view)
elif key == 'unicode_math_can_convert':
return can_convert(view)
elif key == 'unicode_math_convert_on_space_enabled':
return enabled('convert_on_space')
else:
return False
class UnicodeMathConvert(sublime_plugin.TextCommand):
def run(self, edit, instant=False):
self.prefix_re = UNICODE_PREFIX_RE if enabled('convert_list') else UNICODE_SYMBOL_PREFIX_RE
self.search_re = UNICODE_RE if enabled('convert_list') else UNICODE_SYMBOL_RE
for r in self.view.sel():
if r.a == r.b:
self.convert_prefix(edit, r, instant)
else:
self.convert_selection(edit, r, instant)
def convert_prefix(self, edit, r, instant):
line = get_line_contents(self.view, r.a)
m = self.prefix_re.search(line)
if m:
rep = replacement(m, instant)
if rep is not None:
if enabled('trailing_space'):
rep += " "
self.view.replace(edit, sublime.Region(r.begin() - (m.end() - m.start()), r.begin()), rep)
def convert_selection(self, edit, r, instant):
contents = self.view.substr(r)
replaces = []
# collect replacements as pairs (region, string to replace with)
for m in self.search_re.finditer(contents):
rep = replacement(m, instant)
if rep is not None:
replaces.append((sublime.Region(r.begin() + m.start(), r.begin() + m.end()), rep))
# apply all replacements
offset = 0
for reg, rep in replaces:
self.view.replace(edit, sublime.Region(reg.begin() + offset, reg.end() + offset), rep)
offset += len(rep) - reg.size()
class UnicodeMathConvertInstantly(sublime_plugin.TextChangeListener):
def __init__(self):
super().__init__()
self.running = False
def on_text_changed(self, changes):
# Sublime only marks changes as processed once we return. But we run 'unicode_math_convert'
# which causes text changes; as a result, Sublime will re-invoke this listener before we
# return, and re-send the same changes since they're not technically "processed" yet. To
# avoid confusion and infinite loops, disable all the recursive calls.
if self.running:
return
# only convert when adding text (length of old content == 0)
if enabled('convert_instantly') and any(c.len_utf8 == 0 for c in changes):
view = sublime.active_window().active_view()
if view is not None and syntax_allowed(view):
if can_convert(view, instant=True):
self.running = True
view.run_command('unicode_math_convert', args={'instant': True})
self.running = False
class UnicodeMathConvertBack(sublime_plugin.TextCommand):
"""
Convert symbols back to either name or code
"""
def run(self, edit, code=False):
if len(self.view.sel()) == 1:
(region, names) = find_rev(self.view, self.view.sel()[0])
if code:
self.view.replace(edit, region, names[-1])
else:
if len(names) <= 2: # name or name + code
self.view.replace(edit, region, names[0])
else:
self.region = region
self.names = names
self.view.window().show_quick_panel(self.names[:-1], self.on_done)
else:
for r in self.view.sel():
(region, names) = find_rev(self.view, r)
if names:
self.view.replace(edit, region, names[-1] if code else names[0])
def on_done(self, idx):
if idx == -1:
return
self.view.run_command('unicode_math_replace_in_view', {
'replace_with': self.names[idx],
'begin': self.region.begin(),
'end': self.region.end()})
class UnicodeMathInsertSpace(sublime_plugin.TextCommand):
def run(self, edit):
for r in self.view.sel():
self.view.insert(edit, r.a, " ")
class UnicodeMathReplaceInView(sublime_plugin.TextCommand):
def run(self, edit, replace_with=None, begin=None, end=None):
if not replace_with:
return
if begin is not None and end is not None: # Excplicit region
self.view.replace(edit, sublime.Region(int(begin), int(end)), replace_with)
else:
for r in self.view.sel():
self.view.replace(edit, r, replace_with)
class UnicodeMathInsert(sublime_plugin.WindowCommand):
def run(self):
self.menu_items = []
self.symbols = []
for k, v in maths.direct.items():
value = v + ' ' + k
if k in synonyms.inverse:
value += ' ' + ' '.join(synonyms.inverse[k])
self.menu_items.append(value)
self.symbols.append(v)
self.window.show_quick_panel(self.menu_items, self.on_done)
def on_done(self, idx):
if idx == -1:
return
view = self.window.active_view()
if not view:
return
view.run_command('unicode_math_replace_in_view', {
'replace_with': self.symbols[idx]})