-
Notifications
You must be signed in to change notification settings - Fork 39
/
SublimeCodeIntel.py
825 lines (681 loc) · 30.3 KB
/
SublimeCodeIntel.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
# -*- coding: utf-8 -*-
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is SublimeCodeIntel code by German M. Bravo (Kronuz).
#
# Contributor(s):
# ActiveState Software Inc
#
# Portions created by ActiveState Software Inc are Copyright (C) 2000-2007
# ActiveState Software Inc. All Rights Reserved.
#
"""
CodeIntel is a plugin intended to display "code intelligence" information.
The plugin is based in code from the Open Komodo Editor and has a MPL license.
Port by German M. Bravo (Kronuz). 2011-2017
"""
from __future__ import absolute_import, unicode_literals, print_function
NAME = "SublimeCodeIntel"
VERSION = "3.0.0-rc.1"
import os
import re
import logging
import textwrap
import threading
from collections import deque
import sublime
import sublime_plugin
from .libs.codeintel import CodeIntel, CodeIntelBuffer, logger as codeintel_logger, logger_level as codeintel_logger_level
from .settings import Settings, SettingTogglerCommandMixin
logger_name = 'CodeIntel'
logger_level = logging.WARNING # WARNING
logger = logging.getLogger(logger_name)
logger.setLevel(logger_level)
if logger.root.handlers:
logger.root.handlers[0].setFormatter(logging.Formatter("%(name)s: %(levelname)s: %(message)s"))
EXTRA_PATHS_MAP = {
'ECMAScript': 'ecmascriptExtraPaths',
'JavaScript': 'javascriptExtraPaths',
'Node.js': 'nodejsExtraPaths',
'Perl': 'perlExtraPaths',
'PHP': 'phpExtraPaths',
'Python3': 'python3ExtraPaths',
'Python': 'pythonExtraPaths',
'Ruby': 'rubyExtraPaths',
'C++': 'cppExtraPaths',
}
EXCLUDE_PATHS_MAP = {
'ECMAScript': 'ecmascriptExcludePaths',
'JavaScript': 'javascriptExcludePaths',
'Node.js': 'nodejsExcludePaths',
'Perl': 'perlExcludePaths',
'PHP': 'phpExcludePaths',
'Python3': 'python3ExcludePaths',
'Python': 'pythonExcludePaths',
'Ruby': 'rubyExcludePaths',
'C++': 'cppExcludePaths',
}
def unique(lst):
used = set()
return [x for x in lst if x not in used and (used.add(x) or True)]
class CodeintelHandler(object):
HISTORY_SIZE = 64
MAX_FILESIZE = 1 * 1024 * 1024 # 1MB
jump_history_by_window = {} # map of window id -> deque([], HISTORY_SIZE)
status_msg = {}
status_lineno = {}
status_lock = threading.Lock()
def __init__(self, *args, **kwargs):
self.log = logging.getLogger(logger_name + '.' + self.__class__.__name__)
super(CodeintelHandler, self).__init__(*args, **kwargs)
ci.add_observer(self)
@property
def window(self):
if hasattr(self, '_window'):
return self._window
window = sublime.active_window()
if window:
return window
@window.setter
def window(self, value):
self._window = value
@property
def view(self):
if hasattr(self, '_view'):
return self._view
window = self.window
if window:
view = window.active_view()
if view:
return view
@view.setter
def view(self, value):
self._view = value
def set_status(self, ltype, msg=None, timeout=None, delay=0, lid='SublimeCodeIntel', logger_obj=None):
view = self.view
if not view:
return
if timeout is None:
timeout = {'critical': 3000, 'error': 3000, 'warning': 5000, 'info': 10000, 'debug': 10000,
'event': 10000}.get(ltype, 3000)
if msg is None:
msg, ltype = ltype, 'info'
if isinstance(msg, tuple):
try:
msg = msg[0] % msg[1:]
except Exception:
msg = repr(msg)
msg = msg.strip()
CodeintelHandler.status_lock.acquire()
try:
CodeintelHandler.status_msg.setdefault(lid, [None, None, 0])
if msg == CodeintelHandler.status_msg[lid][1]:
return
CodeintelHandler.status_msg[lid][2] += 1
order = CodeintelHandler.status_msg[lid][2]
finally:
CodeintelHandler.status_lock.release()
def _set_status():
is_warning = 'warning' in lid
if not is_warning:
view_sel = view.sel()
lineno = view.rowcol(view_sel[0].end())[0] if view_sel else 0
CodeintelHandler.status_lock.acquire()
try:
current_type, current_msg, current_order = CodeintelHandler.status_msg.get(lid, [None, None, 0])
if msg != current_msg and order == current_order:
_logger_obj = getattr(logger, ltype, None) if logger_obj is None else logger_obj
if _logger_obj:
_logger_obj(msg)
if ltype != 'debug':
view.set_status(lid, "%s %s: %s" % (lid, ltype.capitalize(), msg.rstrip('.')))
CodeintelHandler.status_msg[lid] = [ltype, msg, order]
if not is_warning:
CodeintelHandler.status_lineno[lid] = lineno
finally:
CodeintelHandler.status_lock.release()
def _erase_status():
CodeintelHandler.status_lock.acquire()
try:
if msg == CodeintelHandler.status_msg.get(lid, [None, None, 0])[1]:
view.erase_status(lid)
CodeintelHandler.status_msg[lid][1] = None
if lid in CodeintelHandler.status_lineno:
del CodeintelHandler.status_lineno[lid]
finally:
CodeintelHandler.status_lock.release()
if msg:
sublime.set_timeout(_set_status, delay or 0)
sublime.set_timeout(_erase_status, timeout)
else:
sublime.set_timeout(_erase_status, delay or 0)
def pos2bytes(self, content, pos):
return len(content[:pos].encode('utf-8'))
def guess_language(self, view, path):
language = os.path.splitext(os.path.basename(view.settings().get('syntax')))[0]
lang = settings.get('syntax_map', {}).get(language, language)
logger.info("Language guessed: %s (for %s)", lang, language)
if lang in settings.get('disabled_languages', []):
return
if settings.get('@disable', False, lang=lang):
return
return lang
def buf_from_view(self, view):
if not view:
return
view_sel = view.sel()
if not view_sel:
return
file_name = view.file_name()
path = file_name if file_name else "<Unsaved>"
lang = self.guess_language(view, path)
if not lang or lang not in ci.languages:
logger.debug("buf_from_view: %r, %r? no: language unavailable in: [%s]", path, lang, ", ".join(ci.languages))
return
if not settings.get('live', False, lang=lang):
logger.debug("buf_from_view: %r, %r? no: live disabled", path, lang)
return
view_size = view.size()
if view.size() > self.MAX_FILESIZE:
logger.warn("File %r has size greater than 1MB (%d)", path, view_size)
return
logger.debug("buf_from_view: %r, %r? yes", path, lang)
vid = view.id()
try:
buf = ci.buffers[vid]
except KeyError:
logger.debug("creating new %s document %s", lang, path)
buf = CodeIntelBuffer(ci, vid=vid)
ci.buffers[vid] = buf
sel = view_sel[0]
original_pos = sel.end()
lpos = view.line(sel).begin()
text_in_current_line = view.substr(sublime.Region(lpos, original_pos + 1))
text = view.substr(sublime.Region(0, view_size))
# Get encoded content and current position
pos = self.pos2bytes(text, original_pos)
buf.lang = lang
buf.path = path
buf.text = text
buf.pos = pos
buf.text_in_current_line = text_in_current_line
buf.original_pos = original_pos
prefs = settings.get_prefs(lang)
if settings.get('scan_files_in_project', lang=lang):
window = sublime.active_window()
extra_paths_name = EXTRA_PATHS_MAP.get(lang)
extra_paths = prefs.get(extra_paths_name, '').split(os.pathsep)
exclude_paths_name = EXCLUDE_PATHS_MAP.get(lang)
exclude_paths = prefs.get(exclude_paths_name, '').split(os.pathsep)
for f in window.folders():
f = os.path.normcase(os.path.normpath(os.path.expanduser(f))).rstrip(os.sep)
if f not in exclude_paths and f not in extra_paths:
extra_paths.append(f)
if extra_paths:
prefs[extra_paths_name] = os.pathsep.join(extra_paths)
buf.prefs = prefs
return buf
def format_completions_by_language(self, cplns, lang, text_in_current_line, type):
function = None if 'import ' in text_in_current_line else 'function'
def get_desc(c):
return c[2] if len(c) > 2 else c[1]
def get_type(c):
return c[0].title()
if lang == 'PHP' and type != 'object-members':
def get_name(c):
name = c[1]
if c[0] == 'variable':
name = "$" + name
name = name.replace("$", "\\$")
if c[0] == function:
name += "($0)"
return name
elif lang == 'ECMAScript':
def get_name(c):
name = c[1]
name = name.replace("$", "\\$")
if c[0] == 'attribute':
name += "=$0 "
elif c[0] == function:
name += "($0)"
return name
else:
def get_name(c):
name = c[1]
name = name.replace("$", "\\$")
if c[0] == function:
name += "($0)"
return name
def sorter(c):
return {
'import': '_',
'attribute': '__',
'variable': '__',
'function': '___',
}.get(c[0].lower(), c[0]), c[1]
return [('%s\t〔%s〕' % (get_desc(c), get_type(c)), get_name(c)) for c in sorted(cplns, key=sorter)]
# Handlers follow
def on_document_scanned(self, buf):
"""Handler callback for scan_document"""
def on_get_calltip_range(self, buf, start, end):
pass
def on_trg_from_pos(self, buf, context, trg):
if context == 'trg_from_pos':
buf.async_eval_at_trg(self, trg)
elif context == 'defn_trg_from_pos':
buf.async_eval_at_trg(self, trg)
def set_status_message(self, buf, message, highlight=None):
def _set_status_message():
self.set_status(message)
sublime.set_timeout(_set_status_message, 0)
def set_call_tip_info(self, buf, calltip, explicit, trg):
def _set_call_tip_info():
view = self.view
if not view:
return
vid = view.id()
if vid != buf.vid:
return
# TODO: This snippets are created and work for Python language def functions.
# i.e. in the form: name(arg1, arg2, arg3)
# Other languages might need different treatment.
# Figure out how many arguments are there already:
text_in_current_line = buf.text_in_current_line[:-1] # Remove next char after cursor
arguments = text_in_current_line.rpartition('(')[2].replace(' ', '').strip() or 0
if arguments:
initial_separator = ''
if arguments[-1] == ',':
arguments = arguments[:-1]
else:
initial_separator += ','
if not text_in_current_line.endswith(' '):
initial_separator += ' '
arguments = arguments.count(',') + 1 if arguments else 0
# Insert parameters as snippet:
snippet = None
tip_info = calltip.split('\n')
tip0 = tip_info[0]
m = re.search(r'^(.*\()([^\[\(\)]*)(.*)$', tip0)
if m:
params = [p.strip() for p in m.group(2).split(',')]
if params:
n = 1
tip0 = []
snippet = []
for i, p in enumerate(params):
if p:
var, sep, default = p.partition('=')
var = var.strip()
tvar = var
if sep:
tvar = "%s<i>=%s</i>" % (tvar, default)
# if i == arguments:
# tvar = "<b>%s</b>" % tvar
tip0.append(tvar)
if i >= arguments:
if ' ' in var:
var = var.split(' ')[1]
if var[0] == '$':
var = var[1:]
snippet.append('${%s:%s}' % (n, var))
n += 1
tip0 = "<h1>%s%s%s</h1>" % (m.group(1), ', '.join(tip0), m.group(3))
snippet = ', '.join(snippet)
if arguments and snippet:
snippet = initial_separator + snippet
css = (
"html {background-color: #232628; color: #999999;}" +
"body {font-size: 10px; }" +
"b {color: #6699cc; }" +
"a {color: #99cc99; }" +
"h1 {color: #cccccc; font-weight: normal; font-size: 11px; }"
)
# Wrap lines that are too long:
wrapper = textwrap.TextWrapper(width=100, break_on_hyphens=False, break_long_words=False)
measured_tips = [tip0]
for t in tip_info[1:]:
measured_tips.extend(wrapper.wrap(t))
if hasattr(view, 'show_popup'):
def insert_snippet(href):
view.run_command('insert_snippet', {'contents': snippet})
view.hide_popup()
view.show_popup('<style>%s</style>%s<br><br><a href="insert">insert</a>' % (css, "<br>".join(measured_tips)), location=-1, max_width=700, on_navigate=insert_snippet)
else:
# Insert tooltip snippet
padding = ' '
snippets = [((padding if i > 0 else '') + l + (padding if i > 0 else ''), snippet or '${0}') for i, l in enumerate(measured_tips)]
buf.cplns = snippets or None
if buf.cplns:
view.run_command('auto_complete', {
'disable_auto_insert': True,
'api_completions_only': True,
'next_completion_if_showing': False,
'auto_complete_commit_on_tab': True,
})
sublime.set_timeout(_set_call_tip_info, 0)
def set_auto_complete_info(self, buf, cplns, trg):
def _set_auto_complete_info():
view = self.view
if not view:
return
vid = view.id()
if vid != buf.vid:
return
_cplns = self.format_completions_by_language(cplns, buf.lang, buf.text_in_current_line, trg.get('type'))
buf.cplns = _cplns or None
if buf.cplns:
view.run_command('auto_complete', {
'disable_auto_insert': True,
'api_completions_only': True,
'next_completion_if_showing': False,
'auto_complete_commit_on_tab': True,
})
sublime.set_timeout(_set_auto_complete_info, 0)
def set_definitions_info(self, buf, defns, trg):
def _set_definitions_info():
view = self.view
view_sel = view.sel()
if not view_sel:
return
file_name = view.file_name()
path = file_name if file_name else "<Unsaved>"
defn = defns[0]
row, col = defn['line'], 1
path = defn['path']
if not path:
msg = "Cannot jump to definition!"
logger.debug(msg)
return
jump_location = "%s:%s:%s" % (path, row, col)
msg = "Jumping to: %s" % jump_location
logger.debug(msg)
window = sublime.active_window()
wid = window.id()
if wid not in CodeintelHandler.jump_history_by_window:
CodeintelHandler.jump_history_by_window[wid] = deque([], CodeintelHandler.HISTORY_SIZE)
jump_history = CodeintelHandler.jump_history_by_window[wid]
# Save current position so we can return to it
row, col = view.rowcol(view_sel[0].begin())
current_location = "%s:%d:%d" % (file_name, row + 1, col + 1)
jump_history.append(current_location)
window.open_file(jump_location, sublime.ENCODED_POSITION)
window.open_file(jump_location, sublime.ENCODED_POSITION)
sublime.set_timeout(_set_definitions_info, 0)
def done(self):
pass
class SublimeCodeIntel(CodeintelHandler, sublime_plugin.EventListener):
def observer(self, topic, data):
def _get_and_log_message(response):
message = response.get('message')
if message:
stack = response.get('stack')
if stack:
logger.error(message.rstrip() + "\n" + stack)
return message
def _observer():
if topic == 'status_message':
ltype = 'info'
elif topic == 'error_message':
ltype = 'error'
elif 'codeintel_buffer_scanned':
return
else:
return
logger_obj = None
if data.get('type') == 'logging':
# logger_name = data.get('name')
level = data.get('level')
if level >= logging.CRITICAL:
logger_obj = logger.critical
ltype = 'critical'
elif level >= logging.ERROR:
logger_obj = logger.error
ltype = 'error'
elif level >= logging.WARNING:
logger_obj = logger.warn
ltype = 'warning'
elif level >= logging.INFO:
logger_obj = logger.info
ltype = 'info'
elif level >= logging.DEBUG:
logger_obj = logger.debug
ltype = 'debug'
progress = data.get('progress') or data.get('completed')
if progress is not None:
total = data.get('total', 100)
if not total:
progress = None
elif total == 100:
progress = ("%0.1f" % progress).rstrip('.0') + "%"
else:
progress = "%s/%s" % (progress, total)
message = _get_and_log_message(data)
if progress and message:
message = "%s - %s" % (progress, message)
elif progress:
message = progress
elif not message:
return
self.set_status(ltype, message, lid='SublimeCodeIntel Notification', logger_obj=logger_obj)
sublime.set_timeout(_observer, 0)
def on_pre_save(self, view):
if view.is_dirty():
buf = self.buf_from_view(view)
if buf:
buf.scan_document(self, True)
def on_close(self, view):
vid = view.id()
ci.buffers.pop(vid, None)
def on_modified(self, view):
view_sel = view.sel()
if not view_sel:
return
if settings.get('@disable', False) or not settings.get('live', False):
return
sel = view_sel[0]
pos = sel.end()
current_char = view.substr(sublime.Region(pos - 1, pos))
if not current_char or current_char in ('\n', '\t'):
return
command_history = getattr(view, 'command_history', None)
if command_history:
redo_command = command_history(1)
previous_command = view.command_history(0)
before_previous_command = view.command_history(-1)
else:
redo_command = previous_command = before_previous_command = None
# print('on_modified', "%r\n\tcommand_history: %r\n\tredo_command: %r\n\tprevious_command: %r\n\tbefore_previous_command: %r" % (current_char, bool(command_history), redo_command, previous_command, before_previous_command))
if not command_history or redo_command[1] is None and (
previous_command[0] == 'insert' and previous_command[1]['characters'][-1] not in ('\n', '\t') or
previous_command[0] in ('insert_completion', 'paste', 'codeintel_complete_commit') or
previous_command[0] == 'insert_snippet' and previous_command[1]['contents'] in (
'(${0:$SELECTION})', '[${0:$SELECTION}]', '{${0:$SELECTION}}', '`${0:$SELECTION}`', '"${0:$SELECTION}"', "'${0:$SELECTION}'",
'($0)', '[$0]', '{$0}', '`$0`', '"$0"', "'$0'",
) or
before_previous_command[0] in ('insert', 'paste') and (
previous_command[0] == 'commit_completion' or
previous_command[0] == 'insert_completion' or
previous_command[0] == 'insert_best_completion'
)
):
buf = self.buf_from_view(view)
# print('on_modified.triggering', bool(buf))
if buf:
buf.trg_from_pos(self, True)
def on_selection_modified(self, view):
pass
def on_query_completions(self, view, prefix, locations):
buf = self.buf_from_view(view)
if buf:
cplns, buf.cplns = getattr(buf, 'cplns', None), None
return cplns
def on_query_context(self, view, key, operator, operand, match_all):
if key.startswith("codeintel.setting."):
setting_name = key[len("codeintel.setting."):]
value = settings.get(setting_name)
if operator == sublime.OP_NOT_EQUAL:
return value != operand
elif operator == sublime.OP_EQUAL:
return value == operand
class CodeintelAutoCompleteCommand(CodeintelHandler, sublime_plugin.TextCommand):
def run(self, edit, block=False):
view = self.view
buf = self.buf_from_view(view)
if buf:
buf.trg_from_pos(self, True)
class CodeintelGoToDefinitionCommand(CodeintelHandler, sublime_plugin.TextCommand):
def run(self, edit, block=False):
view = self.view
buf = self.buf_from_view(view)
if buf:
buf.defn_trg_from_pos(self)
class CodeintelBackFromDefinitionCommand(sublime_plugin.TextCommand):
def run(self, edit, block=False):
window = sublime.active_window()
wid = window.id()
if wid in CodeintelHandler.jump_history_by_window:
jump_history = CodeintelHandler.jump_history_by_window[wid]
if len(jump_history) > 0:
previous_location = jump_history.pop()
window = sublime.active_window()
window.open_file(previous_location, sublime.ENCODED_POSITION)
class CodeintelCompleteCommitCommand(CodeintelHandler, sublime_plugin.TextCommand):
def run(self, edit, character):
view = self.view
buf = self.buf_from_view(view)
if buf:
cpln_fillup_chars = buf.cpln_fillup_chars
cpln_stop_chars = buf.cpln_stop_chars
else:
cpln_fillup_chars = ""
cpln_stop_chars = "~`!@#$%^&*()-=+{}[]|\\;:'\",.<>?/ "
# Fillup characters commit autocomplete
if settings.get(buf.lang, 'complete_commit_fillup') and character in cpln_fillup_chars:
view.window().run_command('commit_completion')
if character not in ("(", "="):
view.run_command('insert', {'characters': character})
# Stop characters hide autocomplete window
elif character in cpln_stop_chars:
view.run_command('hide_auto_complete')
view.run_command('insert', {'characters': character})
else:
view.run_command('insert', {'characters': character})
################################################################################
# Initialize settings and main objects only once
class CodeintelSettings(Settings):
nested_settings = ('syntax_map', 'language_settings')
def get(self, setting, default=None, lang=None):
"""Return a plugin setting, defaulting to default if not found."""
language_settings = self.settings.get('language_settings', {}).get(lang)
if language_settings and setting in language_settings:
return language_settings[setting]
return super(CodeintelSettings, self).get(setting, default)
def on_update(self):
"""
The previous settings are compared with the new settings; depending
on what changed, engine will either be reconfigured or restarted.
"""
need_deactivate = False
for setting in ('@disable', 'command', 'oop_mode', 'log_levels'):
if (
setting in self.changeset or
self.previous_settings and self.previous_settings.get(setting) != self.settings.get(setting)
):
self.changeset.discard(setting)
need_deactivate = True
if (
'debug' in self.changeset or
self.previous_settings.get('debug', False) != self.settings.get('debug', False)
):
self.changeset.discard('debug')
if self.settings.get('debug'):
logger.setLevel(logging.DEBUG)
codeintel_logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logger_level)
codeintel_logger.setLevel(codeintel_logger_level)
if need_deactivate:
ci.deactivate()
if not self.settings.get('@disable'):
env = dict(os.environ)
env.update(self.settings.get('env', {}))
prefs = self.get_prefs()
if ci.enabled:
ci.mgr.set_global_environment(
env=env,
prefs=prefs,
)
else:
command = self.settings.get('command')
oop_mode = self.settings.get('oop_mode')
log_levels = self.settings.get('log_levels')
ci.activate(
reset_db_as_necessary=False,
codeintel_command=command,
oop_mode=oop_mode,
log_levels=log_levels,
env=env,
prefs=prefs,
)
def get_prefs(self, lang=None):
prefs = {
'codeintel_max_recursive_dir_depth': self.settings.get('max_recursive_dir_depth'),
'codeintel_scan_files_in_project': self.settings.get('scan_files_in_project'),
'codeintel_selected_catalogs': self.settings.get('selected_catalogs'),
}
disabled_languages = self.settings.get('disabled_languages', [])
scan_extra_paths = self.settings.get('scan_extra_paths', [])
if scan_extra_paths:
scan_extra_paths = [os.path.normcase(os.path.normpath(os.path.expanduser(e))).rstrip(os.sep) for e in scan_extra_paths]
scan_exclude_paths = self.settings.get('scan_exclude_paths', [])
if scan_exclude_paths:
scan_exclude_paths = [os.path.normcase(os.path.normpath(os.path.expanduser(e))).rstrip(os.sep) for e in scan_exclude_paths]
language_settings = self.settings.get('language_settings', {})
for language, language_settings in language_settings.items():
if lang is not None and language != lang:
continue
if language in disabled_languages or language_settings.get('@disable'):
continue
for k, v in language_settings.items():
if k not in self.settings:
prefs[k] = v
extra_paths_name = EXTRA_PATHS_MAP.get(language)
language_scan_extra_paths = language_settings.get('scan_extra_paths', []) + language_settings.get(extra_paths_name, [])
if language_scan_extra_paths:
language_scan_extra_paths = [os.path.normcase(os.path.normpath(os.path.expanduser(e))).rstrip(os.sep) for e in language_scan_extra_paths]
if language in ('Python', 'Python3'):
language_scan_extra_paths.append(os.path.normcase(os.path.normpath(os.path.dirname(sublime.__file__))))
if extra_paths_name:
prefs[extra_paths_name] = os.pathsep.join(unique(scan_extra_paths + language_scan_extra_paths))
exclude_paths_name = EXCLUDE_PATHS_MAP.get(language)
language_scan_exclude_paths = language_settings.get('scan_exclude_paths', []) + language_settings.get(exclude_paths_name, [])
if language_scan_exclude_paths:
language_scan_exclude_paths = [os.path.normcase(os.path.normpath(os.path.expanduser(e))).rstrip(os.sep) for e in language_scan_exclude_paths]
if exclude_paths_name:
prefs[exclude_paths_name] = os.pathsep.join(unique(scan_exclude_paths + language_scan_exclude_paths))
return prefs
settings = CodeintelSettings(NAME)
class CodeintelToggleSettingCommand(SettingTogglerCommandMixin, sublime_plugin.WindowCommand):
settings = settings
if 'ci' not in globals():
ci = CodeIntel(lambda fn: sublime.set_timeout(fn, 0))
################################################################################
def plugin_loaded():
settings.load()
# ST3 features a plugin_loaded hook which is called when ST's API is ready.
#
# We must therefore call our init callback manually on ST2. It must be the last
# thing in this plugin (thanks, beloved contributors!).
if int(sublime.version()) < 3000:
plugin_loaded()