blob: faad718a552aca08bdcf8f0c02ebf797b2707094 [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 Moolenaardd2436f2005-09-05 22:14:46 +00004" Last Change: 2005 Sep 05
Bram Moolenaare344bea2005-09-01 20:46:49 +00005
6function! ccomplete#Complete(findstart, base)
7 if a:findstart
8 " locate the start of the word
9 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
23 " return list of matches
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024 if a:base !~ '\.\|->'
Bram Moolenaare344bea2005-09-01 20:46:49 +000025 " Only one part, no "." or "->": complete from tags file.
Bram Moolenaardd2436f2005-09-05 22:14:46 +000026 let diclist = taglist(a:base)
Bram Moolenaare344bea2005-09-01 20:46:49 +000027 return map(diclist, 'v:val["name"]')
28 endif
Bram Moolenaardd2436f2005-09-05 22:14:46 +000029
30 " Find variable locally in function or file.
31 let items = split(a:base, '\.\|->')
32
33 " At the moment we only do "aa.bb", not "aa.bb.cc"
34 if len(items) > 2
35 return []
36 endif
37
38 let line = ''
39 if searchdecl(items[0]) == 0 || searchdecl(items[0], 1) == 0
40 " Found, now figure out the type.
41 " TODO: join previous line if it makes sense
42 let line = getline('.')
43 let col = col('.')
44 else
45 " Find the variable in the tags file
46 let diclist = taglist(items[0])
47 for i in range(len(diclist))
48 " For now we only recognize a variable.
49 if diclist[i]['kind'] == 'v'
50 let line = diclist[i]['cmd']
51 if line[0] == '/' && line[1] == '^'
52 " the command is a search pattern, remove the leading /^
53 let line = strpart(line, 2)
54 endif
55 let col = match(line, items[0])
56 break
57 endif
58 endfor
59 endif
60
61 if line == ''
62 return []
63 endif
64
65 " Is there a * before the variable name?
66 let col -= 1
67 let star = 0
68 while col > 0
69 let col -= 1
70 if line[col] == '*'
71 let star = 1
72 elseif line[col] !~ '\s'
73 break
74 endif
75 endwhile
76
77 " Use the line up to the variable name and split it in tokens.
78 let lead = strpart(line, 0, col + 1)
79 let tokens = split(lead, '\s\+\|\<')
80
81 let basetext = matchstr(a:base, '.*\.\|->')
82
83 for i in range(len(tokens) - 1)
84 if tokens[i] == 'struct'
85 let name = tokens[i + 1]
86 " Todo: Use all tags files; What about local structures?
87 exe 'vimgrep /\<struct:' . name . '\>/j tags'
88 let res = []
89 for l in getqflist()
90 let memb = matchstr(l['text'], '[^\t]*')
91 if len(items) == 1 || memb =~ '^' . items[1]
92 call add(res, basetext . memb)
93 endif
94 endfor
95 return res
96 endif
97 endfor
98
99 return tokens
Bram Moolenaare344bea2005-09-01 20:46:49 +0000100endfunction
101