forked from SublimeText/LaTeXTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
latex_fill_all.py
1200 lines (1003 loc) · 39.5 KB
/
latex_fill_all.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import print_function
import sublime
import sublime_plugin
import re
import sys
import traceback
if sublime.version() < '3000':
# we are on ST2 and Python 2.X
_ST3 = False
from getRegion import getRegion
from latextools_plugin import (
get_plugins_by_type, _classname_to_internal_name
)
from latextools_utils import get_setting
from latextools_utils.internal_types import FillAllHelper
exec("""def reraise(tp, value, tb=None):
raise tp, value, tb
""")
strbase = basestring
else:
_ST3 = True
# hack to ensure relative package imports work
__package__ = 'LaTeXTools'
from .getRegion import getRegion
from .latextools_plugin import (
get_plugins_by_type, _classname_to_internal_name
)
from .latextools_utils import get_setting
from .latextools_utils.internal_types import FillAllHelper
def reraise(tp, value, tb=None):
if value is None:
value = tp()
if value.__traceback__ is not tb:
raise value.with_traceback(tb)
raise value
strbase = str
long = int
class LatexFillHelper(object):
'''
Base class for some LaTeXTools TextCommands. Implements several methods
helpful for inserting text into the view and updating the cursor posiiton.
'''
# This is necessarily incomplete, but is intended to cover a number of
# cases and could be extended as needed. I'm unsure that this is the best
# design; it's done this way to emulate STs default definition of
# word_separators
#
# defines non-word characters; see get_current_word
NON_WORD_CHARACTERS = u'/\\()"\':,.;<>~!@#$%^&*|+=\\[\\]{}`~?\\s'
WORD_SEPARATOR_RX = re.compile(
r'([^' + NON_WORD_CHARACTERS + r']*)',
re.UNICODE
)
# define fancy match prefix to support, e.g., \cite_prefix
FANCY_PREFIX_RX = re.compile(
r'([^_' + NON_WORD_CHARACTERS + r']*)_',
re.UNICODE
)
# defines which characters need a matching bracket and their match
MATCH_CHARS = {
'{': '}',
'[': ']',
'(': ')'
}
def complete_auto_match(self, view, edit, insert_char):
'''
Completes brackets if auto_match is enabled; also implements the
"smart_bracket_auto_trigger" logic, which tries to complete the nearest
open bracket intelligently.
:param view:
the current view
:param edit:
the current edit
:param insert_char:
the character to try to automatch
'''
if sublime.load_settings('Preferences.sublime-settings').get(
'auto_match_enabled', True
):
# simple case: we have an insert char, insert closing char,
# if its defined
if insert_char:
self.insert_at_end(
view, edit, self.get_match_char(insert_char)
)
# if the insert_char is a bracket, move cursor to middle of
# bracket and return
if insert_char in self.MATCH_CHARS:
new_regions = []
for sel in view.sel():
if not sel.empty():
new_regions.append(sel)
else:
new_point = sel.end() - 1
new_regions.append(getRegion(new_point, new_point))
self.update_selections(view, new_regions)
return
elif get_setting('smart_bracket_auto_trigger', True):
# more complex: if we do not have an insert_char, try to close
# the nearest bracket that occurs before each selection
new_regions = []
for sel in view.sel():
word_region = self.get_current_word(view, sel)
close_bracket = self.get_closing_bracket(view, word_region)
# we should close the bracket
if close_bracket:
# insert the closing bracket
view.insert(edit, word_region.end(), close_bracket)
if sel.empty():
if word_region.empty():
new_regions.append(
getRegion(
word_region.end(), word_region.end()))
else:
new_point = word_region.end() + len(
close_bracket)
new_regions.append(getRegion(
new_point, new_point))
else:
new_regions.append(
getRegion(
sel.begin(),
word_region.end() + len(close_bracket)))
else:
new_regions.append(sel)
self.update_selections(view, new_regions)
def complete_brackets(self, view, edit, insert_char='', remove_regions=[]):
'''
Intended to be called from a TextCommand to insert a specified
insert_char, close the nearest bracket, and remove any regions
specified
:param view:
the current view
:param edit:
the current edit
:param insert_char:
the character to insert and possibly automatch
:param remove_regions:
any regions to be removed from the current view
'''
self.insert_at_end(view, edit, insert_char)
self.complete_auto_match(view, edit, insert_char)
self.remove_regions(view, edit, remove_regions)
self.clear_bracket_cache()
def get_closing_bracket(self, view, sel):
'''
Determines if the nearest bracket that occurs before the given
selection is closed. If the bracket should be closed, returns the
closing bracket to use.
Note that this will not work if the arguments to the command span
multiple lines, but we generally don't support that anyway.
:param view:
the current view
:param sel:
a sublime.Region indicating the selected area
'''
# candidates stores matched bracket-pairs so that we only have
# to find all matches once per bracket type
# if the view has changed, we reset the candidates
candidates = None
if not hasattr(self, 'last_view') or self.last_view != view.id():
self.last_view = view.id()
self.use_full_scan = get_setting(
'smart_bracket_scan_full_document', False)
candidates = self.candidates = {}
if not self.use_full_scan:
# always clear the candidates when not using a full scan
candidates = {}
# when not using a full scan, get the number of lines to
# look-behind
try:
look_around = int(get_setting('smart_bracket_look_around', 5))
except ValueError:
look_around = 5
if candidates is None:
try:
candidates = self.candidates
except:
candidates = self.candidates = {}
# first, find the nearest bracket
if type(sel) is sublime.Region:
start, end = sel.begin(), sel.end()
else:
start = end = sel
if self.use_full_scan:
prefix = view.substr(getRegion(0, start))
prefix_start = 0
suffix_end = view.size()
else:
prefix_lines = view.lines(getRegion(0, start))
if len(prefix_lines) >= look_around:
prefix_start = prefix_lines[-look_around].begin()
else:
prefix_start = prefix_lines[0].begin()
suffix_lines = view.lines(getRegion(end, view.size()))
if len(suffix_lines) >= look_around:
suffix_end = suffix_lines[look_around - 1].end()
else:
suffix_end = suffix_lines[-1].end()
prefix = view.substr(getRegion(prefix_start, start))
open_bracket, last_index = None, -1
for char in self.MATCH_CHARS:
index = prefix.rfind(char)
if index > last_index:
open_bracket, last_index = char, index
if last_index == -1:
# can't determine bracket to match
return None
close_bracket = self.MATCH_CHARS[open_bracket]
open_brackets = []
# this is used to throw-away as many matches as possible
# so subsequent requests don't need to consider every match
closed_brackets = []
if open_bracket not in candidates:
# find all open / close brackets in the current buffer,
# removing all comments
candidates[open_bracket] = results = []
start = prefix_start
re_str = re.escape(open_bracket) + '|' + re.escape(close_bracket)
while True:
if start >= suffix_end:
break
c = view.find(re_str, start)
if c is None or c.begin() == -1:
break
if c.end() > suffix_end:
break
if view.score_selector(c.begin(), 'comment') != 0:
start = c.end()
continue
results.append(c)
start = c.end()
for candidate in candidates[open_bracket]:
if view.substr(candidate) == open_bracket:
if len(open_brackets) == 0 and candidate.begin() > end:
break
open_brackets.append(candidate)
else:
try:
removed = open_brackets.pop()
if candidate.end() < start:
closed_brackets.append(removed)
closed_brackets.append(candidate)
except IndexError:
# unbalanced close before open
if candidate.end() > end:
break
if len(closed_brackets) > 0:
candidates[open_bracket] = [
c for c in candidates[open_bracket]
if c not in closed_brackets
]
# if we have an open bracket left, then the current bracket needs to
# be closed
return close_bracket if len(open_brackets) > 0 else None
def clear_bracket_cache(self):
'''
Clears the cache of brackets stored by get_closing_bracket
If get_closing_bracket is used, this method must be called at the end
or else subsequent calls to get_closing_brackets will not be updated
with a fresh view of the current buffer
'''
try:
del self.candidates
except:
pass
try:
del self.last_view
except:
pass
try:
del self.use_full_scan
except:
pass
def get_common_prefix(self, view, locations):
'''
gets the common prefix (if any) from a list of locations
:param view:
the current view
:param locations:
either a list of points or a list of sublime.Regions
'''
if type(locations[0]) is int or type(locations[0]) is long:
locations = [getRegion(l, l) for l in locations]
old_prefix = None
for location in locations:
if location.empty():
word_region = getRegion(
self.get_current_word(view, location).begin(),
location.b
)
prefix = view.substr(word_region)
else:
prefix = view.substr(location)
if old_prefix is None:
old_prefix = prefix
elif old_prefix != prefix:
prefix = ''
break
return prefix
def get_common_fancy_prefix(self, view, locations):
'''
get the common fancy prefix (if any) from a list of locations
see get_fancy_prefix for the definition of a fancy prefix
:param view:
the current view
:param locations:
either a list of points or a list of sublime.Regions
'''
remove_regions = []
old_prefix = None
new_prefix = ''
for location in locations:
prefix_region = self.get_fancy_prefix(view, location)
if prefix_region.empty():
continue
new_prefix = view.substr(
getRegion(
prefix_region.begin() + 1, prefix_region.end()
)
)
remove_regions.append(prefix_region)
if old_prefix is None:
old_prefix = new_prefix
elif old_prefix != new_prefix:
# dummy value that is not None and will never match the
# prefix
old_prefix = True
new_prefix = ''
return new_prefix, remove_regions
def get_current_word(self, view, location):
'''
Gets the region containing the current word which contains the caret
or the given selection.
The current word is defined between the nearest non-word characters to
the left and to the right of the current selected location.
Non-word characters are defined by the WORD_SEPARATOR_RX.
:param view:
the current view
:param location:
either a point or a sublime.Region that defines the caret position
or current selection
'''
if isinstance(location, sublime.Region):
start, end = location.begin(), location.end()
else:
start = end = location
start_line = view.line(start)
end_line = view.line(end)
# inverse prefix so we search from the right-hand side
line_prefix = view.substr(getRegion(start_line.begin(), start))[::-1]
line_suffix = view.substr(getRegion(end, end_line.end()))
# prefix is the characters before caret
m = self.WORD_SEPARATOR_RX.search(line_prefix)
prefix = m.group(1) if m else ''
m = self.WORD_SEPARATOR_RX.search(line_suffix)
suffix = m.group(1) if m else ''
return getRegion(
start - len(prefix), end + len(suffix)
)
def get_match_char(self, insert_char):
return self.MATCH_CHARS.get(insert_char, '')
def get_fancy_prefix(self, view, location):
'''
Gets the prefix for the command assuming it takes a form like:
\cite_prefix
\ref_prefix
These are also supported:
\cite_prefix{
\ref_prefix{
The prefix is defined by everything *after* the underscore
:param view:
the current view
:param location:
either a point or a sublime.Region that defines the caret position
or current selection
'''
if isinstance(location, sublime.Region):
start = location.begin()
else:
start = location
start_line = view.line(start)
# inverse prefix so we search from the right-hand side
line_prefix = view.substr(getRegion(start_line.begin(), start))[::-1]
m = self.FANCY_PREFIX_RX.match(line_prefix)
if not m:
return getRegion(start, start)
return getRegion(
start - len(m.group(0)),
start - m.start()
)
def insert_at_end(self, view, edit, value):
'''
Inserts a string at the end of every current selection
:param view:
the current view
:param edit:
the current edit
:param value:
the string to insert
'''
if value:
new_regions = []
for sel in view.sel():
view.insert(edit, sel.end(), value)
if sel.empty():
new_start = new_end = sel.end() + len(value)
else:
new_start = sel.begin()
new_end = sel.end() + len(value)
new_regions.append(getRegion(new_start, new_end))
self.update_selections(view, new_regions)
def replace_word(self, view, edit, value):
'''
Replaces the current word with the provided string in each selection
For the definition of word, see get_current_word()
:param view:
the current view
:param edit:
the current edit
:param value:
the string to replace the current word with
'''
new_regions = []
for sel in view.sel():
if sel.empty():
word_region = self.get_current_word(view, sel.end())
start_point = word_region.begin()
end_point = word_region.end()
else:
word_region = self.get_current_word(view, sel)
start_point = word_region.begin()
end_point = word_region.end()
view.replace(
edit, getRegion(start_point, end_point),
value
)
if sel.empty():
start_point = end_point = start_point + len(value)
else:
end_point = start_point + len(value)
new_regions.append(getRegion(start_point, end_point))
self.update_selections(view, new_regions)
def remove_regions(self, view, edit, regions):
view = self.view
for region in regions:
view.erase(edit, region)
def update_selections(self, view, new_regions):
'''
Removes all current selections and adds the specified selections
NB When calling this method, it is important that all current
selections be either replaced or simply included as-is. Otherwise,
these selections will be lost
:param view:
the current view
:param new_regions:
a list of sublime.Regions that should be selected
'''
sel = view.sel()
sel.clear()
# we could use ST3's add_all, but this way has less branching...
for region in new_regions:
sel.add(region)
def regions_to_tuples(self, regions):
'''
Converts a list of regions to a list of two-element tuples containing
the corresponding points
This is necessary to properly serialize a set of regions as an argument
to a Sublime command, since arguments MUST be serializable as JSON
objects
:param regions:
an iterable of sublime.Regions to convert to tuples
'''
# a and b are used instead of begin() / end() so that the caret
# position (b) is preserved
if type(regions) == sublime.Region:
return [(regions.a, regions.b)]
return [
[r.a, r.b]
for r in regions
]
def tuples_to_regions(self, tuples):
'''
Converts a list of 2-tuples to a list of corresponding regions
This is the opposite of regions_to_tuples and is intended to
deserialize regions serialized using that method
:param tuples:
an iterable of two-element tuples to convert to sublime.Regions
'''
if type(tuples) == tuple:
return [getRegion(tuples[0], tuples[1])]
return [
getRegion(start, end)
for start, end in tuples
]
def score_selector(self, view, selector):
'''
Scores a selector on a view, returns True if the selectors is
scored for each selection.
:param view:
the current view
:param selector:
the selector, which should be scored
'''
return all(view.score_selector(sel.b, selector) for sel in view.sel())
class LatexFillAllPluginConsumer(object):
'''
Base class for classes which use FillAllHelper plugins
'''
COMPLETION_TYPE_NAMES = []
COMPLETION_TYPES = None
def _load_plugins(self):
'''
Loads the FillAllHelper plugins
'''
self.COMPLETION_TYPES = {}
for plugin in get_plugins_by_type(FillAllHelper):
name = _classname_to_internal_name(plugin.__name__)
if name.endswith('_fill_all_helper'):
name = name[:-16]
self.COMPLETION_TYPES[name] = plugin()
self.COMPLETION_TYPE_NAMES = list(self.COMPLETION_TYPES.keys())
def get_completion_types(self):
'''
Gets the list of plugin names
'''
if self.COMPLETION_TYPES is None:
self._load_plugins()
return self.COMPLETION_TYPE_NAMES
def get_completion_type(self, name):
if self.COMPLETION_TYPES is None:
self._load_plugins()
return self.COMPLETION_TYPES.get(name)
class LatexFillAllEventListener(
sublime_plugin.EventListener, LatexFillHelper, LatexFillAllPluginConsumer
):
'''
Implements the query completions and query context functionality for some
completions and the logic to insert brackets as necessary
'''
# keys supported by on_query_context
SUPPORTED_KEYS = None
SUPPORTED_INSERT_CHARS = {
'open_curly': '{',
'open_square': '[',
'comma': ',',
'equal_sign': '='
}
def on_query_context(self, view, key, operator, operand, match_all):
'''
supports query_context for all completion types
key is "lt_fill_all_{name}" where name is the short name of the
completion type, e.g. "lt_fill_all_cite", etc.
'''
# quick exit conditions
if not key.startswith("lt_fill_all_"):
return None
for sel in view.sel():
point = sel.b
if not view.score_selector(point, "text.tex.latex"):
return None
# load the plugins
if self.SUPPORTED_KEYS is None:
self.SUPPORTED_KEYS = dict(
("lt_fill_all_{0}".format(name), name)
for name in self.get_completion_types()
)
try:
key, insert_char = key.split('.')
except:
insert_char = ''
# not handled here
if key not in self.SUPPORTED_KEYS:
return None
# unsupported bracket
elif insert_char and insert_char not in self.SUPPORTED_INSERT_CHARS:
return False
# unsupported operators
elif operator not in [sublime.OP_EQUAL, sublime.OP_NOT_EQUAL]:
return False
insert_char = self.SUPPORTED_INSERT_CHARS.get(insert_char, '')
completion_type = self.get_completion_type(
self.SUPPORTED_KEYS.get(key)
)
if not(completion_type and completion_type.is_enabled()):
return False
selector = completion_type.get_supported_scope_selector()
if not self.score_selector(view, selector):
return False
lines = [
insert_char + view.substr(
getRegion(view.line(sel).begin(), sel.b)
)[::-1]
for sel in view.sel()
]
func = all if match_all else any
result = func((
completion_type.matches_line(line)
for line in lines
))
return result if operator == sublime.OP_EQUAL else not result
def on_query_completions(self, view, prefix, locations):
for location in locations:
if not view.score_selector(location, "text.tex.latex"):
return
completion_types = self.get_completion_types()
orig_prefix = prefix
# tracks any regions to be removed
fancy_prefix, remove_regions = self.get_common_fancy_prefix(
view, locations
)
# although a prefix is passed in, our redefinition of "word" boundaries
# mean we should recalculate this
prefix = self.get_common_prefix(view, locations)
fancy_prefixed_line = None
if remove_regions:
if remove_regions:
fancy_prefixed_line = view.substr(
getRegion(view.line(locations[0]).begin(), locations[0])
)[::-1]
line = view.substr(
getRegion(view.line(locations[0]).begin(), locations[0])
)[::-1]
completion_type = None
for name in completion_types:
ct = self.get_completion_type(name)
if (
fancy_prefixed_line is not None and
hasattr(ct, 'matches_fancy_prefix')
):
if ct.matches_fancy_prefix(fancy_prefixed_line):
line = fancy_prefixed_line
prefix = fancy_prefix
completion_type = ct
break
elif ct.matches_line(line):
completion_type = ct
remove_regions = []
break
elif ct.matches_line(line):
completion_type = ct
# reset fancy prefix
remove_regions = []
break
if completion_type is None:
self.clear_bracket_cache()
return []
elif not self.score_selector(
view, completion_type.get_supported_scope_selector()):
self.clear_bracket_cache()
return []
# completions could be unpredictable if we've changed the prefix
elif orig_prefix and not prefix:
self.clear_bracket_cache()
return []
try:
completions = completion_type.get_auto_completions(
view, prefix, line[::-1]
)
except:
traceback.print_exc()
self.clear_bracket_cache()
return []
if len(completions) == 0:
self.clear_bracket_cache()
return []
elif type(completions) is tuple and len(completions) == 2:
completions, insert_char = completions
if len(completions) == 0:
self.clear_bracket_cache()
return []
else:
# this assumes that all regions have a similar current word
# not ideal, but reasonably safe see:
# http://docs.sublimetext.info/en/latest/extensibility/completions.html#completions-with-multiple-cursors
insert_char = view.substr(
self.get_current_word(view, locations[0])
)
# we found a _<prefix> entry, so clear it and remove the prefix
# and close the brackets
if remove_regions:
view.run_command(
'latex_tools_fill_all_complete_bracket',
{
'insert_char': insert_char,
'remove_regions': self.regions_to_tuples(remove_regions)
}
)
if type(completions[0]) is tuple:
show, completions = zip(*completions)
else:
show = completions[:]
# we did not find a fancy prefix, so append the closing bracket,
# if needed, to the completions
if not remove_regions:
closing_bracket = None
for sel in view.sel():
new_bracket = self.get_closing_bracket(view, sel)
if closing_bracket is None:
closing_bracket = new_bracket
elif new_bracket != closing_bracket:
closing_bracket = None
break
if closing_bracket:
completions = [
c + closing_bracket
for c in completions
]
self.clear_bracket_cache()
return (
zip(show, completions),
sublime.INHIBIT_WORD_COMPLETIONS |
sublime.INHIBIT_EXPLICIT_COMPLETIONS
)
class LatexFillAllCommand(
sublime_plugin.TextCommand, LatexFillHelper, LatexFillAllPluginConsumer
):
'''
Implements the quick panel for auto-triggered completions and the
logic to insert brackets as necessary
:param edit:
the current edit
:param completion_type:
the completion plugin to use (optional)
may be:
* a string indicating the specific completion type, e.g. "cite"
* a list of such strings
* None, in which case all available completion types are searched
:param insert_char:
the character to insert before the completion; also determines the
matching brace if any
:param overwrite:
boolean indicating whether or not to overwrite the current field;
if false, text within the current selection or to the left of the
cursor is treated as the prefix, which usually restricts the
displayed results;
if true, the current word will be replaced by the selected entry
:param force:
boolean indicating whether or not to match the context or simply
insert an entry; if force is true, completion_type must be a string;
if force is true, the bracket matching and word overwriting behaviour
is disabled
'''
def run(
self, edit, completion_type=None, insert_char="", overwrite=False,
force=False
):
view = self.view
for sel in view.sel():
point = sel.b
if not view.score_selector(point, "text.tex.latex"):
self.complete_brackets(view, edit, insert_char)
return
# if completion_type is a simple string, try to load it
if isinstance(completion_type, strbase):
completion_type = self.get_completion_type(completion_type)
if completion_type is None:
if not force:
self.complete_brackets(view, edit, insert_char)
return
elif force:
print('Cannot set `force` if completion type is not specified')
return
if force:
insert_char = ''
overwrite = False
# tracks any regions to be removed
remove_regions = []
prefix = ''
# handle the _ prefix, if necessary
if (
not isinstance(completion_type, FillAllHelper) or
hasattr(completion_type, 'matches_fancy_prefix')
):
fancy_prefix, remove_regions = self.get_common_fancy_prefix(
view, view.sel()
)
# if we found a _ prefix, we use the raw line, so \ref_eq
fancy_prefixed_line = None
if remove_regions:
fancy_prefixed_line = view.substr(
getRegion(view.line(point).begin(), point)
)[::-1]
# normal line calculation
line = (view.substr(
getRegion(view.line(point).begin(), point)
) + insert_char)[::-1]
# handle a list of completion types
if type(completion_type) is list:
for name in completion_type:
try:
ct = self.get_completion_type(name)
if (
fancy_prefixed_line is not None and
hasattr(ct, 'matches_fancy_prefix')
):
if ct.matches_fancy_prefix(fancy_prefixed_line):
completion_type = ct
prefix = fancy_prefix
break
elif ct.matches_line(line):
completion_type = ct
remove_regions = []
break
elif ct.matches_line(line):
completion_type = ct
remove_regions = []
break
except:
pass
if type(completion_type) is list:
message = "No valid completions found"
print(message)
sublime.status_message(message)
self.complete_brackets(view, edit, insert_char)
return
# unknown completion type
elif (
completion_type is None or
not isinstance(completion_type, FillAllHelper)
):
for name in self.get_completion_types():
ct = self.get_completion_type(name)
if ct is None:
continue
if (
fancy_prefixed_line is not None and