This repository has been archived by the owner on May 31, 2024. It is now read-only.
forked from SublimeText/LaTeXTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
latex_ref_completions.py
272 lines (223 loc) · 9.05 KB
/
latex_ref_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
import sublime, sublime_plugin
import os, os.path
import re
import getTeXRoot
def match(rex, str):
m = rex.match(str)
if m:
return m.group(0)
else:
return None
# recursively search all linked tex files to find all
# included \label{} tags in the document and extract
def find_labels_in_files(rootdir, src, labels):
if src[-4:].lower() != ".tex":
src = src + ".tex"
file_path = os.path.normpath(os.path.join(rootdir, src))
print "Searching file: " + repr(file_path)
dir_name = os.path.dirname(file_path)
# read src file and extract all label tags
try:
with open(file_path, "r") as src_file:
src_content = re.sub("%.*", "", src_file.read())
labels += re.findall(r'\\label\{([^\{\}]+)\}', src_content)
except IOError:
print "WARNING! I can't find it! Check your \\include's and \\input's."
return
# search through input tex files recursively
for f in re.findall(r'\\(?:input|include)\{([^\{\}]+)\}', src_content):
find_labels_in_files(dir_name, f, labels)
# Based on html_completions.py
#
# It expands references; activated by
# ref<tab>
# refp<tab> [this adds parentheses around the ref]
# eqref<tab> [obvious]
#
# Furthermore, you can "pre-filter" the completions: e.g. use
#
# ref_sec
#
# to select all labels starting with "sec".
#
# There is only one problem: if you have a label "sec:intro", for instance,
# doing "ref_sec:" will find it correctly, but when you insert it, this will be done
# right after the ":", so the "ref_sec:" won't go away. The problem is that ":" is a
# word boundary. Then again, TextMate has similar limitations :-)
class LatexRefCompletions(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
# Only trigger within LaTeX
if not view.match_selector(locations[0],
"text.tex.latex"):
return []
# Get the contents of line 0, from the beginning of the line to
# the current point
l = locations[0]
line = view.substr(sublime.Region(view.line(l).a, l))
# Reverse, to simulate having the regex
# match backwards (cool trick jps btw!)
line = line[::-1]
#print line
# Check the first location looks like a ref, but backward
rex = re.compile("([^_]*_)?(p)?fer(qe)?")
expr = match(rex, line)
# print expr
if expr:
# Return the matched bits, for mangling
prefix, has_p, has_eq = rex.match(expr).groups()
preformatted = False
if prefix:
prefix = prefix[::-1] # reverse
prefix = prefix[1:] # chop off #
else:
prefix = ""
#print prefix, has_p, has_eq
else:
# Check to see if the location matches a preformatted "\ref{blah"
rex = re.compile(r"([^{}]*)\{fer(qe)?\\(\()?")
expr = match(rex, line)
if not expr:
return []
preformatted = True
# Return the matched bits (barely needed, in this case)
prefix, has_eq, has_p = rex.match(expr).groups()
if prefix:
prefix = prefix[::-1] # reverse
else:
prefix = ""
#print prefix, has_p, has_eq
if has_p:
pre_snippet = "(\\ref{"
post_snippet = "})"
elif has_eq:
pre_snippet = "\\eqref{"
post_snippet = "}"
else:
pre_snippet = "\\ref{"
post_snippet = "}"
if not preformatted:
# Replace ref_blah with \ref{blah
expr_region = sublime.Region(l - len(expr), l)
#print expr[::-1], view.substr(expr_region)
ed = view.begin_edit()
view.replace(ed, expr_region, pre_snippet + prefix)
view.end_edit(ed)
else:
# Don't include post_snippet if it's already present
suffix = view.substr(sublime.Region(l, l + len(post_snippet)))
if post_snippet == suffix:
post_snippet = ""
completions = []
# stop matching at FIRST } after \label{
# I think we don't need to match here, as getTeXroot returns our file name
# if there is no TEX root directive
# view.find_all(r'\\label\{([^\{\}]+)\}', 0, '\\1', completions)
root = getTeXRoot.get_tex_root(view)
print "TEX root: " + repr(root)
find_labels_in_files(os.path.dirname(root), root, completions)
# remove duplicate bib files
completions = list(set(completions))
# r = [(label + "\t\\ref{}", label + post_snippet) for label in completions]
r = [(label, label + post_snippet) for label in completions]
#print r
return (r, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)
### Ref completions using the quick panel
class LatexRefCommand(sublime_plugin.TextCommand):
# Remember that this gets passed an edit object
def run(self, edit):
# get view and location of first selection, which we expect to be just the cursor position
view = self.view
point = view.sel()[0].b
print point
# Only trigger within LaTeX
# Note using score_selector rather than match_selector
if not view.score_selector(point,
"text.tex.latex"):
return
# Get the contents of the current line, from the beginning of the line to
# the current point
line = view.substr(sublime.Region(view.line(point).a, point))
print line
# Reverse, to simulate having the regex
# match backwards (cool trick jps btw!)
line = line[::-1]
#print line
# Check the first location looks like a ref, but backward
rex = re.compile("([^_]*_)?(p)?fer(qe)?")
expr = match(rex, line)
# print expr
if expr:
# Return the matched bits, for mangling
prefix, has_p, has_eq = rex.match(expr).groups()
preformatted = False
if prefix:
prefix = prefix[::-1] # reverse
prefix = prefix[1:] # chop off #
else:
prefix = ""
#print prefix, has_p, has_eq
else:
# Check to see if the location matches a preformatted "\ref{blah"
rex = re.compile(r"([^{}]*)\{fer(qe)?\\(\()?")
expr = match(rex, line)
if not expr:
return []
preformatted = True
# Return the matched bits (barely needed, in this case)
prefix, has_eq, has_p = rex.match(expr).groups()
if prefix:
prefix = prefix[::-1] # reverse
else:
prefix = ""
#print prefix, has_p, has_eq
if has_p:
pre_snippet = "(\\ref{"
post_snippet = "})"
elif has_eq:
pre_snippet = "\\eqref{"
post_snippet = "}"
else:
pre_snippet = "\\ref{"
post_snippet = "}"
if not preformatted:
# Replace ref_blah with \ref{blah
expr_region = sublime.Region(point - len(expr), point)
#print expr[::-1], view.substr(expr_region)
ed = view.begin_edit()
view.replace(ed, expr_region, pre_snippet + prefix)
# save prefix begin and endpoints points
new_point_a = point - len(expr) + len(pre_snippet)
new_point_b = new_point_a + len(prefix)
view.end_edit(ed)
else:
# Don't include post_snippet if it's already present
suffix = view.substr(sublime.Region(point, point + len(post_snippet)))
new_point_a = point - len(prefix)
new_point_b = point
if post_snippet == suffix:
post_snippet = ""
completions = []
root = getTeXRoot.get_tex_root(view)
print "TEX root: " + repr(root)
find_labels_in_files(os.path.dirname(root), root, completions)
# remove duplicate bib files
completions = list(set(completions))
# filter! Note matching is "less fuzzy" than ST2. Room for improvement...
completions = [c for c in completions if prefix in c]
if not completions:
sublime.error_message("No label matches %s !" % (prefix,))
return
# Note we now generate refs on the fly. Less copying of vectors! Win!
def on_done(i):
print "latex_ref_completion called with index %d" % (i,)
# Allow user to cancel
if i<0:
return
ref = completions[i] + post_snippet
print "selected %s" % completions[i]
# Replace ref expression with reference and possibly post_snippet
expr_region = sublime.Region(new_point_a,new_point_b)
ed = view.begin_edit()
view.replace(ed, expr_region, ref)
view.end_edit(ed)
view.window().show_quick_panel(completions, on_done)