This repository has been archived by the owner on Nov 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clangcomplete.py
292 lines (258 loc) · 8.62 KB
/
clangcomplete.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
#!/usr/bin/env python
#
# Copyright (C) 2016 Martin Willi
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
import os, subprocess, shlex
from clang.cindex import TranslationUnit, Index
from gi.repository import GObject, Gtk, Gedit, GtkSource
class ClangCompletionProvider(GObject.Object, GtkSource.CompletionProvider):
__gtype_name__ = 'ClangCompletionProvider'
def __init__(self, window):
GObject.Object.__init__(self)
self.window = window
self.index = Index.create()
self.completions = None
self.line = 0
self.token = None
self.doc = None
self.resource_dir = None
self.max_proposals = 16
def _get_buffer(self, context):
return context.get_iter()[1].get_buffer()
def _get_pos(self, buf):
pos = buf.get_iter_at_offset(buf.get_property('cursor-position'))
return pos.get_line() + 1, pos.get_line_offset() + 1
def _get_token_before_iter(self, end):
buf = end.get_buffer()
start = buf.get_iter_at_offset(end.get_offset())
start.set_line_offset(0)
line = buf.get_text(start, end, False)
if line.endswith('.') or line.endswith('->'):
return ''
tlen = 0
for c in reversed(line):
if c.isalnum() or c == '_':
tlen = tlen + 1
else:
break
if tlen == 0:
return None
return line[len(line) - tlen:]
def _get_token(self, context):
buf = self._get_buffer(context)
end = context.get_iter()[1]
return self._get_token_before_iter(end)
def _get_doc(self):
doc = self.window.get_active_document()
if doc:
loc = doc.get_location()
if loc:
return loc.get_path()
return None
def _get_docdir(self):
path = self._get_doc()
if path:
return os.path.dirname(path)
return None
def _find_makefile(self):
docdir = self._get_docdir()
while docdir:
makefile = os.path.join(docdir, 'Makefile')
if os.path.isfile(makefile):
return makefile
if os.path.dirname(docdir) == docdir:
return None
docdir = os.path.dirname(docdir)
return None
def _cd_builddir(self):
makefile = self._find_makefile()
if makefile:
os.chdir(os.path.dirname(makefile))
def _add_cwd_include(self, args):
docdir = self._get_docdir()
if docdir:
args.append("-I{}".format(docdir).encode('utf-8'))
def _get_clang_resource_dir(self):
pipe = subprocess.PIPE
clang = subprocess.Popen(['clang', '-###', '-E', '-'],
stdout=pipe, stdin=pipe, stderr=pipe)
out = clang.communicate(input=''.encode('utf-8'))[1].decode('utf-8')
for line in out.split('\n'):
if len(line) != 0:
match = False
for arg in shlex.split(line):
if match:
self.resource_dir = "-I{}/include".format(arg)
return True
if arg == '-resource-dir':
match = True
return False
def _add_clang_resource_dir(self, args):
if self.resource_dir or self._get_clang_resource_dir():
args.append(self.resource_dir.encode('utf-8'))
def _add_make_cflags(self, args):
makefile = self._find_makefile()
if makefile:
pipe = subprocess.PIPE
make = subprocess.Popen(['make', '-f', makefile, '-f', '-',
'print-CFLAGS', 'print-CPPFLAGS',
'print-AM_CFLAGS', 'print-AM_CPPFLAGS'],
stdout=pipe, stdin=pipe, stderr=pipe)
printer = 'print-%:\n\t@echo \'$*=$($*)\'\n'.encode('utf-8')
out = make.communicate(input=printer)[0].decode('utf-8')
for line in out.split('\n'):
if len(line) != 0:
for arg in shlex.split(line[line.index('=') + 1:]):
args.append(arg.encode('utf-8'))
def _add_make_include_dirs(self, args):
docdir = self._get_docdir()
while docdir:
makefile = os.path.join(docdir, 'Makefile')
if os.path.isfile(makefile):
args.append("-I{}".format(os.path.dirname(makefile)).encode('utf-8'))
if os.path.dirname(docdir) == docdir:
break
docdir = os.path.dirname(docdir)
def _get_completion_args(self, context):
args = []
self._add_clang_resource_dir(args)
self._add_cwd_include(args)
self._add_make_cflags(args)
self._add_make_include_dirs(args)
return args
def _get_completion_path(self):
path = self._get_doc()
if (path):
return os.path.relpath(path, os.getcwd()).encode('utf-8')
return None
def _can_complete(self, context):
lang = self._get_buffer(context).get_language()
if lang:
if lang.get_id() == 'c':
return True
return False
def _get_completions(self, context, token):
parseopts = TranslationUnit.PARSE_INCOMPLETE
parseopts += TranslationUnit.PARSE_PRECOMPILED_PREAMBLE
parseopts += TranslationUnit.PARSE_CACHE_COMPLETION_RESULTS
parseopts += TranslationUnit.PARSE_SKIP_FUNCTION_BODIES
parseopts += TranslationUnit.PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION
if not self._can_complete(context):
return []
buf = self._get_buffer(context)
line, column = self._get_pos(buf)
column -= len(token)
cwd = os.getcwd()
self._cd_builddir()
path = self._get_completion_path()
args = self._get_completion_args(context)
src = buf.get_text(buf.get_start_iter(), buf.get_end_iter(), True)
files = [(path, src)]
tu = TranslationUnit.from_source(path, args, unsaved_files=files,
options=parseopts,
index=self.index)
cr = tu.codeComplete(path, line, column, unsaved_files=files,
include_macros=True, include_code_patterns=True,
include_brief_comments=True)
os.chdir(cwd)
completions = []
for result in cr.results:
hint = ''
contents = ''
string = result.string
doc = string.briefComment
for chunk in string:
s = chunk.spelling
if not s:
continue
if not isinstance(s, str):
s = s.decode('utf-8')
if chunk.isKindTypedText():
trigger = s
hint += s
if chunk.isKindResultType():
hint += ' '
else:
contents += s
if len(trigger) and len(hint):
completions.append((trigger, hint, contents, doc))
return completions
def do_get_name(self):
return _('clang completion')
def do_match(self, context):
return self._can_complete(context)
def _get_cached_completions(self, context, token):
if self.completions:
doc = self._get_doc()
line, column = self._get_pos(self._get_buffer(context))
if doc == self.doc and line == self.line and \
len(token) > 0 and token.startswith(self.token):
return self.completions
self.doc = doc
self.line = line
self.token = token
self.completions = self._get_completions(context, token)
return self.completions
def do_populate(self, context):
token = self._get_token(context)
proposals = []
if token != None:
completions = self._get_cached_completions(context, token)
for (trigger, hint, contents, doc) in completions:
if len(token) == 0 or trigger.startswith(token):
item = GtkSource.CompletionItem.new(hint, contents, None, doc)
proposals.append(item)
if len(proposals) > self.max_proposals:
break
context.add_proposals(self, proposals, True)
def do_get_activation(self):
return GtkSource.CompletionActivation.INTERACTIVE # USER_REQUESTED
def do_activate_proposal(self, proposal, end):
token = self._get_token_before_iter(end)
if token:
buf = end.get_buffer()
start = buf.get_iter_at_offset(end.get_offset())
start.set_line_offset(end.get_line_offset() - len(token))
buf.delete(start, end)
text = proposal.get_text()
buf.insert(end, text)
if text[-1] == ')' and '(' in text:
start = buf.get_iter_at_offset(end.get_offset())
start.set_line_offset(end.get_line_offset() -
len(text) + text.index('(') + 1)
buf.select_range(start, end)
return True
return False
def do_get_priority(self):
return 100
class ClangCompletion(GObject.Object, Gedit.WindowActivatable):
__gtype_name__ = 'ClangCompletion'
window = GObject.property(type=Gedit.Window)
providers = {}
def __init__(self):
GObject.Object.__init__(self)
def do_update_state(self):
for view in self.window.get_views():
if view not in self.providers:
provider = ClangCompletionProvider(self.window)
view.get_completion().add_provider(provider)
self.providers[view] = provider
for view, provider in self.providers.items():
if view not in self.window.get_views():
if provider in view.get_completion().get_providers():
view.get_completion().remove_provider(provider)
del self.providers[view]
break
def do_activate(self):
self.do_update_state()
def do_deactivate(self):
self.do_update_state()