-
Notifications
You must be signed in to change notification settings - Fork 365
/
biblatex_snippet_completions.py
72 lines (53 loc) · 2.04 KB
/
biblatex_snippet_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
# coding=utf-8
# This is here precisely so snippet completion doesn't interfere
# with other autocompletions.
import os
import traceback
from xml.etree import ElementTree
import sublime
import sublime_plugin
from .latextools_utils import is_bib_buffer, is_biblatex_buffer
__dir__ = os.path.dirname(__file__)
if __dir__ == '.':
__dir__ = os.path.join(sublime.packages_path(), 'LaTeXTools')
def _get_completions(ext):
completions = []
for root, dirs, files in os.walk(
os.path.join(__dir__, 'snippets')):
files = [f for f in files if f.endswith(ext)]
for f in files:
doc = ElementTree.parse(os.path.join(root, f))
try:
# completions must be tuples on ST2
completions.append(
(
"{0}\t{1}".format(
doc.find('tabTrigger').text.strip(),
doc.find('description').text.strip()
),
doc.find('content').text.strip()
)
)
except:
print(
'Error occurred when trying to load snippet from {0}'
.format(os.path.join(root, f)))
traceback.print_exc()
return completions
def get_biblatex_completions():
return _get_completions('.biblatex-snippet')
def get_bibtex_completions():
return _get_completions('.bibtex-snippet')
class SnippetCompletions(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
if not is_bib_buffer(view):
return []
# do not return completions if the cursor is inside an entry
if view.score_selector(
view.sel()[0].b, 'meta.entry.braces.bibtex') > 0:
return []
if is_biblatex_buffer(view):
return (
get_biblatex_completions(), sublime.INHIBIT_WORD_COMPLETIONS)
else:
return (get_bibtex_completions(), sublime.INHIBIT_WORD_COMPLETIONS)