blob: 2d54ea6d2d683a26001f2e1d92abf4d9f7e85c2f [file] [log] [blame]
Bram Moolenaare344bea2005-09-01 20:46:49 +00001" Vim completion script
2" Language: C
3" Maintainer: Bram Moolenaar <Bram@vim.org>
Bram Moolenaarcaa0fcf2005-09-07 21:21:14 +00004" Last Change: 2005 Sep 07
Bram Moolenaare344bea2005-09-01 20:46:49 +00005
6function! ccomplete#Complete(findstart, base)
7 if a:findstart
Bram Moolenaarcaa0fcf2005-09-07 21:21:14 +00008 " Locate the start of the item, including "." and "->".
Bram Moolenaare344bea2005-09-01 20:46:49 +00009 let line = getline('.')
10 let start = col('.') - 1
11 while start > 0
12 if line[start - 1] =~ '\w\|\.'
13 let start -= 1
14 elseif start > 1 && line[start - 2] == '-' && line[start - 1] == '>'
15 let start -= 2
16 else
17 break
18 endif
19 endwhile
20 return start
21 endif
22
Bram Moolenaarcaa0fcf2005-09-07 21:21:14 +000023 " Return list of matches.
24
25 " Split item in words, keep empty word after "." or "->".
26 " "aa" -> ['aa'], "aa." -> ['aa', ''], "aa.bb" -> ['aa', 'bb'], etc.
27 let items = split(a:base, '\.\|->', 1)
28 if len(items) <= 1
Bram Moolenaare344bea2005-09-01 20:46:49 +000029 " Only one part, no "." or "->": complete from tags file.
Bram Moolenaarcaa0fcf2005-09-07 21:21:14 +000030 " When local completion is wanted CTRL-N would have been used.
31 return map(taglist('^' . a:base), 'v:val["name"]')
Bram Moolenaare344bea2005-09-01 20:46:49 +000032 endif
Bram Moolenaardd2436f2005-09-05 22:14:46 +000033
Bram Moolenaarcaa0fcf2005-09-07 21:21:14 +000034 let basetext = matchstr(a:base, '.*\(\.\|->\)')
Bram Moolenaardd2436f2005-09-05 22:14:46 +000035
Bram Moolenaarcaa0fcf2005-09-07 21:21:14 +000036 " Find variable locally in current function, current file or tags file.
Bram Moolenaardd2436f2005-09-05 22:14:46 +000037 if searchdecl(items[0]) == 0 || searchdecl(items[0], 1) == 0
38 " Found, now figure out the type.
39 " TODO: join previous line if it makes sense
40 let line = getline('.')
41 let col = col('.')
Bram Moolenaarcaa0fcf2005-09-07 21:21:14 +000042 let res = ccomplete#Nextitem(strpart(line, 0, col), items[1:], basetext)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000043 else
44 " Find the variable in the tags file
Bram Moolenaarcaa0fcf2005-09-07 21:21:14 +000045 let diclist = taglist('^' . items[0] . '$')
46
47 let res = []
Bram Moolenaardd2436f2005-09-05 22:14:46 +000048 for i in range(len(diclist))
49 " For now we only recognize a variable.
Bram Moolenaarcaa0fcf2005-09-07 21:21:14 +000050 " The command in the tags file must be a search pattern that shows the
51 " declaration of the variable.
Bram Moolenaardd2436f2005-09-05 22:14:46 +000052 if diclist[i]['kind'] == 'v'
53 let line = diclist[i]['cmd']
54 if line[0] == '/' && line[1] == '^'
Bram Moolenaarcaa0fcf2005-09-07 21:21:14 +000055 let line = strpart(line, 2) " Remove /^ from the cmd
56 let col = match(line, items[0])
57 call extend(res, ccomplete#Nextitem(strpart(line, 0, col), items[1:], basetext)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000058 endif
Bram Moolenaardd2436f2005-09-05 22:14:46 +000059 endif
60 endfor
61 endif
62
Bram Moolenaarcaa0fcf2005-09-07 21:21:14 +000063 return res
64endfunc
Bram Moolenaardd2436f2005-09-05 22:14:46 +000065
Bram Moolenaarcaa0fcf2005-09-07 21:21:14 +000066function! ccomplete#Nextitem(lead, items, basetext)
67
68 " Use the text up to the variable name and split it in tokens.
69 let tokens = split(a:lead, '\s\+\|\<')
70
71 " Try to recognize the type of the variable. This is rough guessing...
72 let members = []
73 let taglines = []
74 for tidx in range(len(tokens))
75
76 " Recognize 'struct foobar'.
77 if tokens[tidx] == 'struct' && tidx + 1 < len(tokens)
78 let [members, taglines] = ccomplete#StructMembers(tokens[tidx + 1], a:items[0])
Bram Moolenaardd2436f2005-09-05 22:14:46 +000079 break
80 endif
Bram Moolenaardd2436f2005-09-05 22:14:46 +000081
Bram Moolenaarcaa0fcf2005-09-07 21:21:14 +000082 " Recognize a typedef: 'foobar_t'.
83 let diclist = taglist('^' . tokens[tidx] . '$')
84 for i in range(len(diclist))
85 " For now we only recognize "typedef struct foobar".
86 " The command in the tags file must be a search pattern that shows the
87 " typedef.
88 let cmd = diclist[i]['cmd']
89 let ci = matchend(cmd, 'typedef\s\+struct\s\+')
90 if ci > 1
91 let name = matchstr(cmd, '\w*', ci)
92 let [m, l] = ccomplete#StructMembers(name, a:items[0])
93 call extend(members, m)
94 call extend(taglines, l)
95 endif
96 endfor
97 if len(members) > 0
98 break
Bram Moolenaardd2436f2005-09-05 22:14:46 +000099 endif
Bram Moolenaarcaa0fcf2005-09-07 21:21:14 +0000100
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000101 endfor
102
Bram Moolenaarcaa0fcf2005-09-07 21:21:14 +0000103 if len(members) > 0
104 if len(a:items) == 1
105 return map(members, 'a:basetext . v:val')
106 endif
107
108 " More items following. For each of the possible members find the
109 " matching following members.
110 let res = []
111 for i in range(len(members))
112 let line = taglines[i]
113 let memb = members[i]
114 let s = match(line, '\t\zs/^')
115 if s > 0
116 let e = match(line, members[i], s)
117 if e > 0
118 call extend(res, ccomplete#Nextitem(strpart(line, s, e - s), a:items[1:], a:basetext))
119 endif
120 endif
121 endfor
122 return res
123 endif
124
125 " Failed to find anything.
126 return []
Bram Moolenaare344bea2005-09-01 20:46:49 +0000127endfunction
128
Bram Moolenaarcaa0fcf2005-09-07 21:21:14 +0000129
130" Return a list with two lists:
131" - a list of members of structure "name" starting with string "item".
132" - a list of the tag lines where the member is defined.
133function! ccomplete#StructMembers(name, item)
134 " Todo: Use all tags files; What about local structures?
135 exe 'vimgrep /\<struct:' . a:name . '\>/j tags'
136
137 let members = []
138 let taglines = []
139 for l in getqflist()
140 let memb = matchstr(l['text'], '[^\t]*')
141 if memb =~ '^' . a:item
142 call add(members, memb)
143 call add(taglines, l['text'])
144 endif
145 endfor
146 return [members, taglines]
147endfunction