Skip to content

Commit 22625a3

Browse files
committed
Reused Managers. Improved autocomplete sorting.
1 parent 6c26ef5 commit 22625a3

1 file changed

Lines changed: 85 additions & 67 deletions

File tree

python_codeintel.py

Lines changed: 85 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ def logger(view, type, msg=None, delay=0, timeout=None, id='CodeIntel'):
107107
calltip(view, '%s: %s' % (type, msg), delay, timeout, id + '-' + type)
108108

109109
class PythonCodeIntel(sublime_plugin.EventListener):
110+
def on_close(self, view):
111+
path = view.file_name() or "<Unsaved>"
112+
codeintel_cleanup(path)
113+
110114
def on_modified(self, view):
111115
pos = view.sel()[0].end()
112116
text = view.substr(sublime.Region(pos-7, pos))
@@ -126,7 +130,10 @@ def on_modified(self, view):
126130
cplns, calltips = codeintel(view, path, content, lang, pos, forms=('cplns', 'calltips'))
127131
if cplns is not None:
128132
# Show autocompletions:
129-
self.completions = sorted((name, name) for type, name in cplns)
133+
self.completions = sorted(
134+
[ ('%s (%s)' % (name, type), name) for type, name in cplns ],
135+
cmp=lambda a, b: a[1] < b[1] if a[1].startswith('_') and b[1].startswith('_') else False if a[1].startswith('_') else True if b[1].startswith('_') else a[1] < b[1]
136+
)
130137
view.run_command('auto_complete')
131138
elif calltips is not None:
132139
# Triger a tooltip
@@ -162,11 +169,18 @@ def run(self, edit, block=False):
162169
window.open_file(path, sublime.ENCODED_POSITION)
163170

164171

172+
_codeintel_ = {}
165173
_ci_db_base_dir_ = None
166174
_ci_db_catalog_dirs_ = []
167175
_ci_db_import_everything_langs = None
168176
_ci_extra_module_dirs_ = None
169177

178+
def codeintel_cleanup(path):
179+
if path in _codeintel_:
180+
mgr, env = _codeintel_[path]
181+
mgr.finalize()
182+
del _codeintel_[path]
183+
170184
def codeintel(view, path, content, lang, pos, forms):
171185
cplns = None
172186
calltips = None
@@ -179,77 +193,81 @@ def codeintel(view, path, content, lang, pos, forms):
179193
return [None] * len(forms)
180194
encoding = None
181195

182-
calltip(view, "About to update indexes. The first time this can take a while. Do not despair!", delay=1000)
183-
184-
mgr = Manager(
185-
extra_module_dirs = _ci_extra_module_dirs_,
186-
db_base_dir = _ci_db_base_dir_,
187-
db_catalog_dirs = _ci_db_catalog_dirs_,
188-
db_import_everything_langs = _ci_db_import_everything_langs,
189-
db_event_reporter = lambda m: logger(view, m),
190-
)
191-
catalogs = []
192-
for catalog in mgr.db.get_catalogs_zone().avail_catalogs():
193-
if catalog['lang'] == lang:
194-
catalogs.append(catalog['name'])
195-
config = {
196-
"codeintel_selected_catalogs": catalogs,
197-
"codeintel_max_recursive_dir_depth": 10,
198-
"codeintel_scan_files_in_project": True,
199-
}
200-
_config = {}
201-
tryReadCodeIntelDict(os.path.expanduser(os.path.join('~', '.codeintel', 'config')), _config)
202-
project_dir = find_ropeproject(path, '.codeintel')
203-
if project_dir:
204-
tryReadCodeIntelDict(os.path.join(project_dir, 'config'), _config)
205-
config.update(_config.get(lang, {}))
206-
for conf in [ 'pythonExtraPaths', 'rubyExtraPaths', 'perlExtraPaths', 'javascriptExtraPaths', 'phpExtraPaths' ]:
207-
v = config.get(conf)
208-
if v and isinstance(v, (list, tuple)):
209-
config[conf] = os.pathsep.join(v)
196+
try:
197+
mgr, env = _codeintel_[path]
198+
print 'reused!'
199+
except KeyError:
200+
print 'created!'
201+
calltip(view, "About to update indexes. The first time this can take a while. Do not despair!", delay=1000)
202+
203+
mgr = Manager(
204+
extra_module_dirs = _ci_extra_module_dirs_,
205+
db_base_dir = _ci_db_base_dir_,
206+
db_catalog_dirs = _ci_db_catalog_dirs_,
207+
db_import_everything_langs = _ci_db_import_everything_langs,
208+
db_event_reporter = lambda m: logger(view, m),
209+
)
210+
catalogs = []
211+
for catalog in mgr.db.get_catalogs_zone().avail_catalogs():
212+
if catalog['lang'] == lang:
213+
catalogs.append(catalog['name'])
214+
config = {
215+
"codeintel_selected_catalogs": catalogs,
216+
"codeintel_max_recursive_dir_depth": 10,
217+
"codeintel_scan_files_in_project": True,
218+
}
219+
_config = {}
220+
tryReadCodeIntelDict(os.path.expanduser(os.path.join('~', '.codeintel', 'config')), _config)
221+
project_dir = find_ropeproject(path, '.codeintel')
222+
if project_dir:
223+
tryReadCodeIntelDict(os.path.join(project_dir, 'config'), _config)
224+
config.update(_config.get(lang, {}))
225+
for conf in [ 'pythonExtraPaths', 'rubyExtraPaths', 'perlExtraPaths', 'javascriptExtraPaths', 'phpExtraPaths' ]:
226+
v = config.get(conf)
227+
if v and isinstance(v, (list, tuple)):
228+
config[conf] = os.pathsep.join(v)
210229

