/
clang_complete.vim
382 lines (329 loc) · 11.7 KB
/
clang_complete.vim
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
" original: http://github.com/Rip-Rip/clang_complete
" File: clang_complete.vim
" Author: Xavier Deguillard <deguilx@gmail.com>
" Modified by: eagletmt <eagletmt@gmail.com>
" Shougo Matsushita <Shougo.Matsu At gmail.com>
"
" Description: Use of clang to complete in C/C++.
"
" Configuration: Each project can have a .clang_complete at his root,
" containing the compiler options. This is useful if
" you're using some non-standard include paths.
" For simplicity, please don't put relative and
" absolute include path on the same line. It is not
" currently correctly handled.
"
" Todo: - Fix bugs
" - Add snippets on Pattern and OVERLOAD (is it possible?)
"
" Variables initialize.
if !exists('g:neocomplcache_clang_use_library')
let g:neocomplcache_clang_use_library = 0
endif
if !exists('g:neocomplcache_clang_macros')
let g:neocomplcache_clang_macros = 0
endif
if !exists('g:neocomplcache_clang_patterns')
let g:neocomplcache_clang_patterns = 0
endif
if !exists('g:neocomplcache_clang_auto_options')
let g:neocomplcache_clang_auto_options = 'path, .clang_complete'
endif
if !exists('g:neocomplcache_clang_user_options')
let g:neocomplcache_clang_user_options = ''
endif
if !exists('g:neocomplcache_clang_debug')
let g:neocomplcache_clang_debug = 0
endif
let s:source = {
\ 'name': 'clang_complete',
\ 'kind': 'ftplugin',
\ 'filetypes': { 'c': 1, 'cpp': 1, 'objc': 1, 'objcpp': 1 },
\ }
" Store plugin path, as this is available only when sourcing the file,
" not during a function call.
let s:plugin_path = escape(expand('<sfile>:p:h'), '\')
function! s:init_ClangCompletePython()
python import sys
if exists('g:neocomplcache_clang_library_path')
" Load the library from the given library path.
execute 'python sys.argv = ["' . escape(g:neocomplcache_clang_library_path, '\') . '"]'
else
" By setting argv[0] to '' force the python bindings to load the library
" from the normal system search path.
python sys.argv[0] = ''
endif
execute 'python sys.path = ["' . s:plugin_path . '/clang_complete"] + sys.path'
execute 'pyfile ' . s:plugin_path . '/clang_complete/libclang.py'
python initClangComplete(vim.eval('g:neocomplcache_clang_lib_flags'))
endfunction
function! s:init_ClangComplete()
let b:should_overload = 0
call s:loadUserOptions()
let b:clang_exec = 'clang'
let b:clang_parameters = '-x c'
if &filetype == 'objc'
let b:clang_parameters = '-x objective-c'
endif
if &filetype == 'cpp' || &filetype == 'objcpp'
let b:clang_parameters .= '++'
endif
if expand('%:e') =~ 'h*'
let b:clang_parameters .= '-header'
endif
let g:neocomplcache_clang_lib_flags = 0
if g:neocomplcache_clang_macros
let b:clang_parameters .= ' -code-completion-macros'
let g:neocomplcache_clang_lib_flags = 1
endif
if g:neocomplcache_clang_patterns
let b:clang_parameters .= ' -code-completion-patterns'
let g:clang_complete_lib_flags += 2
endif
" Load the python bindings of libclang.
if g:neocomplcache_clang_use_library
if has('python')
call s:init_ClangCompletePython()
else
echoe 'clang_complete: No python support available.'
echoe 'Cannot use clang library, using executable'
echoe 'Compile vim with python support to use libclang'
let g:neocomplcache_clang_use_library = 0
return
endif
endif
endfunction
function! s:loadUserOptions()
let b:clang_user_options = ''
let l:option_sources = split(g:neocomplcache_clang_auto_options, ',')
let l:remove_spaces_cmd = 'substitute(v:val, "\\s*\\(.*\\)\\s*", "\\1", "")'
let l:option_sources = map(l:option_sources, l:remove_spaces_cmd)
for l:source in l:option_sources
if l:source == 'path'
call s:parsePathOption()
elseif l:source == '.clang_complete'
call s:parseConfig()
endif
endfor
endfunction
function! s:parseConfig()
let l:local_conf = findfile('.clang_complete', '.;')
if l:local_conf == '' || !filereadable(l:local_conf)
return
endif
let l:opts = readfile(l:local_conf)
for l:opt in l:opts
" Better handling of absolute path
" I don't know if those pattern will work on windows
" platform
if matchstr(l:opt, '\C-I\s*/') != ''
let l:opt = substitute(l:opt, '\C-I\s*\(/\%(\w\|\\\s\)*\)',
\ '-I' . '\1', 'g')
else
let l:opt = substitute(l:opt, '\C-I\s*\(\%(\w\|\\\s\)*\)',
\ '-I' . l:local_conf[:-16] . '\1', 'g')
endif
let b:clang_user_options .= ' ' . l:opt
endfor
endfunction
function! s:parsePathOption()
let l:dirs = split(&path, ',')
for l:dir in l:dirs
if len(l:dir) == 0 || !isdirectory(l:dir)
continue
endif
" Add only absolute paths
if matchstr(l:dir, '\s*/') != ''
let l:opt = '-I' . l:dir
let b:clang_user_options .= ' ' . l:opt
endif
endfor
endfunction
function! s:get_kind(proto)
if a:proto == ""
return 't'
endif
let l:ret = match(a:proto, '^\[#')
let l:params = match(a:proto, '(')
if l:ret == -1 && l:params == -1
return 't'
endif
if l:ret != -1 && l:params == -1
return 'v'
endif
if l:params != -1
return 'f'
endif
return 'm'
endfunction
function! s:source.initialize()
autocmd neocomplcache FileType c,cpp,objc,objcpp call s:init_ClangComplete()
if &l:filetype == 'c' || &l:filetype == 'cpp' || &l:filetype == 'objc' || &l:filetype == 'objcpp'
call s:init_ClangComplete()
endif
call neocomplcache#set_completion_length('clang_complete', 0)
endfunction
function! s:source.finalize()
endfunction
function! s:ClangQuickFix(clang_output)
let l:list = []
for l:line in a:clang_output
let l:erridx = stridx(l:line, "error:")
if l:erridx == -1
continue
endif
let l:bufnr = bufnr("%")
let l:pattern = '\.*:\(\d*\):\(\d*\):'
let tmp = matchstr(l:line, l:pattern)
let l:lnum = substitute(tmp, l:pattern, '\1', '')
let l:col = substitute(tmp, l:pattern, '\2', '')
let l:text = l:line
let l:type = 'E'
let l:item = {
\ "bufnr": l:bufnr,
\ "lnum": l:lnum,
\ "col": l:col,
\ "text": l:text[l:erridx + 7:],
\ "type": l:type }
let l:list = add(l:list, l:item)
endfor
call setqflist(l:list)
endfunction
function! s:DemangleProto(prototype)
let l:proto = substitute(a:prototype, '[#', '', 'g')
let l:proto = substitute(l:proto, '#]', ' ', 'g')
let l:proto = substitute(l:proto, '#>', '', 'g')
let l:proto = substitute(l:proto, '<#', '', 'g')
" TODO: add a candidate for each optional parameter
let l:proto = substitute(l:proto, '{#', '', 'g')
let l:proto = substitute(l:proto, '#}', '', 'g')
return l:proto
endfunction
function! s:source.get_keyword_pos(cur_text)
if neocomplcache#is_auto_complete()
\ && a:cur_text !~ '\%(->\|\.\|::\)$'
" auto complete is very slow!
return -1
endif
let l:line = getline('.')
let l:start = col('.') - 1
let l:wsstart = l:start
if l:line[l:wsstart - 1] =~ '\s'
while l:wsstart > 0 && l:line[l:wsstart - 1] =~ '\s'
let l:wsstart -= 1
endwhile
endif
if l:line[l:wsstart - 1] =~ '[(,]'
let b:should_overload = 1
return l:wsstart
endif
let b:should_overload = 0
while l:start > 0 && l:line[l:start - 1] =~ '\i'
let l:start -= 1
endwhile
return l:start
endfunction
function! s:source.get_complete_words(cur_keyword_pos, cur_keyword_str)
if g:neocomplcache_clang_use_library
python vim.command('let l:clang_output = ' + str(getCurrentCompletions(vim.eval('a:cur_keyword_str'), int(vim.eval('a:cur_keyword_pos+1')))))
" echomsg string(l:clang_output)
else
let l:clang_output = s:complete_from_clang_binary(a:cur_keyword_pos, a:cur_keyword_str)
endif
return l:clang_output
endfunction
function! s:complete_from_clang_binary(cur_keyword_pos, cur_keyword_str)
let l:buf = getline(1, '$')
let l:tempfile = expand('%:p:h') . '/' . localtime() . expand('%:t')
if neocomplcache#is_win()
let l:tempfile = substitute(l:tempfile, '\\', '/', 'g')
endif
call writefile(l:buf, l:tempfile)
let l:escaped_tempfile = shellescape(l:tempfile)
let l:command = b:clang_exec . ' -cc1 -fsyntax-only'
\ . ' -fno-caret-diagnostics -fdiagnostics-print-source-range-info'
\ . ' -code-completion-at='
\ . l:escaped_tempfile . ":" . line('.') . ":" . (a:cur_keyword_pos+1)
\ . ' ' . l:escaped_tempfile
\ . ' ' . b:clang_parameters . ' ' . b:clang_user_options . ' ' . g:neocomplcache_clang_user_options
let l:clang_output = split(neocomplcache#system(l:command), '\n')
call delete(l:tempfile)
let l:filter_str = "v:val =~ '^COMPLETION: " . a:cur_keyword_str . "\\|^OVERLOAD: '"
call filter(l:clang_output, l:filter_str)
let l:res = []
for l:line in l:clang_output
if l:line[:11] == 'COMPLETION: ' && b:should_overload != 1
let l:value = l:line[12:]
let l:colonidx = stridx(l:value, ' : ')
if l:colonidx == -1
let l:wabbr = s:DemangleProto(l:value)
let l:word = l:value
let l:proto = l:value
else
let l:word = l:value[:l:colonidx - 1]
" WTF is that?
if l:word =~ '(Hidden)'
let l:word = l:word[:-10]
endif
let l:wabbr = l:word
let l:proto = l:value[l:colonidx + 3:]
endif
let l:kind = s:GetKind(l:proto)
if l:kind == 't' && getline('.') =~ '\%(->\|\.\|::\)$'
continue
endif
let l:word = l:wabbr
let l:proto = s:DemangleProto(l:proto)
elseif l:line[:9] == 'OVERLOAD: ' && b:should_overload == 1
let l:value = l:line[10:]
if match(l:value, '<#') == -1
continue
endif
let l:word = substitute(l:value, '.*<#', '<#', 'g')
let l:word = substitute(l:word, '#>.*', '#>', 'g')
let l:wabbr = substitute(l:word, '<#\([^#]*\)#>', '\1', 'g')
let l:proto = s:DemangleProto(l:value)
let l:kind = ''
else
continue
endif
let l:item = {
\ 'word': l:word,
\ 'abbr': l:wabbr,
\ 'menu': l:proto,
\ 'info': l:proto,
\ 'dup': 1,
\ 'kind': l:kind }
call add(l:res, l:item)
endfor
return l:res
endfunction
function! s:GetKind(proto)
if a:proto == ''
return 't'
endif
let l:ret = match(a:proto, '^\[#')
let l:params = match(a:proto, '(')
if l:ret == -1 && l:params == -1
return 't'
endif
if l:ret != -1 && l:params == -1
return 'v'
endif
if l:params != -1
return 'f'
endif
return 'm'
endfunction
function! s:DemangleProto(prototype)
let l:proto = substitute(a:prototype, '[#', '', 'g')
let l:proto = substitute(l:proto, '#]', ' ', 'g')
let l:proto = substitute(l:proto, '#>', '', 'g')
let l:proto = substitute(l:proto, '<#', '', 'g')
let l:proto = substitute(l:proto, '{#.*#}', '', 'g')
return l:proto
endfunction
function! neocomplcache#sources#clang_complete#define()
return s:source
endfunction
" vim: expandtab:ts=4:sts=4:sw=4