forked from niosus/EasyClangComplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEasyClangComplete.py
201 lines (163 loc) · 6.54 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
"""EasyClangComplete plugin for Sublime Text 3.
Provides completion suggestions for C/C++ languages based on clang
Attributes:
completer (plugin.completion.base_completion.BaseCompleter):
This object handles auto completion. It can be one of the following:
- bin_complete.Completer
- lib_complete.Completer
log (logging.Logger): logger for this module
settings (plugin_settings.Settings): class that encapsulates settings
"""
import sublime_plugin
import imp
import logging
from threading import Thread
from .plugin import tools
from .plugin import error_vis
from .plugin import plugin_settings
from .plugin.completion import lib_complete
from .plugin.completion import bin_complete
from .plugin.completion import flags_manager
# reload the modules
imp.reload(tools)
imp.reload(flags_manager)
imp.reload(plugin_settings)
imp.reload(error_vis)
imp.reload(lib_complete)
imp.reload(bin_complete)
from .plugin.tools import SublBridge
from .plugin.tools import Tools
from .plugin.tools import PosStatus
# unfortunately because of how sublime text initializes the plugins I cannot
# move these inside of some class.
settings = None
completer = None
log = logging.getLogger(__name__)
def plugin_loaded():
"""called 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."""
global settings
global completer
settings = plugin_settings.Settings()
# init the loggers
if settings.verbose:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
# init everythin else
if settings.use_libclang:
log.info(" init completer based on libclang")
completer = lib_complete.Completer(settings.clang_binary)
if not completer.valid:
log.error(" cannot initialize completer with libclang.")
log.info(" falling back to using clang in a subprocess.")
completer = None
if not completer:
log.info(" init completer based on clang from cmd")
completer = bin_complete.Completer(settings.clang_binary)
class EasyClangComplete(sublime_plugin.EventListener):
"""Base class for this plugin. Most of the functionality is delegated
"""
@staticmethod
def on_activated_async(view):
"""Called upon activating a view. Execution in a worker thread.
Args:
view (sublime.View): current view
"""
log.debug(" on_activated_async view id %s", view.buffer_id())
if Tools.is_valid_view(view):
if not completer:
return
if not completer.needs_init(view):
return
log.debug("init completer for view id: %s", view.buffer_id())
completer.init(view, settings)
@staticmethod
def on_selection_modified(view):
"""Called when selection is modified. Executed in gui thread.
Args:
view (sublime.View): current view
"""
if Tools.is_valid_view(view):
(row, _) = SublBridge.cursor_pos(view)
if not completer:
return
completer.error_vis.show_popup_if_needed(view, row)
@staticmethod
def on_modified_async(view):
"""Called in a worker thread when view is modified
Args:
view (sublime.View): current view
"""
if Tools.is_valid_view(view):
log.debug(" on_modified_async view id %s", view.buffer_id())
if not completer:
return
completer.error_vis.clear(view)
@staticmethod
def on_post_save_async(view):
"""On save. Executed in a worker thread.
Args:
view (sublime.View): current view
"""
if Tools.is_valid_view(view):
log.debug(" saving view: %s", view.buffer_id())
if not completer:
return
completer.error_vis.erase_regions(view)
completer.update(view, settings.errors_on_save)
@staticmethod
def on_close(view):
"""Called on closing the view.
Args:
view (sublime.View): current view
"""
if Tools.is_valid_view(view):
log.debug(" closing view %s", view.buffer_id())
if not completer:
return
completer.remove(view.buffer_id())
@staticmethod
def on_query_completions(view, prefix, locations):
"""Function that is called 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
"""
log.debug(" on_query_completions view id %s", view.buffer_id())
if not Tools.is_valid_view(view):
return Tools.SHOW_DEFAULT_COMPLETIONS
if not completer:
return Tools.SHOW_DEFAULT_COMPLETIONS
if completer.async_completions_ready:
completer.async_completions_ready = False
return completer.get_completions(settings.hide_default_completions)
# Verify that character under the cursor is one allowed trigger
pos_status = Tools.get_position_status(locations[0], view, settings)
if pos_status == PosStatus.WRONG_TRIGGER:
# we are at a wrong trigger, remove all completions from the list
return Tools.HIDE_DEFAULT_COMPLETIONS
if pos_status == PosStatus.COMPLETION_NOT_NEEDED:
# show default completions for now if allowed
if settings.hide_default_completions:
log.debug(" hiding default completions")
return Tools.HIDE_DEFAULT_COMPLETIONS
log.debug(" showing default completions")
return Tools.SHOW_DEFAULT_COMPLETIONS
# create a daemon thread to update the completions
log.debug(" starting async auto_complete at pos: %s", locations[0])
completion_thread = Thread(
target=completer.complete,
args=[view, locations[0], settings.errors_on_save])
completion_thread.deamon = True
completion_thread.start()
# show default completions for now if allowed
if settings.hide_default_completions:
log.debug(" hiding default completions")
return Tools.HIDE_DEFAULT_COMPLETIONS
log.debug(" showing default completions")
return Tools.SHOW_DEFAULT_COMPLETIONS