-
Notifications
You must be signed in to change notification settings - Fork 367
/
latex_cwl_completions.py
548 lines (446 loc) · 17.5 KB
/
latex_cwl_completions.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
# -*- coding:utf-8 -*-
import os
import re
import threading
from sys import exc_info
import sublime
import sublime_plugin
from .latex_cite_completions import (
OLD_STYLE_CITE_REGEX, NEW_STYLE_CITE_REGEX, match
)
from .latex_ref_completions import (
OLD_STYLE_REF_REGEX, NEW_STYLE_REF_REGEX
)
from . import latex_input_completions
from .latex_own_command_completions import (
get_own_command_completion, get_own_env_completion
)
from .getTeXRoot import get_tex_root
from .latextools_utils import get_setting, analysis, utils
from .latextools_utils.parser_utils import command_to_snippet
__all__ = ['get_cwl_completions', 'is_cwl_available']
# Do not do completions in these environments
ENV_DONOT_AUTO_COM = [
OLD_STYLE_CITE_REGEX,
NEW_STYLE_CITE_REGEX,
OLD_STYLE_REF_REGEX,
NEW_STYLE_REF_REGEX
]
# whether the leading backslash is escaped
ESCAPE_REGEX = re.compile(r"\w*(\\\\)+([^\\]|$)")
# regex to detect that the cursor is predecended by a \begin{
BEGIN_END_BEFORE_REGEX = re.compile(
r"([^{}\[\]]*)\{"
r"(?:\][^{}\[\]]*\[)?"
r"(?:nigeb|dne)\\"
)
# regex to parse a environment line from the cwl file
# only search for \end to create a list without duplicates
ENVIRONMENT_REGEX = re.compile(
r"\\end"
r"\{(?P<name>[^\}]+)\}"
)
# global setting to check whether the LaTeX-cwl package is available or not
CWL_COMPLETION_ENABLED = None
# global instance of CwlCompletions class
CWL_COMPLETIONS = None
# KOMA-Script classes are all in one cwl file
KOMA_SCRIPT_CLASSES = set(('class-scrartcl', 'class-scrreprt', 'class-book'))
# -- Public Methods --
def is_cwl_available(view=None):
if CWL_COMPLETION_ENABLED is None:
_check_if_cwl_enabled(view)
return CWL_COMPLETION_ENABLED
def get_cwl_file_name(package):
if package.endswith('.cwl'):
cwl_file = package
else:
cwl_file = '{0}.cwl'.format(package)
return cwl_file
# returns the cwl completions instances
def get_cwl_completions():
plugin_loaded()
return CWL_COMPLETIONS
class CwlCompletions(object):
'''
Completion manager that coordinates between between the event listener and
the thread that does the actual parsing. It also stores the completions
once they have been parsed.
N.B. This class should not be instantiated directly. It is intended to
be used a single object stored in the CWL_COMPLETION value of this module
'''
def __init__(self):
self._started = False
self._completed = False
self._triggered = False
self._completions = None
self._environment_completions = None
self._WLOCK = threading.RLock()
# get the completions
def get_completions(self, env=False):
with self._WLOCK:
if self._completed:
self._triggered = False
cwl_files = []
packages = self.get_packages()
if len(packages) == 0:
return []
for package in packages:
cwl_file = get_cwl_file_name(package)
# some hacks for particular packages that do not match
# the standard pattern
if package == 'polyglossia':
cwl_file = 'babel.cwl'
elif package in KOMA_SCRIPT_CLASSES:
cwl_file = 'class-scrartcl,scrreprt,scrbook.cwl'
if cwl_file not in cwl_files:
cwl_files.append(cwl_file)
completions = []
if env:
completion_dict = self._environment_completions
else:
completion_dict = self._completions
for cwl_file in cwl_files:
try:
completions.extend(completion_dict[cwl_file])
except KeyError:
# TODO - should we attempt to load the package?
pass
return completions
else:
self._triggered = True
if not self._started:
self.load_completions()
return []
# loads the list of currently specified cwl files
def get_packages(self):
packages = get_setting('cwl_list', [
"latex-document.cwl",
"tex.cwl",
"latex-dev.cwl",
"latex-209.cwl",
"latex-l2tabu.cwl",
"latex-mathsymbols.cwl"
])
# autoload packages by scanning the document
if get_setting('cwl_autoload', True):
root = get_tex_root(sublime.active_window().active_view())
if root is not None:
doc = analysis.get_analysis(root)
# really, there should only be one documentclass
packages.extend([
'class-{0}'.format(documentclass.args)
for documentclass in doc.filter_commands(
'documentclass',
analysis.ONLY_PREAMBLE |
analysis.ONLY_COMMANDS_WITH_ARGS
)
])
packages.extend([
package.args for package in doc.filter_commands(
'usepackage',
analysis.ONLY_PREAMBLE |
analysis.ONLY_COMMANDS_WITH_ARGS
)
])
packages = [get_cwl_file_name(package) for package in packages]
flag = True
while flag:
packages_set = set(packages)
for package in packages:
packages_set = packages_set.union(set(self._dependencies.get(package, [])))
flag = len(packages_set) > len(packages)
packages = list(packages_set)
# TODO - Attempt to read current buffer
return packages
# loads all available completions on a new background thread
# set force to True to force completions to load regardless
# of whether they have already been loaded
def load_completions(self, force=False):
with self._WLOCK:
if self._started or (self._completed and not force):
return
self._started = True
t = threading.Thread(
target=cwl_parsing_handler,
args=(
self._on_completions,
)
)
t.daemon = True
t.start()
# hack to display the autocompletions once they are available
def _hack(self):
sublime.active_window().run_command("hide_auto_complete")
def hack2():
sublime.active_window().active_view().run_command("auto_complete")
sublime.set_timeout(hack2, 1)
# callback when completions are loaded
def _on_completions(self, completions, environment_completions, dependencies):
with self._WLOCK:
self._completions = completions
self._environment_completions = environment_completions
self._dependencies = dependencies
self._started = False
self._completed = True
# if the user has tried to summon autocompletions, reload
# now that we have some
if self._triggered and len(self._completions) != 0:
sublime.set_timeout(self._hack, 0)
class LatexCwlCompletion(sublime_plugin.EventListener):
'''
Event listener to supply cwl completions at appropriate points
'''
def on_query_completions(self, view, prefix, locations):
if not is_cwl_available():
return []
point = locations[0]
if not view.score_selector(point, "text.tex.latex"):
return []
point_before = point - len(prefix)
char_before = view.substr(
sublime.Region(point_before - 1, point_before))
is_prefixed = char_before == "\\"
line = view.substr(sublime.Region(view.line(point).begin(), point))
line = line[::-1]
is_env = bool(BEGIN_END_BEFORE_REGEX.match(line))
# default completion level is "prefixed"
completion_level = get_setting(
"command_completion", "prefixed"
)
do_complete = {
"never": False,
"prefixed": is_prefixed or is_env,
"always": True
}.get(completion_level, is_prefixed or is_env)
if not do_complete:
return []
# do not autocomplete if the leading backslash is escaped
if ESCAPE_REGEX.match(line):
# if there the autocompletion has been opened with the \ trigger
# (no prefix) and the user has not enabled auto completion for the
# scope, then hide the auto complete popup
selector = view.settings().get("auto_complete_selector")
if not prefix and not view.score_selector(point, selector):
view.run_command("hide_auto_complete")
return []
# Do not do completions in actions
if (
latex_input_completions.TEX_INPUT_FILE_REGEX not in
ENV_DONOT_AUTO_COM
):
ENV_DONOT_AUTO_COM.append(
latex_input_completions.TEX_INPUT_FILE_REGEX)
for rex in ENV_DONOT_AUTO_COM:
if match(rex, line) is not None:
return []
# load the completions for the document
if is_env:
completions = CWL_COMPLETIONS.get_completions(env=True) + \
get_own_env_completion(view)
else:
completions = CWL_COMPLETIONS.get_completions() + \
get_own_command_completion(view)
# autocompleting with slash already on line
# this is necessary to work around a short-coming in ST where having a
# keyed entry appears to interfere with it recognising that there is a
# \ already on the line
#
# NB this may not work if there are other punctuation marks in the
# completion
# this workaround is no longer needed in st4
if sublime.version() < '4058' and is_prefixed:
completions = [
(c[0], c[1][1:]) if c[1].startswith("\\") else c
for c in completions
]
return (
completions,
sublime.INHIBIT_WORD_COMPLETIONS |
sublime.INHIBIT_EXPLICIT_COMPLETIONS
)
# This functions is to determine whether LaTeX-cwl is installed,
# if so, trigger auto-completion in latex buffers by '\'
def on_activated_async(self, view):
is_cwl_available(view)
# used to ensure that completions are loaded whenever a LaTeX document
# is loaded; run on_load instead of on_load_async to assure that view
# exists / is active
def on_load(self, view):
if not view.score_selector(0, "text.tex.latex"):
return
CWL_COMPLETIONS.load_completions()
# -- Internal API --
# run to see if cwl completions should be enabled
CWL_PACKAGE_PATHS = []
def _create_cwl_packages_paths():
global CWL_PACKAGE_PATHS
CWL_PACKAGE_PATHS = [os.path.join(sublime.packages_path(), 'LaTeX-cwl')]
# add to the front as this is most likely to exist
CWL_PACKAGE_PATHS.insert(0, os.path.join(
sublime.installed_packages_path(), 'LaTeX-cwl.sublime-package'
))
CWL_PACKAGE_PATHS.append(
os.path.join(sublime.packages_path(), 'User', 'cwl'))
def _check_if_cwl_enabled(view=None):
if view is None:
try:
view = sublime.active_window().active_view()
except AttributeError:
return
if view is None or not view.score_selector(0, "text.tex.latex"):
return
if get_setting('command_completion', 'prefixed', view=view) == 'never':
return
# Checking whether LaTeX-cwl is installed
global CWL_COMPLETION_ENABLED
for path in CWL_PACKAGE_PATHS:
if os.path.exists(path):
CWL_COMPLETION_ENABLED = True
break
else:
CWL_COMPLETION_ENABLED = False
return
# add `\` as an autocomplete trigger
g_settings = sublime.load_settings("Preferences.sublime-settings")
acts = g_settings.get("auto_complete_triggers", [])
# Whether auto trigger is already set in
# Preferences.sublime-settings
TEX_AUTO_COM = False
for i in acts:
if (
i.get("selector") == "text.tex.latex" and
i.get("characters") == "\\"
):
TEX_AUTO_COM = True
if not TEX_AUTO_COM:
acts.append({
"characters": "\\",
"selector": "text.tex.latex"
})
g_settings.set("auto_complete_triggers", acts)
# pre-load the completions
get_cwl_completions().load_completions()
# -- Internal Parsing API --
# this is the function called by the CwlCompletions class to handle parsing
# it loads every cwl in turn and returns a dictionary mapping from the
# cwl file name to the set of parsed completions
def cwl_parsing_handler(callback):
completion_results = {}
environment_results = {}
dependencies_result = {}
cwl_files, use_package = get_cwl_package_files()
for cwl_file in cwl_files:
base_name = os.path.basename(cwl_file)
if use_package:
try:
s = (sublime.load_resource(cwl_file).replace("\r\n", "\n")
.replace("\r", "\n"))
except IOError:
print(
u'{0} does not exist or could not be accessed'.format(
cwl_file
)
)
continue
except UnicodeDecodeError:
print(
u'{0}: {1}'.format(
cwl_file, exc_info()[1]
)
)
continue
else:
if not os.path.isabs(cwl_file) and cwl_file.startswith('Package'):
cwl_file = os.path.normpath(
cwl_file.replace('Package', sublime.packages_path())
)
try:
s = utils.read_file_unix_endings(cwl_file)
except IOError:
print(
u'{0} does not exist or could not be accessed'.format(
cwl_file
)
)
continue
except UnicodeDecodeError:
print(
u'{0}: {1}'.format(
cwl_file, exc_info()[1]
)
)
continue
completions, environments, dependencies = parse_cwl_file(base_name, s)
completion_results[base_name] = completions
environment_results[base_name] = environments
dependencies_result[base_name] = dependencies
callback(completion_results, environment_results, dependencies_result)
# gets a list of all cwl package files available, whether in the
# sublime-package file or an exploded directory; returns a tuple
# consisting of the list of cwl files and a boolean indicating
# whether it is in a .sublime-package file or an expanded directory
def get_cwl_package_files():
results = [
r for r in sublime.find_resources('*.cwl')
if (r.startswith('Packages/User/cwl/') or
r.startswith('Packages/LaTeX-cwl/'))
]
return(results, True) if results else ([], False)
def parse_line_as_environment(line):
m = ENVIRONMENT_REGEX.match(line)
if not m:
return
env_name = m.group("name")
return env_name, env_name
def parse_line_as_command(line):
return command_to_snippet(line)
# actually does the parsing of the cwl files
def parse_cwl_file(cwl, s):
parse_lines = {"completions": parse_line_as_command, "environments": parse_line_as_environment}
results = {"completions": list(), "environments": list()}
dependencies = []
method = os.path.splitext(cwl)[0]
# we need some state tracking to ignore keyval data
# it could be useful at a later date
KEYVAL = False
for line in s.split('\n'):
line = line.lstrip()
if line == '':
continue
if line[0] == '#':
if line.startswith('#keyvals') or line.startswith('#ifOption'):
KEYVAL = True
if line.startswith('#endkeyvals') or line.startswith('#endif'):
KEYVAL = False
if line.startswith('#include:') and not KEYVAL:
dependencies.append(get_cwl_file_name(line[len('#include:'):]))
continue
# ignore TeXStudio's keyval structures
if KEYVAL:
continue
# remove everything after the comment hash
# again TeXStudio uses this for interesting
# tracking purposes, but we can ignore it
line = line.split('#', 1)[0]
# trailing spaces aren't relevant (done here in case they precede)
# a # char
line = line.rstrip()
for (key, parse_line) in parse_lines.items():
result = parse_line(line)
if result is None:
continue
keyword, insertion = result
# pad the keyword with spaces; this is to keep the size of the
# autocompletions consistent regardless of the returned results
keyword = keyword.ljust(50)
item = (u'%s\t%s' % (keyword, method), insertion)
results[key].append(item)
return results["completions"], results["environments"], dependencies
# ensure that CWL_COMPLETIONS has a value
# its better to do it here because its more stable across reloads
def plugin_loaded():
global CWL_COMPLETIONS
_create_cwl_packages_paths()
if CWL_COMPLETIONS is None:
CWL_COMPLETIONS = CwlCompletions()