forked from mbbill/code_complete
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_complete.vim
More file actions
277 lines (253 loc) · 8.99 KB
/
code_complete.vim
File metadata and controls
277 lines (253 loc) · 8.99 KB
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
"==================================================
" File: code_complete.vim
" Brief: function parameter complete, code snippets, and much more.
" Author: Mingbai <mbbill AT gmail DOT com>
" Last Change: 2009-06-09 00:09:03
" Version: 2.9
"
" Install: 1. Put code_complete.vim to plugin
" directory.
" 2. Use the command below to create tags
" file including signature field.
" ctags -R --c-kinds=+p --fields=+S .
"
" Usage:
" hotkey:
" "<tab>" (default value of g:completekey)
" Do all the jobs with this key, see
" example:
" press <tab> after function name and (
" foo ( <tab>
" becomes:
" foo ( `<first param>`,`<second param>` )
" press <tab> after code template
" if <tab>
" becomes:
" if( `<...>` )
" {
" `<...>`
" }
"
"
" variables:
"
" g:disable_codecomplete
" Disable code_complete, default enabled.
"
" g:completekey
" the key used to complete function
" parameters and key words.
"
" g:rs, g:re
" region start and stop
" you can change them as you like.
"
" g:user_defined_snippets
" file name of user defined snippets.
"
" key words:
" see "templates" section.
"==================================================
if v:version < 700
finish
endif
if exists("g:disable_codecomplete")
finish
endif
" Variable Definitions: {{{1
" options, define them as you like in vimrc:
if !exists("g:completekey")
let g:completekey = "<tab>" "hotkey
endif
if !exists("g:rs")
let g:rs = '`<' "region start
endif
if !exists("g:re")
let g:re = '>`' "region stop
endif
if !exists("g:user_defined_snippets")
let g:user_defined_snippets = "$VIMRUNTIME/plugin/my_snippets.vim"
endif
" ----------------------------
let s:expanded = 0 "in case of inserting char after expand
let s:signature_list = []
let s:jumppos = -1
let s:doappend = 1
" Autocommands: {{{1
autocmd BufReadPost,BufNewFile * call CodeCompleteStart()
" Menus:
menu <silent> &Tools.Code\ Complete\ Start :call CodeCompleteStart()<CR>
menu <silent> &Tools.Code\ Complete\ Stop :call CodeCompleteStop()<CR>
" Function Definitions: {{{1
function! CodeCompleteStart()
exec "silent! iunmap <buffer> ".g:completekey
exec "inoremap <buffer> ".g:completekey." <c-r>=CodeComplete()<cr><c-r>=SwitchRegion()<cr>"
endfunction
function! CodeCompleteStop()
exec "silent! iunmap <buffer> ".g:completekey
endfunction
function! FunctionComplete(fun)
let s:signature_list=[]
let signature_word=[]
let ftags=taglist("^".a:fun."$")
if type(ftags)==type(0) || ((type(ftags)==type([])) && ftags==[])
return ''
endif
let tmp=''
for i in ftags
if match(i.cmd,'^/\^.*\(\*'.a:fun.'\)\(.*\)\;\$/')>=0
if match(i.cmd,'(\s*void\s*)')<0 && match(i.cmd,'(\s*)')<0
let tmp=substitute(i.cmd,'^/\^','','')
let tmp=substitute(tmp,'.*\(\*'.a:fun.'\)','','')
let tmp=substitute(tmp,'^[\){1}]','','')
let tmp=substitute(tmp,';\$\/;{1}','','')
let tmp=substitute(tmp,'\$\/','','')
let tmp=substitute(tmp,';','','')
let tmp=substitute(tmp,',',g:re.','.g:rs,'g')
let tmp=substitute(tmp,'(\(.*\))',g:rs.'\1'.g:re.')','g')
else
let tmp=''
endif
if (tmp != '') && (index(signature_word,tmp) == -1)
let signature_word+=[tmp]
let item={}
let item['word']=tmp
let item['menu']=i.filename
let s:signature_list+=[item]
endif
endif
if has_key(i,'kind') && has_key(i,'name') && has_key(i,'signature')
if (i.kind=='p' || i.kind=='f') && i.name==a:fun " p is declare, f is definition
if match(i.signature,'(\s*void\s*)')<0 && match(i.signature,'(\s*)')<0
let tmp=substitute(i.signature,',',g:re.','.g:rs,'g')
let tmp=substitute(tmp,'(\(.*\))',g:rs.'\1'.g:re.')','g')
else
let tmp=''
endif
if (tmp != '') && (index(signature_word,tmp) == -1)
let signature_word+=[tmp]
let item={}
let item['word']=tmp
let item['menu']=i.filename
let s:signature_list+=[item]
endif
endif
endif
endfor
if s:signature_list==[]
return ')'
endif
if len(s:signature_list)==1
return s:signature_list[0]['word']
else
call complete(col('.'),s:signature_list)
return ''
endif
endfunction
function! ExpandTemplate(cword)
"let cword = substitute(getline('.')[:(col('.')-2)],'\zs.*\W\ze\w*$','','g')
if has_key(g:template,&ft)
if has_key(g:template[&ft],a:cword)
let s:jumppos = line('.')
return "\<c-w>" . g:template[&ft][a:cword]
endif
endif
if has_key(g:template['_'],a:cword)
let s:jumppos = line('.')
return "\<c-w>" . g:template['_'][a:cword]
endif
return ''
endfunction
function! SwitchRegion()
if len(s:signature_list)>1
let s:signature_list=[]
return ''
endif
if s:jumppos != -1
call cursor(s:jumppos,0)
let s:jumppos = -1
endif
if match(getline('.'),g:rs.'.*'.g:re)!=-1 || search(g:rs.'.\{-}'.g:re)!=0
normal 0
call search(g:rs,'c',line('.'))
normal v
call search(g:re,'e',line('.'))
if &selection == "exclusive"
exec "norm l"
endif
return "\<c-\>\<c-n>gvo\<c-g>"
else
if s:doappend == 1
if g:completekey == "<tab>"
return "\<tab>"
endif
endif
return ''
endif
endfunction
function! CodeComplete()
let s:doappend = 1
let function_name = matchstr(getline('.')[:(col('.')-2)],'\zs\w*\ze\s*(\s*$')
if function_name != ''
let funcres = FunctionComplete(function_name)
if funcres != ''
let s:doappend = 0
endif
return funcres
else
let template_name = substitute(getline('.')[:(col('.')-2)],'\zs.*\W\ze\w*$','','g')
let tempres = ExpandTemplate(template_name)
if tempres != ''
let s:doappend = 0
endif
return tempres
endif
endfunction
" [Get converted file name like __THIS_FILE__ ]
function! GetFileName()
let filename=expand("%:t")
let filename=toupper(filename)
let _name=substitute(filename,'\.','_',"g")
"let _name="__"._name."__"
return _name
endfunction
" Templates: {{{1
" to add templates for new file type, see below
"
" "some new file type
" let g:template['newft'] = {}
" let g:template['newft']['keyword'] = "some abbrevation"
" let g:template['newft']['anotherkeyword'] = "another abbrevation"
" ...
"
" ---------------------------------------------
" C templates
let g:template = {}
let g:template['c'] = {}
let g:template['c']['cc'] = "/* */\<left>\<left>\<left>"
let g:template['c']['cd'] = "/**< */\<left>\<left>\<left>"
let g:template['c']['de'] = "#define "
let g:template['c']['in'] = "#include \"\"\<left>"
let g:template['c']['is'] = "#include <>\<left>"
let g:template['c']['ff'] = "#ifndef \<c-r>=GetFileName()\<cr>\<CR>#define \<c-r>=GetFileName()\<cr>".
\repeat("\<cr>",5)."#endif /*\<c-r>=GetFileName()\<cr>*/".repeat("\<up>",3)
let g:template['c']['for'] = "for( ".g:rs."...".g:re." ; ".g:rs."...".g:re." ; ".g:rs."...".g:re." )\<cr>{\<cr>".
\g:rs."...".g:re."\<cr>}\<cr>"
let g:template['c']['main'] = "int main(int argc, char \*argv\[\])\<cr>{\<cr>".g:rs."...".g:re."\<cr>}"
let g:template['c']['switch'] = "switch ( ".g:rs."...".g:re." )\<cr>{\<cr>case ".g:rs."...".g:re." :\<cr>break;\<cr>case ".
\g:rs."...".g:re." :\<cr>break;\<cr>default :\<cr>break;\<cr>}"
let g:template['c']['if'] = "if( ".g:rs."...".g:re." )\<cr>{\<cr>".g:rs."...".g:re."\<cr>}"
let g:template['c']['while'] = "while( ".g:rs."...".g:re." )\<cr>{\<cr>".g:rs."...".g:re."\<cr>}"
let g:template['c']['ife'] = "if( ".g:rs."...".g:re." )\<cr>{\<cr>".g:rs."...".g:re."\<cr>} else\<cr>{\<cr>".g:rs."...".
\g:re."\<cr>}"
" ---------------------------------------------
" C++ templates
let g:template['cpp'] = g:template['c']
" ---------------------------------------------
" common templates
let g:template['_'] = {}
let g:template['_']['xt'] = "\<c-r>=strftime(\"%Y-%m-%d %H:%M:%S\")\<cr>"
" ---------------------------------------------
" load user defined snippets
exec "silent! runtime ".g:user_defined_snippets
" vim: set fdm=marker et :