211-
env = SimplePrefsEnvironment(**config)
230+
env = SimplePrefsEnvironment(**config)
212231

213-
mgr.env = env
214-
mgr.upgrade()
215-
mgr.initialize()
232+
mgr.env = env
233+
mgr.upgrade()
234+
mgr.initialize()
216235

217-
calltip(view, "")
236+
_codeintel_[path] = (mgr, env)
218237

219-
try:
220-
buf = mgr.buf_from_content(content, lang, env, path, encoding)
221-
if not isinstance(buf, CitadelBuffer):
222-
logger(view, "Error", "`%s' (%s) is not a language that uses CIX" % (path, buf.lang))
223-
return [None] * len(forms)
224-
225-
if 'cplns' in forms or 'calltips' in forms or 'defns' in forms:
238+
calltip(view, "")
239+
240+
buf = mgr.buf_from_content(content, lang, env, path, encoding)
241+
if not isinstance(buf, CitadelBuffer):
242+
logger(view, "Error", "`%s' (%s) is not a language that uses CIX" % (path, buf.lang))
243+
return [None] * len(forms)
244+
245+
if 'cplns' in forms or 'calltips' in forms or 'defns' in forms:
246+
try:
247+
trg = buf.trg_from_pos(pos)
248+
defn_trg = buf.defn_trg_from_pos(pos)
249+
except CodeIntelError:
250+
trg = None
251+
defn_trg = None
252+
else:
253+
eval_log_stream = StringIO()
254+
hdlr = logging.StreamHandler(eval_log_stream)
255+
fmtr = logging.Formatter("%(name)s: %(levelname)s: %(message)s")
256+
hdlr.setFormatter(fmtr)
257+
codeintel_logger.addHandler(hdlr)
258+
ctlr = LogEvalController(codeintel_logger)
226259
try:
227-
trg = buf.trg_from_pos(pos)
228-
defn_trg = buf.defn_trg_from_pos(pos)
229-
except CodeIntelError:
230-
trg = None
231-
defn_trg = None
232-
else:
233-
eval_log_stream = StringIO()
234-
hdlr = logging.StreamHandler(eval_log_stream)
235-
fmtr = logging.Formatter("%(name)s: %(levelname)s: %(message)s")
236-
hdlr.setFormatter(fmtr)
237-
codeintel_logger.addHandler(hdlr)
238-
ctlr = LogEvalController(codeintel_logger)
239-
try:
240-
if 'cplns' in forms and trg and trg.form == TRG_FORM_CPLN:
241-
cplns = buf.cplns_from_trg(trg, ctlr=ctlr)
242-
if 'calltips' in forms and trg and trg.form == TRG_FORM_CALLTIP:
243-
calltips = buf.calltips_from_trg(trg, ctlr=ctlr)
244-
if 'defns' in forms and defn_trg and defn_trg.form == TRG_FORM_DEFN:
245-
defns = buf.defns_from_trg(defn_trg, ctlr=ctlr)
246-
finally:
247-
codeintel_logger.removeHandler(hdlr)
248-
msg = eval_log_stream.getvalue()
249-
if msg:
250-
logger(view, "Error", msg)
251-
finally:
252-
mgr.finalize()
260+
if 'cplns' in forms and trg and trg.form == TRG_FORM_CPLN:
261+
cplns = buf.cplns_from_trg(trg, ctlr=ctlr)
262+
if 'calltips' in forms and trg and trg.form == TRG_FORM_CALLTIP:
263+
calltips = buf.calltips_from_trg(trg, ctlr=ctlr)
264+
if 'defns' in forms and defn_trg and defn_trg.form == TRG_FORM_DEFN:
265+
defns = buf.defns_from_trg(defn_trg, ctlr=ctlr)
266+
finally:
267+
codeintel_logger.removeHandler(hdlr)
268+
msg = eval_log_stream.getvalue()
269+
if msg:
270+
logger(view, "Error", msg)
253271

254272
ret = []
255273
l = locals()

0 commit comments

Comments
 (0)