-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcscope.vim
288 lines (249 loc) · 8.09 KB
/
cscope.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
let s:trigger = has("nvim") ? "<C-Space>" : "<C-@>"
if !has('cscope')
" Nvim 0.9 removed cscope entirely.
exe printf("nnoremap %s :echoerr 'cscope is not supported'<CR>", s:trigger)
finish
endif
" Script functions {{
function! s:add_cscope(...) abort
if !executable('cscope')
echo "cscope not found"
return ''
elseif a:0 < 1
echo ":CscopeAdd <file> [<pre-path>=file:p:h] [<flags>=-ia]"
return ''
endif
" Pull arguments out into variables.
let file = fnamemodify(expand(a:1), ':p')
let prepath = expand(get(a:000, 1, fnamemodify(file, ':h')))
let flags = get(a:000, 2, '-ia')
" Validate arguments.
if !filereadable(file)
echo "File not readable:" file
return ''
elseif prepath[0] == '-'
" Second argument is flags.
let flags = prepath
let prepath = fnamemodify(file, ':p:h')
elseif !isdirectory(prepath)
echo "Not a directory:" prepath
return ''
endif
if cscope_connection(2, file)
echo "You wanted to add:"
echo repeat(' ', 11) . join(a:000)
echo "Seems to already be a connection to that cscope database?"
echo ""
cscope show
else
let save_csverb=&cscopeverbose
let save_csprg = &cscopeprg
set nocscopeverbose cscopeprg=cscope
try
execute printf("cscope add %s %s %s", file, prepath, flags)
echo "cscope database opened:" file
finally
let &csverb=save_csverb
let &cscopeprg = save_csprg
endtry
endif
endfunction
function! s:add_gtags(...) abort
if !executable('gtags-cscope')
echo "gtags-cscope not found"
return ''
elseif a:0 < 1
echo ":GtagsAdd <file> [<pre-path>=file:p:h [<flags>=-ia]]"
return ''
endif
" Pull arguments out into variables.
let file = fnamemodify(expand(a:1), ':p')
let prepath = expand(get(a:000, 1, fnamemodify(file, ':h')))
let flags = get(a:000, 2, '-ia')
" Validate arguments.
if !filereadable(file)
echo "File not readable:" file
return ''
elseif prepath[0] == '-'
" Second argument is flags.
let flags = prepath
let prepath = fnamemodify(file, ':p:h')
elseif !isdirectory(prepath)
echo "Not a directory:" prepath
return ''
endif
if cscope_connection(2, file)
echo "You wanted to add:"
echo repeat(' ', 11) . join(a:000)
echo "Seems to already be a connection to that database?"
echo ""
cscope show
else
let save_csprg = &cscopeprg
let save_gtr = $GTAGSROOT
let save_gdp = $GTAGSDBPATH
let save_csverb = &cscopeverbose
set nocscopeverbose cscopeprg=gtags-cscope
let $GTAGSROOT = prepath
let $GTAGSDBPATH = prepath
try
execute printf("cscope add %s %s %s", file, prepath, flags)
echo "gtags-cscope database opened:" file
finally
let $GTAGSROOT = save_gtr
let $GTAGSDBPATH = save_gdp
let &csverb = save_csverb
let &cscopeprg = save_csprg
endtry
endif
endfunction
function! s:resolve_git_root(git_dir)
let answer = a:git_dir
" Since the git repository may be a submodule, use rev-parse to get the
" working directory.
let cmd = printf('git --git-dir=%s rev-parse --show-toplevel', a:git_dir)
let rsp = system(cmd)
if v:shell_error
echo printf("git rev-parse errored with %d\n%s", v:shell_error, rsp)
" Hack for tests - instead of erroring, strip what we assume is .git at
" the end of the b:git_dir variable.
return fnamemodify(b:git_dir, ':h')
endif
return substitute(rsp, '\n', '', '')
endfunction
function! cscope#detect(...)
" One optional argument, to override b:git_dir check
if a:0 > 0
if !isdirectory(a:1)
echo printf("cscope#detect(%s): Not a directory", shellescape(a:1))
return 0
else
let root = a:1
endif
else
" No arguments passed
if !exists('b:git_dir')
return 0
else
let root = s:resolve_git_root(b:git_dir)
endif
endif
let silent = get(g:, 'cscope_detect_silent', 1) ? 'silent! ' : ''
if executable('cscope')
if filereadable(root . '/cscope.out')
execute printf("%sCscopeAdd %s/cscope.out %s", silent, root, root)
elseif exists('b:git_dir') && filereadable(b:git_dir . '/cscope') " git hooks based
execute printf("%sCscopeAdd %s/.git/cscope %s", silent, b:git_dir, root)
endif
endif
if executable('gtags-cscope') && filereadable(root . '/GTAGS')
execute printf("%sGtagsAdd %s/GTAGS", silent, root)
endif
return 1
endfunction
function! s:do_cs_find(key)
let cw = expand('<cword>')
let cf = expand('<cfile>')
if cw == '' && cf == ''
echo "Nothing under cursor"
return
endif
let csfindargs = {
\'s': cw,
\'g': cw,
\'t': cw,
\'c': cw,
\'d': cw,
\'e': cw,
\'f': cf,
\'i': cf
\}
if !has_key(csfindargs, a:key)
echo "cscope.vim: do_cs_find missing key:" a:key
return ''
endif
let arg = csfindargs[a:key]
try
execute printf("cscope find %s %s", a:key, arg)
catch
echo v:exception
endtry
endfunction
function! s:prompt()
if expand('<cword>') == '' && expand('<cfile>') == ''
echo "Nothing under cursor"
return
endif
let handlekeys = "sgtcdefi"
let prompt = "&symbol\n&definition\n&text\n&calls\ncalle&d by this\n&egrep\ngo to &file\n&includes\n& cancel"
let which = confirm("cscope search type?", prompt, 0)
if which && which <= strlen(handlekeys)
call s:do_cs_find(handlekeys[which - 1])
endif
endfunction
function! s:generate(directory, tagkind, cmd) abort
let directory = a:directory
if directory == ''
if !exists("b:git_dir")
return 0
endif
let directory = s:resolve_git_root(b:git_dir)
endif
if !isdirectory(directory)
echoerr "Not a directory"
return 0
endif
echo "Working on it..."
let savecwd = getcwd()
execute printf("chdir %s", escape(directory, ' '))
echo system(printf("%s 2>&1 >/dev/null", a:cmd))
execute printf("chdir %s", escape(savecwd, ' '))
redraw
echo printf("%s generation complete!", a:tagkind)
return 1
endfunction
" }}
" TODO: Intelligent completion based on which arg you're doing?
" Like this? http://vi.stackexchange.com/a/6696
if executable('cscope')
function! cscope#gen_cscope(...) abort
return s:generate(get(a:000, 0, ''), 'cscope', 'cscope -Rbq -f cscope.out')
endfunction
command! -nargs=* -complete=file CscopeAdd call s:add_cscope(<f-args>)
command! -nargs=? -complete=dir CscopeGen call cscope#gen_cscope(<f-args>) | call cscope#detect(<f-args>)
endif
if executable('gtags-cscope')
function! cscope#gen_gtags(...) abort
return s:generate(get(a:000, 0, ''), 'gtags', 'gtags --gtagslabel ctags')
endfunction
command! -nargs=* -complete=file GtagsAdd call s:add_gtags(<f-args>)
command! -nargs=? -complete=dir GtagsGen call cscope#gen_gtags(<f-args>) | call cscope#detect(<f-args>)
endif
" FIXME hack to work around plugin load order
if get(g:, 'cscope_has_fugitive', 1)
function! s:fugitive_boot()
augroup cscopeDetect
autocmd!
autocmd User Fugitive silent call cscope#detect()
augroup END
endfunction
autocmd! User FugitiveBoot call s:fugitive_boot()
endif
" Key bindings {{
nnoremap <silent> <Plug>cscopePrompt :call <SID>prompt()<CR>
" Launch prompt
exe printf("nmap %s<Space> <Plug>cscopePrompt", s:trigger)
exe printf("nmap %s%s <Plug>cscopePrompt", s:trigger, s:trigger)
exe printf("nmap %s? <Plug>cscopePrompt", s:trigger)
exe printf("nmap %s<C-?> <Plug>cscopePrompt", s:trigger)
" Go right to a query operation
for s:c in ['s', 'g', 't', 'c', 'd', 'e', 'f', 'i']
let s:nr = char2nr(s:c)
execute printf("nnoremap <silent> %s%c :call <SID>do_cs_find('%c')<CR>", s:trigger, s:nr, s:nr)
unlet s:c
unlet s:nr
endfor
set nocscopeverbose
" Use both cscope and ctags for 'ctrl-]', ':ta', and 'vim -t'
set cscopetag
" }}