-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
EasyClangComplete.py
652 lines (549 loc) · 24.4 KB
/
EasyClangComplete.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
"""EasyClangComplete plugin for Sublime Text 3.
Provides completion suggestions for C/C++ languages based on clang
Attributes:
log (logging.Logger): logger for this module
"""
import sublime
import sublime_plugin
import logging
import shutil
from os import path
from .plugin.utils import tools
from .plugin.view_config import view_config_manager
from .plugin import flags_sources
from .plugin.utils import thread_pool
from .plugin.utils import thread_job
from .plugin.utils import progress_status
from .plugin.utils import quick_panel_handler
from .plugin.utils import action_request
from .plugin.utils import module_reloader
from .plugin.utils import singleton
from .plugin.utils import include_parser
from .plugin.utils import file
from .plugin.settings import settings_manager
from .plugin.settings import settings_storage
from .plugin.utils.subl import subl_bridge
from .plugin.utils.subl import row_col
from .plugin.flags_sources import bazel
# Reload all modules modules ignoring those that contain the given string.
module_reloader.ModuleReloader.reload_all(ignore_string='singleton')
# Set some aliases for simplicity.
SettingsManager = settings_manager.SettingsManager
SettingsStorage = settings_storage.SettingsStorage
ViewConfigManager = view_config_manager.ViewConfigManager
SublBridge = subl_bridge.SublBridge
Tools = tools.Tools
File = file.File
MoonProgressStatus = progress_status.MoonProgressStatus
ColorSublimeProgressStatus = progress_status.ColorSublimeProgressStatus
NoneSublimeProgressStatus = progress_status.NoneSublimeProgressStatus
PosStatus = subl_bridge.PosStatus
CMakeFile = flags_sources.cmake_file.CMakeFile
CMakeFileCache = singleton.CMakeFileCache
GenericCache = singleton.GenericCache
ThreadPool = thread_pool.ThreadPool
ThreadJob = thread_job.ThreadJob
ErrorQuickPanelHandler = quick_panel_handler.ErrorQuickPanelHandler
IncludeCompleter = include_parser.IncludeCompleter
ActionRequest = action_request.ActionRequest
ZeroIndexedRowCol = row_col.ZeroIndexedRowCol
Bazel = bazel.Bazel
log = logging.getLogger("ECC")
log.setLevel(logging.DEBUG)
log.propagate = False
formatter_default = logging.Formatter(
'[%(name)s:%(levelname)-7s]: %(message)s')
formatter_verbose = logging.Formatter(
'[%(name)s:%(levelname)s]:[%(filename)s]:[%(funcName)s]:'
'[%(threadName)s]: %(message)s')
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
ch.setFormatter(formatter_default)
if not log.hasHandlers():
log.addHandler(ch)
handle_plugin_loaded_function = None
handle_plugin_unloaded_function = None
def plugin_loaded():
"""Call right after sublime api is ready to use.
We need it to initialize all the different classes that encapsulate
functionality. We can only properly init them after sublime api is
available."""
module_reloader.ModuleReloader.reload_all(ignore_string='singleton')
GenericCache.clear_all_caches()
handle_plugin_loaded_function()
def plugin_unloaded():
"""Call right before the package was unloaded."""
handle_plugin_unloaded_function()
class EccShowAllErrorsCommand(sublime_plugin.TextCommand):
"""Handle easy_clang_goto_declaration command."""
def run(self, edit):
"""Show all errors available in this view.
This function shows all errors that are available from within a view.
Note that the errors can be from different files.
"""
if not SublBridge.is_valid_view(self.view):
return
config_manager = EasyClangComplete.view_config_manager
if not config_manager:
log.error("No ViewConfigManager available.")
return
config = config_manager.get_from_cache(self.view)
log.debug("config: %s", config)
if not config:
log.error("No ViewConfig for view: %s.", self.view.buffer_id())
return
if not config.completer:
log.error("No Completer for view: %s.", self.view.buffer_id())
window = self.view.window()
ErrorQuickPanelHandler(
self.view, config.completer.latest_errors).show(window)
class EccGotoDeclarationCommand(sublime_plugin.TextCommand):
"""Handle easy_clang_goto_declaration command."""
def run(self, edit):
"""Run goto declaration command.
Navigates to delcaration of entity located by current position
of cursor.
"""
if not SublBridge.is_valid_view(self.view):
return
config_manager = EasyClangComplete.view_config_manager
if not config_manager:
return
location = config_manager.trigger_get_declaration_location(self.view)
if location:
loc = location.file.name
loc += ":" + str(location.line)
loc += ":" + str(location.column)
log.debug("Navigating to declaration: %s", loc)
sublime.active_window().open_file(loc, sublime.ENCODED_POSITION)
class EccCompleteIncludesCommand(sublime_plugin.TextCommand):
"""Handle easy_clang_goto_declaration command."""
def run(self, edit, opening_char):
"""Run goto declaration command.
Navigates to delcaration of entity located by current position
of cursor.
"""
def passthrough(view, trigger):
"""Passthrough a trigger."""
return view.run_command("insert", {"characters": trigger})
if not SublBridge.is_valid_view(self.view):
return passthrough(self.view, opening_char)
settings = EasyClangComplete.settings_manager.settings_for_view(
self.view)
if not settings.autocomplete_includes:
log.debug("Includes completion disabled.")
return passthrough(self.view, opening_char)
config_manager = EasyClangComplete.view_config_manager
if not config_manager:
return passthrough(self.view, opening_char)
config = config_manager.get_from_cache(self.view)
if not config or not config.include_folders:
log.error("No ViewConfig for view: %s.", self.view.buffer_id())
return passthrough(self.view, opening_char)
panel_handler = IncludeCompleter(
view=self.view,
opening_char=opening_char,
thread_pool=EasyClangComplete.thread_pool)
panel_handler.start_completion(config.include_folders,
settings.force_unix_includes)
class CleanCmakeCommand(sublime_plugin.TextCommand):
"""Command that cleans cmake build directory."""
def run(self, edit):
"""Run clean command.
Detects if there is a CMakeLists.txt associated to current view and
cleans all related information in case there is one.
"""
if not SublBridge.is_valid_view(self.view):
return
import gc
file_path = self.view.file_name()
cmake_cache = CMakeFileCache()
try:
cmake_file_path = cmake_cache[file_path]
log.debug("Cleaning file: '%s'", cmake_file_path)
del cmake_cache[file_path]
del cmake_cache[cmake_file_path]
EasyClangComplete.view_config_manager.clear_for_view(
self.view.buffer_id())
# Better safe than sorry. Cleanup!
gc.collect()
temp_proj_dir = CMakeFile.unique_folder_name(cmake_file_path)
if path.exists(temp_proj_dir):
log.debug("Cleaning build directory: '%s'", temp_proj_dir)
shutil.rmtree(temp_proj_dir, ignore_errors=True)
except KeyError:
log.debug("Nothing to clean")
class GenerateBazelCompDbCommand(sublime_plugin.TextCommand):
"""Command that generates a compilation database with bazel."""
def run(self, edit):
"""Run compdb generation command.
Find a WORKSPACE file up the directory tree and run the compilation
database generation command there.
"""
log.debug("Starting generating compilation database.")
if not SublBridge.is_valid_view(self.view):
return
job = ThreadJob(
name=ThreadJob.GENERATE_DB_TAG,
function=Bazel.generate_compdb,
callback=Bazel.compdb_generated,
args=[self.view])
EasyClangComplete.thread_pool.new_job(job)
class EccShowPopupInfoCommand(sublime_plugin.TextCommand):
"""Command that shows popup info on current cursor location."""
def run(self, edit):
"""Run show popup info command."""
position = self.view.sel()[0].begin()
EasyClangComplete.begin_show_info_job(self.view, position)
class EasyClangComplete(sublime_plugin.EventListener):
"""Base class for this plugin.
Most of the functionality is delegated.
"""
thread_pool = ThreadPool(with_progress=True)
view_config_manager = None
settings_manager = None
current_job_id = None
def __init__(self):
"""Initialize the object."""
super().__init__()
global handle_plugin_loaded_function
global handle_plugin_unloaded_function
handle_plugin_loaded_function = self.on_plugin_loaded
handle_plugin_unloaded_function = self.on_plugin_unloaded
# init instance variables to reasonable defaults
self.current_completions = None
self.loaded = False
def on_plugin_unloaded(self):
"""Manage what we do when the plugin is unloaded."""
log.debug("plugin unloaded")
self.loaded = False
def on_plugin_loaded(self):
"""Call upon plugin load event."""
# init settings manager
self.loaded = True
log.debug("handle plugin loaded")
EasyClangComplete.settings_manager = SettingsManager()
# self.on_settings_changed()
EasyClangComplete.settings_manager.add_change_listener(
self.on_settings_changed)
self.on_settings_changed()
# init view config manager
EasyClangComplete.view_config_manager = ViewConfigManager()
# As the plugin has just loaded, we might have missed an activation
# event for the active view so completion will not work for it until
# re-activated. Force active view initialization in that case.
self.on_activated_async(sublime.active_window().active_view())
def on_settings_changed(self):
"""Call when any of the settings changes."""
log.debug("on settings changed handle")
if not self.loaded:
log.warning(
" cannot process settings change as plugin is not loaded")
return
if not EasyClangComplete.settings_manager:
EasyClangComplete.settings_manager = SettingsManager()
user_settings = EasyClangComplete.settings_manager.user_settings()
if user_settings.verbose:
ch.setFormatter(formatter_verbose)
ch.setLevel(logging.DEBUG)
else:
ch.setFormatter(formatter_default)
ch.setLevel(logging.INFO)
if user_settings.need_reparse():
# stop processing this if the settings are still invalid
return
# set progress status
progress_style_tag = user_settings.progress_style
if progress_style_tag == SettingsStorage.MOON_STYLE_TAG:
progress_style = MoonProgressStatus()
elif progress_style_tag == SettingsStorage.COLOR_SUBLIME_STYLE_TAG:
progress_style = ColorSublimeProgressStatus()
else:
progress_style = NoneSublimeProgressStatus()
EasyClangComplete.thread_pool.progress_status = progress_style
def on_activated_async(self, view):
"""Call upon activating a view. Execution in a worker thread.
Args:
view (sublime.View): current view
"""
# disable on_activated_async when running tests
if view.settings().get("disable_easy_clang_complete"):
return
if not SublBridge.is_valid_view(view):
try:
EasyClangComplete.thread_pool.progress_status.erase_status()
except AttributeError as e:
log.debug("cannot clear status, %s", e)
return
settings = EasyClangComplete.settings_manager.settings_for_view(view)
if (not SublBridge.has_valid_syntax(view, settings)) \
or File.is_ignored(
view.file_name(), settings.ignore_list):
try:
EasyClangComplete.thread_pool.progress_status.erase_status()
except AttributeError as e:
log.debug("cannot clear status, %s", e)
return
EasyClangComplete.thread_pool.progress_status.showing = True
log.debug("on_activated_async view id %s", view.buffer_id())
# All is taken care of. The view is built if needed.
job = ThreadJob(
name=ThreadJob.UPDATE_TAG,
callback=EasyClangComplete.config_updated,
function=EasyClangComplete.view_config_manager.load_for_view,
args=[view, settings])
EasyClangComplete.thread_pool.new_job(job)
def on_selection_modified_async(self, view):
"""Call when selection is modified. Executed in gui thread.
Args:
view (sublime.View): current view
"""
if not SublBridge.is_valid_view(view):
return
settings = EasyClangComplete.settings_manager.settings_for_view(view)
if not SublBridge.has_valid_syntax(view, settings):
return
if File.is_ignored(view.file_name(), settings.ignore_list):
return
row_col = ZeroIndexedRowCol.from_current_cursor_pos(view)
view_config = EasyClangComplete.view_config_manager.get_from_cache(view)
if not view_config:
return
if not view_config.completer:
return
view_config.completer.error_vis.show_popup_if_needed(view, row_col.row)
def on_modified_async(self, view):
"""Call in a worker thread when view is modified.
Args:
view (sublime.View): current view
"""
if SublBridge.is_valid_view(view):
log.debug("on_modified_async view id %s", view.buffer_id())
view_config = EasyClangComplete.view_config_manager.get_from_cache(
view)
if not view_config:
return
if not view_config.completer:
return
view_config.completer.error_vis.clear(view)
def on_post_save_async(self, view):
"""Execute in a worker thread on save.
Args:
view (sublime.View): current view
"""
# disable on_activated_async when running tests
if view.settings().get("disable_easy_clang_complete"):
return
if not SublBridge.is_valid_view(view):
return
if view.file_name().endswith('.sublime-project'):
if not EasyClangComplete.settings_manager:
log.error("no settings manager, no cannot reload settings")
return
log.debug("Project file changed. Reloading settings.")
EasyClangComplete.settings_manager.on_settings_changed()
settings = EasyClangComplete.settings_manager.settings_for_view(view)
if not SublBridge.has_valid_syntax(view, settings):
return
if File.is_ignored(view.file_name(), settings.ignore_list):
return
log.debug("saving view: %s", view.buffer_id())
job = ThreadJob(
name=ThreadJob.UPDATE_TAG,
callback=EasyClangComplete.config_updated,
function=EasyClangComplete.view_config_manager.load_for_view,
args=[view, settings])
EasyClangComplete.thread_pool.new_job(job)
# invalidate current completions
self.current_completions = None
def on_close(self, view):
"""Call on closing the view.
Args:
view (sublime.View): current view
"""
if not SublBridge.is_valid_view(view):
# View is invalid, so just ignore it.
return
if not EasyClangComplete.settings_manager.has_settings_for_view(view):
# View is valid, but is temporary and was never actually opened.
# This mostly happens when previewing views while in Ctrl + P
# environment. Do nothing for such a view.
return
# Now we know that the view is valid and we need to clear it.
log.debug("closing view %s", view.buffer_id())
EasyClangComplete.settings_manager.clear_for_view(view)
file_id = view.buffer_id()
job = ThreadJob(
name=ThreadJob.CLEAR_TAG,
callback=EasyClangComplete.config_removed,
function=EasyClangComplete.view_config_manager.clear_for_view,
args=[file_id])
EasyClangComplete.thread_pool.new_job(job)
@staticmethod
def config_removed(future):
"""Call this callback when config has been removed for a view.
The corresponding view id is saved in future.result()
Args:
future (concurrent.Future): future holding id of removed view
"""
if future.done() and not future.cancelled():
log.debug("removed config for id: %s", future.result())
else:
log.debug("could not remove config -> cancelled")
@staticmethod
def config_updated(future):
"""Call this callback when config has been updated for a view.
Args:
future (concurrent.Future): future holding config of updated view
"""
if future.done() and not future.cancelled():
log.debug("updated config: %s", future.result())
else:
log.debug("could not update config -> cancelled")
@staticmethod
def on_open_declaration(location):
"""Call this callback when link to type is clicked in info popup.
Opens location with type declaration
"""
sublime.active_window().open_file(location, sublime.ENCODED_POSITION)
@staticmethod
def info_finished(future):
"""Call this callback when additional information for tag is available.
Creates popup containing information about text under the cursor
"""
if not future.done():
return
if future.cancelled():
return
(tooltip_request, current_popup) = future.result()
if not tooltip_request:
return
if tooltip_request.get_identifier() != EasyClangComplete.current_job_id:
return
if not current_popup:
return
view = tooltip_request.get_view()
current_popup.show(view,
location=tooltip_request.get_trigger_position(),
on_navigate=EasyClangComplete.on_open_declaration)
def completion_finished(self, future):
"""Call this callback when completion async function has returned.
Checks if job id equals the one that is expected now and updates the
completion list that is going to be used in on_query_completions
Args:
future (concurrent.Future): future holding completion result
"""
if not future.done():
return
if future.cancelled():
return
(completion_request, completions) = future.result()
if not completion_request:
return
current_job_id = EasyClangComplete.current_job_id
if completion_request.get_identifier() != current_job_id:
return
active_view = sublime.active_window().active_view()
if completion_request.is_suitable_for_view(active_view):
self.current_completions = completions
else:
log.debug("ignoring completions")
self.current_completions = []
if self.current_completions:
# we only want to trigger the autocompletion popup if there
# are new completions to show there. Otherwise let it be.
SublBridge.show_auto_complete(active_view)
def on_hover(self, view, point, hover_zone):
"""Call this when mouse pointer hovers over text.
Triggers showing popup with additional information about element under
cursor.
"""
if hover_zone != sublime.HOVER_TEXT:
return
EasyClangComplete.begin_show_info_job(view, point)
@staticmethod
def begin_show_info_job(view, position):
"""Start thead job to show popup info.
Triggers showing popup with additional information about about
element at position.
"""
if not SublBridge.is_valid_view(view):
return
settings = EasyClangComplete.settings_manager.settings_for_view(view)
if not SublBridge.has_valid_syntax(view, settings):
return
if not settings.show_type_info:
return
tooltip_request = ActionRequest(view, position)
EasyClangComplete.current_job_id = tooltip_request.get_identifier()
job = ThreadJob(
name=ThreadJob.INFO_TAG,
callback=EasyClangComplete.info_finished,
function=EasyClangComplete.view_config_manager.trigger_info,
args=[view, tooltip_request, settings])
EasyClangComplete.thread_pool.new_job(job)
def on_query_completions(self, view, prefix, locations):
"""Call this when user queries completions in the code.
Args:
view (sublime.View): current view
prefix (TYPE): Description
locations (list[int]): positions of the cursor (first if many)
Returns:
sublime.Completions: completions with a flag.
"""
if not SublBridge.is_valid_view(view):
log.debug("not a valid view")
return SublBridge.SHOW_DEFAULT_COMPLETIONS
# get settings for this view
settings = EasyClangComplete.settings_manager.settings_for_view(view)
if not SublBridge.has_valid_syntax(view, settings):
log.debug("we don't work with this syntax")
return SublBridge.SHOW_DEFAULT_COMPLETIONS
if File.is_ignored(view.file_name(), settings.ignore_list):
log.debug("This file matches 'ignore_list' setting. Ignoring.")
return SublBridge.SHOW_DEFAULT_COMPLETIONS
log.debug("on_query_completions view id %s", view.buffer_id())
log.debug("prefix: %s, locations: %s" % (prefix, locations))
trigger_pos = locations[0] - len(prefix)
completion_request = ActionRequest(view, trigger_pos)
current_pos_id = completion_request.get_identifier()
pos_status = SublBridge.get_pos_status(trigger_pos, view, settings)
log.debug("this position has identifier: '%s'", current_pos_id)
current_job_id = EasyClangComplete.current_job_id
if self.current_completions and current_pos_id == current_job_id:
log.debug("returning existing completions")
return SublBridge.format_completions(
self.current_completions,
settings.hide_default_completions)
# Verify that character under the cursor is one allowed trigger
if pos_status == PosStatus.WRONG_TRIGGER:
# we are at a wrong trigger, remove all completions from the list
log.debug("wrong trigger")
log.debug("hiding default completions")
return SublBridge.HIDE_DEFAULT_COMPLETIONS
if pos_status == PosStatus.COMPLETION_NOT_NEEDED:
log.debug("completion not needed")
# show default completions for now if allowed
if settings.hide_default_completions:
log.debug("hiding default completions")
return SublBridge.HIDE_DEFAULT_COMPLETIONS
log.debug("showing default completions")
return SublBridge.SHOW_DEFAULT_COMPLETIONS
EasyClangComplete.current_job_id = current_pos_id
log.debug("starting async auto_complete with id: %s",
EasyClangComplete.current_job_id)
if pos_status == PosStatus.COMPLETION_NEEDED:
# submit async completion job
config_manager = EasyClangComplete.view_config_manager
job = ThreadJob(
name=ThreadJob.COMPLETE_TAG,
callback=self.completion_finished,
function=config_manager.trigger_completion,
args=[view, completion_request])
EasyClangComplete.thread_pool.new_job(job)
# show default completions for now if allowed
if settings.hide_default_completions:
log.debug("hiding default completions")
return SublBridge.HIDE_DEFAULT_COMPLETIONS
log.debug("showing default completions")
return SublBridge.SHOW_DEFAULT_COMPLETIONS