forked from SublimeText/LaTeXTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
latextools_cache_listener.py
246 lines (184 loc) · 7.1 KB
/
latextools_cache_listener.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
from __future__ import unicode_literals, print_function
import sublime
import sublime_plugin
import collections
from functools import partial
import threading
import traceback
try:
from .latex_cite_completions import (
find_bib_files, run_plugin_command
)
from .latextools_utils import analysis, get_setting
from .latextools_utils.bibcache import BibCache
from .latextools_utils.cache import LocalCache
from .latextools_utils.tex_directives import get_tex_root
from .latextools_utils.progress_indicator import ProgressIndicator
except:
from latex_cite_completions import (
find_bib_files, run_plugin_command
)
from latextools_utils import analysis, get_setting
from latextools_utils.bibcache import BibCache
from latextools_utils.cache import LocalCache
from latextools_utils.tex_directives import get_tex_root
from latextools_utils.progress_indicator import ProgressIndicator
_ST3 = sublime.version() >= '3000'
class LatextoolsCacheUpdater(object):
def __init__(self):
super(LatextoolsCacheUpdater, self).__init__()
self._steps = []
def add_step(self, step):
if step is None:
raise ValueError('step cannot be None')
elif not callable(step):
raise TypeError('step must be a no-args function')
self._steps.append(step)
def run_cache_update(self):
t = threading.Thread(target=self._run_cache_update)
t.daemon = True
t.start()
ProgressIndicator(
t, 'Updating LaTeXTools cache', 'LaTeXTools cache updated')
def _run_cache_update(self):
for step in self._steps:
try:
step()
except:
traceback.print_exc()
class LatextoolsAnalysisUpdater(LatextoolsCacheUpdater):
def run_analysis(self, tex_root):
self.add_step(partial(self._run_analysis, tex_root))
def _run_analysis(self, tex_root):
LocalCache(tex_root).set(
'analysis', analysis.analyze_document(tex_root)
)
class LatextoolsBibCacheUpdater(LatextoolsCacheUpdater):
def run_bib_cache(self, tex_root):
self.add_step(partial(self._invalidate_find_bib_files, tex_root))
self.add_step(partial(self._run_bib_cache, tex_root))
def _invalidate_find_bib_files(self, tex_root):
LocalCache(tex_root).invalidate('bib_files')
def _run_bib_cache(self, tex_root):
run_plugin_command('get_entries', *(find_bib_files(tex_root) or []))
class LatextoolsCacheUpdateListener(
sublime_plugin.EventListener, LatextoolsAnalysisUpdater,
LatextoolsBibCacheUpdater
):
# stores a cache instance per open LaTeX view
# note that cache instances share state
_TEX_CACHES = {}
_TEX_ROOT_REFS = collections.defaultdict(lambda: 0)
_BIB_CACHES = {}
def on_load_async(self, view):
if not view.score_selector(0, 'text.tex.latex'):
return
on_load = get_setting('cache_on_load', {}, view=view)
if not on_load or not any(on_load.values()):
return
tex_root = get_tex_root(view)
if tex_root is None:
return
self._TEX_CACHES[view.id()] = local_cache = LocalCache(tex_root)
self._TEX_ROOT_REFS[tex_root] += 1
# because cache state is shared amongst all documents sharing a tex
# root, this ensure we only load the analysis ONCE in the on_load
# event
if (
not local_cache.has('analysis') and
on_load.get('analysis', False)
):
self.run_analysis(tex_root)
if tex_root not in self._BIB_CACHES:
if on_load.get('bibliography', False):
self.run_bib_cache(tex_root)
self._BIB_CACHES[tex_root] = bib_caches = []
LocalCache(tex_root).invalidate('bib_files')
bib_files = find_bib_files(tex_root)
plugins = get_setting(
'bibliography_plugins', ['traditional'], view=view)
if not isinstance(plugins, list):
plugins = [plugins]
if 'new' in plugins or 'new_bibliography' in plugins:
for bib_file in bib_files:
bib_caches.append(BibCache('new', bib_file))
if (
'traditional' in plugins or
'traditional_bibliography' in plugins
):
for bib_file in bib_files:
bib_caches.append(BibCache('trad', bib_file))
self.run_cache_update()
def on_close(self, view):
if not view.score_selector(0, 'text.tex.latex'):
return
_id = view.id()
try:
tex_root = self._TEX_CACHES[_id].tex_root
self._TEX_ROOT_REFS[tex_root] -= 1
if self._TEX_ROOT_REFS[tex_root] <= 0:
del self._TEX_ROOT_REFS[tex_root]
del self._BIB_CACHES[tex_root]
except:
pass
try:
del self._TEX_CACHES[_id]
except:
pass
def on_post_save_async(self, view):
if not view.score_selector(0, 'text.tex.latex'):
return
on_save = get_setting('cache_on_save', {}, view=view)
if not on_save or not any(on_save.values()):
return
tex_root = get_tex_root(view)
if tex_root is None:
return
_id = view.id()
if _id not in self._TEX_CACHES:
local_cache = self._TEX_CACHES[_id] = LocalCache(tex_root)
else:
local_cache = self._TEX_CACHES[_id]
if on_save.get('analysis', False):
# ensure the cache of bib_files is rebuilt on demand
local_cache.invalidate('bib_files')
self.run_analysis(tex_root)
if on_save.get('bibliography', False):
self.run_bib_cache(tex_root)
self.run_cache_update()
if not _ST3:
on_load = on_load_async
on_post_save = on_post_save_async
class LatextoolsCacheUpdateCommand(object):
def is_visible(self):
return sublime.active_window().active_view().score_selector(
0, 'text.tex.latex'
) > 0
class LatextoolsAnalysisUpdateCommand(
sublime_plugin.TextCommand, LatextoolsCacheUpdateCommand,
LatextoolsAnalysisUpdater
):
def __init__(self, *args, **kwargs):
super(LatextoolsAnalysisUpdateCommand, self).__init__(*args, **kwargs)
def run(self, edit):
if not self.view.score_selector(0, 'text.tex.latex'):
return
tex_root = get_tex_root(self.view)
if tex_root is None:
return
self.run_analysis(tex_root)
self.run_cache_update()
class LatextoolsBibcacheUpdateCommand(
sublime_plugin.TextCommand, LatextoolsCacheUpdateCommand,
LatextoolsBibCacheUpdater
):
def __init__(self, *args, **kwargs):
super(LatextoolsBibcacheUpdateCommand, self).__init__(*args, **kwargs)
def run(self, edit):
if not self.view.score_selector(0, 'text.tex.latex'):
return
tex_root = get_tex_root(self.view)
if tex_root is None:
return
self.run_bib_cache(tex_root)
self.run_cache_update()