-
Notifications
You must be signed in to change notification settings - Fork 366
/
latex_input_completions.py
399 lines (339 loc) · 12.5 KB
/
latex_input_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
# -*- coding:utf-8 -*-
import json
import os
import re
import sublime
from .latex_fill_all import FillAllHelper
from .latextools_utils import analysis
from .latextools_utils.is_tex_file import get_tex_extensions
from .latextools_utils.output_directory import (
get_aux_directory, get_output_directory
)
from . import getTeXRoot
from .latextools_utils import get_setting
def _filter_invalid_entries(entries):
"""Remove entries without a regex or sufficient fields."""
remove_entries = []
for i, entry in enumerate(entries):
if "extensions" not in entry:
print("Missing field 'extensions' in entry {0}".format(entry))
remove_entries.append(i)
continue
if "regex" not in entry:
print("Missing field 'regex' in entry {0}".format(entry))
remove_entries.append(i)
continue
try:
reg = re.compile(entry["regex"])
except Exception as e:
print("Invalid regex: '{0}' ({1})".format(entry["regex"], e))
remove_entries.append(i)
continue
if reg.groups != 0:
print("The regex must not have a capturing group, invalidated in "
"entry {0}. You might escape your group with (?:...)"
.format(entry))
remove_entries.append(i)
continue
# remove all blacklisted entries in reversed order, so the remaining
# indexes stay the same
for i in reversed(remove_entries):
del entries[i]
def _update_input_entries(entries):
for entry in entries:
comma = entry.get("comma_separated", False)
if comma:
entry["regex"] = r"([^{}\[\]]*)\{" + entry["regex"]
else:
entry["regex"] = r"([^,{}\[\]]*)\{" + entry["regex"]
entry["type"] = "input"
_fillall_entries = []
_TEX_INPUT_GROUP_MAPPING = None
TEX_INPUT_FILE_REGEX = None
def _post_process_path_only(completions):
result = []
added = set()
for t in completions:
try:
relpath, file_name = t
except:
continue
if relpath == "." or relpath in added:
continue
added.add(relpath)
result.append(relpath + "/")
return result
def plugin_loaded():
# get additional entries from the settings
_setting_entries = get_setting("fillall_helper_entries", [])
_filter_invalid_entries(_setting_entries)
_fillall_entries.extend(_setting_entries)
_fillall_entries.extend([
# input/include
{
"regex": r'(?:edulcni|tupni)\\',
"extensions": [e[1:] for e in get_tex_extensions()],
"strip_extensions": [".tex"]
},
# includegraphics
{
"regex": r'(?:\][^{}\[\]]*\[)?scihpargedulcni\\',
"extensions": get_setting("image_types", [
"pdf", "png", "jpeg", "jpg", "eps"
]),
"folder": "${graphics_path:$base}"
},
# import/subimport
{
"regex": r'\*?(?:tropmibus)\\',
"extensions": [e[1:] for e in get_tex_extensions()],
"strip_extensions": [".tex"],
"post_process": "path_only"
},
{
"regex": r'\}[^{}\[\]]*\{\*?(?:tropmi|morftupni|morfedulcni)?bus\\',
"extensions": [e[1:] for e in get_tex_extensions()],
"strip_extensions": [".tex"],
"post_regex": (
r'\\sub(?:import|includefrom|inputfrom)\*?'
r'\{([^{}\[\]]*)\}\{[^\}]*?$'
),
"folder": "$base/$_1"
},
{
"regex": r'\}[^{}\[\]]*\{\*?(?:tropmi|morftupni|morfedulcni)\\',
"extensions": [e[1:] for e in get_tex_extensions()],
"strip_extensions": [".tex"],
"post_regex": (
r'\\(?:import|includefrom|inputfrom)\*?'
r'\{([^{}\[\]]*)\}\{[^\}]*?$'
),
"folder": "$_1"
},
{
"regex": r'(?:\][^{}\[\]]*\[)?ecruoserbibdda\\',
"extensions": ["bib"]
},
{
"regex": r'yhpargoilbib\\',
"extensions": ["bib"],
"strip_extensions": [".bib"],
"comma_separated": True
}
])
# update the fields of the entries
_update_input_entries(_fillall_entries)
_fillall_entries.extend([
{
"regex": r'([^{}\[\]]*)\{(?:\][^{}\[\]]*\[)?ssalctnemucod\\',
"type": "cached",
"cache_name": "cls"
},
{
"regex": r'([^{}\[\]]*)\{(?:\][^{}\[\]]*\[)?egakcapesu\\',
"type": "cached",
"cache_name": "pkg"
},
{
"regex": r'([^{}\[\]]*)\{elytsyhpargoilbib\\',
"type": "cached",
"cache_name": "bst"
}
])
global _TEX_INPUT_GROUP_MAPPING, TEX_INPUT_FILE_REGEX
_TEX_INPUT_GROUP_MAPPING = dict((i, v) for i, v in enumerate(_fillall_entries))
TEX_INPUT_FILE_REGEX = re.compile(
"(?:{0})".format("|".join(entry["regex"] for entry in _fillall_entries))
)
# Get all file by types
def get_file_list(root, types, filter_exts=[], base_path=None,
output_directory=None, aux_directory=None):
if not base_path:
base_path = os.path.dirname(root)
def file_match(f):
filename, extname = os.path.splitext(f)
# ensure file has extension and its in the list of types
if extname and not extname[1:].lower() in types:
return False
return True
def dir_match(d):
_d = os.path.realpath(os.path.join(dir_name, d))
if (
_d in handled_directories or
_d == output_directory or
_d == aux_directory
):
return False
return True
completions = []
handled_directories = set([])
for dir_name, dirs, files in os.walk(base_path, followlinks=True):
handled_directories.add(os.path.realpath(dir_name))
files = [f for f in files if f[0] != '.' and file_match(f)]
dirs[:] = [d for d in dirs if d[0] != '.' and dir_match(d)]
for f in files:
full_path = os.path.join(dir_name, f)
# Exclude image file have the same name of root file,
# which may be the pdf file of the root files,
# only pdf format.
if os.path.splitext(root)[0] == os.path.splitext(full_path)[0]:
continue
for ext in filter_exts:
if f.endswith(ext):
f = f[:-len(ext)]
completions.append(
(base_path, os.path.relpath(dir_name, base_path), f)
)
return completions
def _get_dyn_entries():
dyn_entries = get_setting("dynamic_fillall_helper_entries", [])
if dyn_entries:
_filter_invalid_entries(dyn_entries)
_update_input_entries(dyn_entries)
dyn_regex = re.compile("(?:{0})".format(
"|".join(entry["regex"] for entry in dyn_entries)))
return dyn_entries, dyn_regex
else:
return [], None
def parse_completions(view, line):
# reverse line, copied from latex_cite_completions, very cool :)
line = line[::-1]
search = None
# search static entries first
search = TEX_INPUT_FILE_REGEX.match(line)
if search:
entries = _fillall_entries
# then search dynamic entries if no static entries match
else:
dyn_entries, dyn_regex = _get_dyn_entries()
if dyn_regex:
search = dyn_regex.match(line)
entries = dyn_entries
# if no regex matched, cancel completions
if not search:
return []
try:
# extract the first group and the prefix from the maching regex
group = next(i for i, v in enumerate(search.groups())
if v is not None)
entry = entries[group]
except Exception as e:
print("Error occurred while extracting entry from matching group.")
print(e)
return []
completions = []
if entry["type"] == "input":
root = getTeXRoot.get_tex_root(view)
if root:
ana = analysis.get_analysis(root)
tex_base_path = ana.tex_base_path(view.file_name())
graphics_path = ";".join(ana.graphics_paths())
completions = []
sub = {
"root": root,
"base": tex_base_path,
}
# only set graphics_path if one exists
if graphics_path != '':
sub['graphics_path'] = graphics_path
if "post_regex" in entry:
m = re.search(entry["post_regex"], line[::-1])
if m:
for i in range(1, len(m.groups()) + 1):
sub["_{0}".format(i)] = m.group(i)
if "folder" in entry:
folders = []
for folder in entry["folder"].split(";"):
folder_path = sublime.expand_variables(folder, sub)
folders.extend(f for f in folder_path.split(";") if f)
else:
folders = [tex_base_path]
for base_path in folders:
output_directory = get_output_directory(view)
aux_directory = get_aux_directory(view)
completions.extend(get_file_list(
root, entry["extensions"],
entry.get("strip_extensions", []),
base_path=base_path,
output_directory=output_directory,
aux_directory=aux_directory
))
else:
# file is unsaved
completions = []
elif entry["type"] == "cached":
cache = _get_cache()
if cache is not None:
completions = cache.get(entry["cache_name"])
else:
print("Unknown entry type {0}.".format(entry["type"]))
if "post_process" in entry:
fkt = globals().get(
"_post_process_{0}".format(entry["post_process"]), None)
if fkt:
completions = fkt(completions)
return completions
def _get_cache():
cache_path = os.path.normpath(
os.path.join(sublime.cache_path(), "LaTeXTools"))
pkg_cache_file = os.path.normpath(
os.path.join(cache_path, 'pkg_cache.cache'))
cache = None
if not os.path.exists(pkg_cache_file):
gen_cache = sublime.ok_cancel_dialog(
"Cache files for installed packages, "
"classes and bibliographystyles do not exists, "
"would you like to generate it? After generating complete, "
"please re-run this completion action!"
)
if gen_cache:
sublime.active_window().run_command("latextools_gen_pkg_cache")
else:
with open(pkg_cache_file) as f:
cache = json.load(f)
return cache
class InputFillAllHelper(FillAllHelper):
def get_auto_completions(self, view, prefix, line):
completions = parse_completions(view, line)
if len(completions) == 0:
return []
elif not type(completions[0]) is tuple:
return completions
else:
return [
# Replace backslash with forward slash to fix Windows paths
# LaTeX does not support forward slashes in paths
os.path.normpath(
os.path.join(relpath, filename)
).replace('\\', '/')
for base_path, relpath, filename in completions
]
def get_completions(self, view, prefix, line):
completions = parse_completions(view, line)
if len(completions) == 0:
return
elif not type(completions[0]) is tuple:
return completions
else:
formatted_completions = []
normal_completions = []
for base_path, relpath, filename in completions:
latex_formatted = os.path.normpath(os.path.join(
relpath, filename)).replace('\\', '/')
formatted_completions.append([
latex_formatted,
os.path.normpath(os.path.join(
base_path, relpath, filename)
)
])
normal_completions.append(latex_formatted)
return formatted_completions, normal_completions
def matches_line(self, line):
if TEX_INPUT_FILE_REGEX.match(line):
return True
_, dyn_regex = _get_dyn_entries()
return bool(
dyn_regex and dyn_regex.match(line)
)
def is_enabled(self):
return get_setting('fill_auto_trigger', True)