26779
|
1 vim9script noclear
|
502
|
2
|
26779
|
3 # Vim completion script
|
|
4 # Language: C
|
|
5 # Maintainer: Bram Moolenaar <Bram@vim.org>
|
|
6 # Rewritten in Vim9 script by github user lacygoill
|
27492
|
7 # Last Change: 2022 Jan 31
|
516
|
8
|
26779
|
9 var prepended: string
|
|
10 var grepCache: dict<list<dict<any>>>
|
|
11
|
|
12 # This function is used for the 'omnifunc' option.
|
27492
|
13 export def Complete(findstart: bool, abase: string): any # {{{1
|
26779
|
14 if findstart
|
|
15 # Locate the start of the item, including ".", "->" and "[...]".
|
|
16 var line: string = getline('.')
|
|
17 var start: number = charcol('.') - 1
|
|
18 var lastword: number = -1
|
502
|
19 while start > 0
|
548
|
20 if line[start - 1] =~ '\w'
|
26779
|
21 --start
|
548
|
22 elseif line[start - 1] =~ '\.'
|
26779
|
23 if lastword == -1
|
|
24 lastword = start
|
|
25 endif
|
|
26 --start
|
|
27 elseif start > 1 && line[start - 2] == '-'
|
|
28 && line[start - 1] == '>'
|
|
29 if lastword == -1
|
|
30 lastword = start
|
|
31 endif
|
|
32 start -= 2
|
654
|
33 elseif line[start - 1] == ']'
|
26779
|
34 # Skip over [...].
|
|
35 var n: number = 0
|
|
36 --start
|
|
37 while start > 0
|
|
38 --start
|
|
39 if line[start] == '['
|
|
40 if n == 0
|
|
41 break
|
|
42 endif
|
|
43 --n
|
|
44 elseif line[start] == ']' # nested []
|
|
45 ++n
|
|
46 endif
|
|
47 endwhile
|
502
|
48 else
|
26779
|
49 break
|
502
|
50 endif
|
|
51 endwhile
|
548
|
52
|
26779
|
53 # Return the column of the last word, which is going to be changed.
|
|
54 # Remember the text that comes before it in prepended.
|
548
|
55 if lastword == -1
|
26779
|
56 prepended = ''
|
|
57 return byteidx(line, start)
|
548
|
58 endif
|
26779
|
59 prepended = line[start : lastword - 1]
|
|
60 return byteidx(line, lastword)
|
502
|
61 endif
|
|
62
|
26779
|
63 # Return list of matches.
|
511
|
64
|
26779
|
65 var base: string = prepended .. abase
|
548
|
66
|
26779
|
67 # Don't do anything for an empty base, would result in all the tags in the
|
|
68 # tags file.
|
654
|
69 if base == ''
|
|
70 return []
|
|
71 endif
|
|
72
|
26779
|
73 # init cache for vimgrep to empty
|
|
74 grepCache = {}
|
720
|
75
|
26779
|
76 # Split item in words, keep empty word after "." or "->".
|
|
77 # "aa" -> ['aa'], "aa." -> ['aa', ''], "aa.bb" -> ['aa', 'bb'], etc.
|
|
78 # We can't use split, because we need to skip nested [...].
|
|
79 # "aa[...]" -> ['aa', '[...]'], "aa.bb[...]" -> ['aa', 'bb', '[...]'], etc.
|
|
80 var items: list<string>
|
|
81 var s: number = 0
|
|
82 var arrays: number = 0
|
654
|
83 while 1
|
26779
|
84 var e: number = base->charidx(match(base, '\.\|->\|\[', s))
|
654
|
85 if e < 0
|
|
86 if s == 0 || base[s - 1] != ']'
|
26779
|
87 items->add(base[s :])
|
654
|
88 endif
|
|
89 break
|
|
90 endif
|
|
91 if s == 0 || base[s - 1] != ']'
|
26779
|
92 items->add(base[s : e - 1])
|
548
|
93 endif
|
654
|
94 if base[e] == '.'
|
26779
|
95 # skip over '.'
|
|
96 s = e + 1
|
654
|
97 elseif base[e] == '-'
|
26779
|
98 # skip over '->'
|
|
99 s = e + 2
|
654
|
100 else
|
26779
|
101 # Skip over [...].
|
|
102 var n: number = 0
|
|
103 s = e
|
|
104 ++e
|
|
105 while e < strcharlen(base)
|
|
106 if base[e] == ']'
|
|
107 if n == 0
|
|
108 break
|
|
109 endif
|
|
110 --n
|
|
111 elseif base[e] == '[' # nested [...]
|
|
112 ++n
|
|
113 endif
|
|
114 ++e
|
654
|
115 endwhile
|
26779
|
116 ++e
|
|
117 items->add(base[s : e - 1])
|
|
118 ++arrays
|
|
119 s = e
|
654
|
120 endif
|
|
121 endwhile
|
505
|
122
|
26779
|
123 # Find the variable items[0].
|
|
124 # 1. in current function (like with "gd")
|
|
125 # 2. in tags file(s) (like with ":tag")
|
|
126 # 3. in current file (like with "gD")
|
|
127 var res: list<dict<any>>
|
|
128 if items[0]->searchdecl(false, true) == 0
|
|
129 # Found, now figure out the type.
|
|
130 # TODO: join previous line if it makes sense
|
|
131 var line: string = getline('.')
|
|
132 var col: number = charcol('.')
|
|
133 if line[: col - 1]->stridx(';') >= 0
|
|
134 # Handle multiple declarations on the same line.
|
|
135 var col2: number = col - 1
|
1624
|
136 while line[col2] != ';'
|
26779
|
137 --col2
|
1624
|
138 endwhile
|
26779
|
139 line = line[col2 + 1 :]
|
|
140 col -= col2
|
1624
|
141 endif
|
26779
|
142 if line[: col - 1]->stridx(',') >= 0
|
|
143 # Handle multiple declarations on the same line in a function
|
|
144 # declaration.
|
|
145 var col2: number = col - 1
|
1624
|
146 while line[col2] != ','
|
26779
|
147 --col2
|
1624
|
148 endwhile
|
26779
|
149 if line[col2 + 1 : col - 1] =~ ' *[^ ][^ ]* *[^ ]'
|
|
150 line = line[col2 + 1 :]
|
|
151 col -= col2
|
1624
|
152 endif
|
|
153 endif
|
654
|
154 if len(items) == 1
|
26779
|
155 # Completing one word and it's a local variable: May add '[', '.' or
|
|
156 # '->'.
|
|
157 var match: string = items[0]
|
|
158 var kind: string = 'v'
|
|
159 if match(line, '\<' .. match .. '\s*\[') > 0
|
|
160 match ..= '['
|
654
|
161 else
|
26779
|
162 res = line[: col - 1]->Nextitem([''], 0, true)
|
|
163 if len(res) > 0
|
|
164 # There are members, thus add "." or "->".
|
|
165 if match(line, '\*[ \t(]*' .. match .. '\>') > 0
|
|
166 match ..= '->'
|
|
167 else
|
|
168 match ..= '.'
|
|
169 endif
|
|
170 endif
|
654
|
171 endif
|
26779
|
172 res = [{match: match, tagline: '', kind: kind, info: line}]
|
14668
|
173 elseif len(items) == arrays + 1
|
26779
|
174 # Completing one word and it's a local array variable: build tagline
|
|
175 # from declaration line
|
|
176 var match: string = items[0]
|
|
177 var kind: string = 'v'
|
|
178 var tagline: string = "\t/^" .. line .. '$/'
|
|
179 res = [{match: match, tagline: tagline, kind: kind, info: line}]
|
654
|
180 else
|
26779
|
181 # Completing "var.", "var.something", etc.
|
|
182 res = line[: col - 1]->Nextitem(items[1 :], 0, true)
|
654
|
183 endif
|
|
184 endif
|
|
185
|
14668
|
186 if len(items) == 1 || len(items) == arrays + 1
|
26779
|
187 # Only one part, no "." or "->": complete from tags file.
|
|
188 var tags: list<dict<any>>
|
14668
|
189 if len(items) == 1
|
26779
|
190 tags = taglist('^' .. base)
|
14668
|
191 else
|
26779
|
192 tags = taglist('^' .. items[0] .. '$')
|
14668
|
193 endif
|
734
|
194
|
26779
|
195 tags
|
|
196 # Remove members, these can't appear without something in front.
|
|
197 ->filter((_, v: dict<any>): bool =>
|
|
198 v->has_key('kind') ? v.kind != 'm' : true)
|
|
199 # Remove static matches in other files.
|
|
200 ->filter((_, v: dict<any>): bool =>
|
|
201 !v->has_key('static')
|
|
202 || !v['static']
|
|
203 || bufnr('%') == bufnr(v['filename']))
|
734
|
204
|
27492
|
205 res = res->extend(tags->map((_, v: dict<any>) => Tag2item(v)))
|
516
|
206 endif
|
|
207
|
|
208 if len(res) == 0
|
26779
|
209 # Find the variable in the tags file(s)
|
|
210 var diclist: list<dict<any>> = taglist('^' .. items[0] .. '$')
|
|
211 # Remove members, these can't appear without something in front.
|
|
212 ->filter((_, v: dict<string>): bool =>
|
|
213 v->has_key('kind') ? v.kind != 'm' : true)
|
734
|
214
|
26779
|
215 res = []
|
|
216 for i: number in len(diclist)->range()
|
|
217 # New ctags has the "typeref" field. Patched version has "typename".
|
|
218 if diclist[i]->has_key('typename')
|
27492
|
219 res = res->extend(diclist[i]['typename']->StructMembers(items[1 :], true))
|
26779
|
220 elseif diclist[i]->has_key('typeref')
|
27492
|
221 res = res->extend(diclist[i]['typeref']->StructMembers(items[1 :], true))
|
516
|
222 endif
|
|
223
|
26779
|
224 # For a variable use the command, which must be a search pattern that
|
|
225 # shows the declaration of the variable.
|
505
|
226 if diclist[i]['kind'] == 'v'
|
26779
|
227 var line: string = diclist[i]['cmd']
|
|
228 if line[: 1] == '/^'
|
|
229 var col: number = line->charidx(match(line, '\<' .. items[0] .. '\>'))
|
27492
|
230 res = res->extend(line[2 : col - 1]->Nextitem(items[1 :], 0, true))
|
26779
|
231 endif
|
505
|
232 endif
|
|
233 endfor
|
|
234 endif
|
|
235
|
26779
|
236 if len(res) == 0 && items[0]->searchdecl(true) == 0
|
|
237 # Found, now figure out the type.
|
|
238 # TODO: join previous line if it makes sense
|
|
239 var line: string = getline('.')
|
|
240 var col: number = charcol('.')
|
|
241 res = line[: col - 1]->Nextitem(items[1 :], 0, true)
|
523
|
242 endif
|
|
243
|
26779
|
244 # If the last item(s) are [...] they need to be added to the matches.
|
|
245 var last: number = len(items) - 1
|
|
246 var brackets: string = ''
|
654
|
247 while last >= 0
|
|
248 if items[last][0] != '['
|
|
249 break
|
523
|
250 endif
|
26779
|
251 brackets = items[last] .. brackets
|
|
252 --last
|
654
|
253 endwhile
|
516
|
254
|
26779
|
255 return res->map((_, v: dict<any>): dict<string> => Tagline2item(v, brackets))
|
|
256 enddef
|
511
|
257
|
26779
|
258 def GetAddition( # {{{1
|
27492
|
259 line: string,
|
|
260 match: string,
|
|
261 memarg: list<dict<any>>,
|
|
262 bracket: bool): string
|
26779
|
263 # Guess if the item is an array.
|
|
264 if bracket && match(line, match .. '\s*\[') > 0
|
654
|
265 return '['
|
|
266 endif
|
|
267
|
26779
|
268 # Check if the item has members.
|
|
269 if SearchMembers(memarg, [''], false)->len() > 0
|
|
270 # If there is a '*' before the name use "->".
|
|
271 if match(line, '\*[ \t(]*' .. match .. '\>') > 0
|
654
|
272 return '->'
|
|
273 else
|
|
274 return '.'
|
|
275 endif
|
|
276 endif
|
|
277 return ''
|
26779
|
278 enddef
|
654
|
279
|
26779
|
280 def Tag2item(val: dict<any>): dict<any> # {{{1
|
|
281 # Turn the tag info "val" into an item for completion.
|
|
282 # "val" is is an item in the list returned by taglist().
|
|
283 # If it is a variable we may add "." or "->". Don't do it for other types,
|
|
284 # such as a typedef, by not including the info that GetAddition() uses.
|
|
285 var res: dict<any> = {match: val['name']}
|
734
|
286
|
26779
|
287 res['extra'] = Tagcmd2extra(val['cmd'], val['name'], val['filename'])
|
666
|
288
|
26779
|
289 var s: string = Dict2info(val)
|
734
|
290 if s != ''
|
26779
|
291 res['info'] = s
|
734
|
292 endif
|
|
293
|
26779
|
294 res['tagline'] = ''
|
|
295 if val->has_key('kind')
|
|
296 var kind: string = val['kind']
|
|
297 res['kind'] = kind
|
734
|
298 if kind == 'v'
|
26779
|
299 res['tagline'] = "\t" .. val['cmd']
|
|
300 res['dict'] = val
|
734
|
301 elseif kind == 'f'
|
26779
|
302 res['match'] = val['name'] .. '('
|
648
|
303 endif
|
|
304 endif
|
734
|
305
|
|
306 return res
|
26779
|
307 enddef
|
654
|
308
|
26779
|
309 def Dict2info(dict: dict<any>): string # {{{1
|
|
310 # Use all the items in dictionary for the "info" entry.
|
|
311 var info: string = ''
|
|
312 for k: string in dict->keys()->sort()
|
|
313 info ..= k .. repeat(' ', 10 - strlen(k))
|
800
|
314 if k == 'cmd'
|
26779
|
315 info ..= dict['cmd']
|
|
316 ->matchstr('/^\s*\zs.*\ze$/')
|
|
317 ->substitute('\\\(.\)', '\1', 'g')
|
800
|
318 else
|
26779
|
319 var dictk: any = dict[k]
|
|
320 if typename(dictk) != 'string'
|
|
321 info ..= dictk->string()
|
|
322 else
|
|
323 info ..= dictk
|
|
324 endif
|
800
|
325 endif
|
26779
|
326 info ..= "\n"
|
800
|
327 endfor
|
|
328 return info
|
26779
|
329 enddef
|
800
|
330
|
26779
|
331 def ParseTagline(line: string): dict<any> # {{{1
|
|
332 # Parse a tag line and return a dictionary with items like taglist()
|
|
333 var l: list<string> = split(line, "\t")
|
|
334 var d: dict<any>
|
800
|
335 if len(l) >= 3
|
26779
|
336 d['name'] = l[0]
|
|
337 d['filename'] = l[1]
|
|
338 d['cmd'] = l[2]
|
|
339 var n: number = 2
|
800
|
340 if l[2] =~ '^/'
|
26779
|
341 # Find end of cmd, it may contain Tabs.
|
800
|
342 while n < len(l) && l[n] !~ '/;"$'
|
26779
|
343 ++n
|
|
344 d['cmd'] ..= ' ' .. l[n]
|
800
|
345 endwhile
|
|
346 endif
|
26779
|
347 for i: number in range(n + 1, len(l) - 1)
|
800
|
348 if l[i] == 'file:'
|
26779
|
349 d['static'] = 1
|
800
|
350 elseif l[i] !~ ':'
|
26779
|
351 d['kind'] = l[i]
|
800
|
352 else
|
26779
|
353 d[l[i]->matchstr('[^:]*')] = l[i]->matchstr(':\zs.*')
|
800
|
354 endif
|
|
355 endfor
|
|
356 endif
|
|
357
|
|
358 return d
|
26779
|
359 enddef
|
800
|
360
|
26779
|
361 def Tagline2item(val: dict<any>, brackets: string): dict<string> # {{{1
|
|
362 # Turn a match item "val" into an item for completion.
|
|
363 # "val['match']" is the matching item.
|
|
364 # "val['tagline']" is the tagline in which the last part was found.
|
|
365 var line: string = val['tagline']
|
|
366 var add: string = GetAddition(line, val['match'], [val], brackets == '')
|
|
367 var res: dict<string> = {word: val['match'] .. brackets .. add}
|
734
|
368
|
26779
|
369 if val->has_key('info')
|
|
370 # Use info from Tag2item().
|
|
371 res['info'] = val['info']
|
734
|
372 else
|
26779
|
373 # Parse the tag line and add each part to the "info" entry.
|
|
374 var s: string = ParseTagline(line)->Dict2info()
|
734
|
375 if s != ''
|
26779
|
376 res['info'] = s
|
734
|
377 endif
|
|
378 endif
|
|
379
|
26779
|
380 if val->has_key('kind')
|
|
381 res['kind'] = val['kind']
|
734
|
382 elseif add == '('
|
26779
|
383 res['kind'] = 'f'
|
734
|
384 else
|
26779
|
385 var s: string = line->matchstr('\t\(kind:\)\=\zs\S\ze\(\t\|$\)')
|
734
|
386 if s != ''
|
26779
|
387 res['kind'] = s
|
734
|
388 endif
|
|
389 endif
|
|
390
|
26779
|
391 if val->has_key('extra')
|
|
392 res['menu'] = val['extra']
|
734
|
393 return res
|
659
|
394 endif
|
666
|
395
|
26779
|
396 # Isolate the command after the tag and filename.
|
|
397 var s: string = line->matchstr('[^\t]*\t[^\t]*\t\zs\(/^.*$/\|[^\t]*\)\ze\(;"\t\|\t\|$\)')
|
666
|
398 if s != ''
|
26779
|
399 res['menu'] = s->Tagcmd2extra(val['match'], line->matchstr('[^\t]*\t\zs[^\t]*\ze\t'))
|
666
|
400 endif
|
734
|
401 return res
|
26779
|
402 enddef
|
648
|
403
|
26779
|
404 def Tagcmd2extra( # {{{1
|
27492
|
405 cmd: string,
|
|
406 name: string,
|
|
407 fname: string): string
|
26779
|
408 # Turn a command from a tag line to something that is useful in the menu
|
|
409 var x: string
|
|
410 if cmd =~ '^/^'
|
|
411 # The command is a search command, useful to see what it is.
|
|
412 x = cmd
|
|
413 ->matchstr('^/^\s*\zs.*\ze$/')
|
|
414 ->substitute('\<' .. name .. '\>', '@@', '')
|
|
415 ->substitute('\\\(.\)', '\1', 'g')
|
|
416 .. ' - ' .. fname
|
|
417 elseif cmd =~ '^\d*$'
|
|
418 # The command is a line number, the file name is more useful.
|
|
419 x = fname .. ' - ' .. cmd
|
666
|
420 else
|
26779
|
421 # Not recognized, use command and file name.
|
|
422 x = cmd .. ' - ' .. fname
|
666
|
423 endif
|
|
424 return x
|
26779
|
425 enddef
|
511
|
426
|
26779
|
427 def Nextitem( # {{{1
|
27492
|
428 lead: string,
|
|
429 items: list<string>,
|
|
430 depth: number,
|
|
431 all: bool): list<dict<string>>
|
26779
|
432 # Find composing type in "lead" and match items[0] with it.
|
|
433 # Repeat this recursively for items[1], if it's there.
|
|
434 # When resolving typedefs "depth" is used to avoid infinite recursion.
|
|
435 # Return the list of matches.
|
511
|
436
|
26779
|
437 # Use the text up to the variable name and split it in tokens.
|
|
438 var tokens: list<string> = split(lead, '\s\+\|\<')
|
505
|
439
|
26779
|
440 # Try to recognize the type of the variable. This is rough guessing...
|
|
441 var res: list<dict<string>>
|
|
442 for tidx: number in len(tokens)->range()
|
|
443
|
|
444 # Skip tokens starting with a non-ID character.
|
720
|
445 if tokens[tidx] !~ '^\h'
|
|
446 continue
|
|
447 endif
|
|
448
|
26779
|
449 # Recognize "struct foobar" and "union foobar".
|
|
450 # Also do "class foobar" when it's C++ after all (doesn't work very well
|
|
451 # though).
|
|
452 if (tokens[tidx] == 'struct'
|
|
453 || tokens[tidx] == 'union'
|
|
454 || tokens[tidx] == 'class')
|
|
455 && tidx + 1 < len(tokens)
|
|
456 res = StructMembers(tokens[tidx] .. ':' .. tokens[tidx + 1], items, all)
|
511
|
457 break
|
|
458 endif
|
|
459
|
26779
|
460 # TODO: add more reserved words
|
|
461 if ['int', 'short', 'char', 'float',
|
|
462 'double', 'static', 'unsigned', 'extern']->index(tokens[tidx]) >= 0
|
516
|
463 continue
|
|
464 endif
|
|
465
|
26779
|
466 # Use the tags file to find out if this is a typedef.
|
|
467 var diclist: list<dict<any>> = taglist('^' .. tokens[tidx] .. '$')
|
|
468 for tagidx: number in len(diclist)->range()
|
|
469 var item: dict<any> = diclist[tagidx]
|
734
|
470
|
26779
|
471 # New ctags has the "typeref" field. Patched version has "typename".
|
|
472 if item->has_key('typeref')
|
27492
|
473 res = res->extend(item['typeref']->StructMembers(items, all))
|
26779
|
474 continue
|
800
|
475 endif
|
26779
|
476 if item->has_key('typename')
|
27492
|
477 res = res->extend(item['typename']->StructMembers(items, all))
|
26779
|
478 continue
|
523
|
479 endif
|
|
480
|
26779
|
481 # Only handle typedefs here.
|
734
|
482 if item['kind'] != 't'
|
26779
|
483 continue
|
734
|
484 endif
|
|
485
|
26779
|
486 # Skip matches local to another file.
|
|
487 if item->has_key('static') && item['static']
|
|
488 && bufnr('%') != bufnr(item['filename'])
|
|
489 continue
|
516
|
490 endif
|
|
491
|
26779
|
492 # For old ctags we recognize "typedef struct aaa" and
|
|
493 # "typedef union bbb" in the tags file command.
|
|
494 var cmd: string = item['cmd']
|
|
495 var ei: number = cmd->charidx(matchend(cmd, 'typedef\s\+'))
|
523
|
496 if ei > 1
|
26779
|
497 var cmdtokens: list<string> = cmd[ei :]->split('\s\+\|\<')
|
|
498 if len(cmdtokens) > 1
|
|
499 if cmdtokens[0] == 'struct'
|
|
500 || cmdtokens[0] == 'union'
|
|
501 || cmdtokens[0] == 'class'
|
|
502 var name: string = ''
|
|
503 # Use the first identifier after the "struct" or "union"
|
|
504 for ti: number in (len(cmdtokens) - 1)->range()
|
|
505 if cmdtokens[ti] =~ '^\w'
|
|
506 name = cmdtokens[ti]
|
|
507 break
|
|
508 endif
|
|
509 endfor
|
|
510 if name != ''
|
27492
|
511 res = res->extend(StructMembers(cmdtokens[0] .. ':' .. name, items, all))
|
26779
|
512 endif
|
|
513 elseif depth < 10
|
|
514 # Could be "typedef other_T some_T".
|
27492
|
515 res = res->extend(cmdtokens[0]->Nextitem(items, depth + 1, all))
|
26779
|
516 endif
|
|
517 endif
|
511
|
518 endif
|
|
519 endfor
|
516
|
520 if len(res) > 0
|
505
|
521 break
|
|
522 endif
|
516
|
523 endfor
|
511
|
524
|
516
|
525 return res
|
26779
|
526 enddef
|
516
|
527
|
26779
|
528 def StructMembers( # {{{1
|
27492
|
529 atypename: string,
|
|
530 items: list<string>,
|
|
531 all: bool): list<dict<string>>
|
516
|
532
|
26779
|
533 # Search for members of structure "typename" in tags files.
|
|
534 # Return a list with resulting matches.
|
|
535 # Each match is a dictionary with "match" and "tagline" entries.
|
|
536 # When "all" is true find all, otherwise just return 1 if there is any member.
|
|
537
|
|
538 # Todo: What about local structures?
|
|
539 var fnames: string = tagfiles()
|
|
540 ->map((_, v: string) => escape(v, ' \#%'))
|
|
541 ->join()
|
516
|
542 if fnames == ''
|
523
|
543 return []
|
516
|
544 endif
|
|
545
|
26779
|
546 var typename: string = atypename
|
|
547 var qflist: list<dict<any>>
|
|
548 var cached: number = 0
|
|
549 var n: string
|
|
550 if !all
|
|
551 n = '1' # stop at first found match
|
|
552 if grepCache->has_key(typename)
|
|
553 qflist = grepCache[typename]
|
|
554 cached = 1
|
720
|
555 endif
|
716
|
556 else
|
26779
|
557 n = ''
|
716
|
558 endif
|
720
|
559 if !cached
|
|
560 while 1
|
26779
|
561 execute 'silent! keepjumps noautocmd '
|
|
562 .. n .. 'vimgrep ' .. '/\t' .. typename .. '\(\t\|$\)/j '
|
|
563 .. fnames
|
720
|
564
|
26779
|
565 qflist = getqflist()
|
|
566 if len(qflist) > 0 || match(typename, '::') < 0
|
|
567 break
|
720
|
568 endif
|
26779
|
569 # No match for "struct:context::name", remove "context::" and try again.
|
|
570 typename = typename->substitute(':[^:]*::', ':', '')
|
720
|
571 endwhile
|
|
572
|
26779
|
573 if !all
|
|
574 # Store the result to be able to use it again later.
|
|
575 grepCache[typename] = qflist
|
516
|
576 endif
|
720
|
577 endif
|
516
|
578
|
26779
|
579 # Skip over [...] items
|
|
580 var idx: number = 0
|
|
581 var target: string
|
14668
|
582 while 1
|
26779
|
583 if idx >= len(items)
|
|
584 target = '' # No further items, matching all members
|
14668
|
585 break
|
|
586 endif
|
26779
|
587 if items[idx][0] != '['
|
|
588 target = items[idx]
|
14668
|
589 break
|
|
590 endif
|
26779
|
591 ++idx
|
14668
|
592 endwhile
|
26779
|
593 # Put matching members in matches[].
|
|
594 var matches: list<dict<string>>
|
|
595 for l: dict<any> in qflist
|
|
596 var memb: string = l['text']->matchstr('[^\t]*')
|
|
597 if memb =~ '^' .. target
|
|
598 # Skip matches local to another file.
|
|
599 if match(l['text'], "\tfile:") < 0
|
|
600 || bufnr('%') == l['text']->matchstr('\t\zs[^\t]*')->bufnr()
|
|
601 var item: dict<string> = {match: memb, tagline: l['text']}
|
734
|
602
|
26779
|
603 # Add the kind of item.
|
|
604 var s: string = l['text']->matchstr('\t\(kind:\)\=\zs\S\ze\(\t\|$\)')
|
|
605 if s != ''
|
|
606 item['kind'] = s
|
|
607 if s == 'f'
|
|
608 item['match'] = memb .. '('
|
|
609 endif
|
|
610 endif
|
734
|
611
|
26779
|
612 matches->add(item)
|
734
|
613 endif
|
516
|
614 endif
|
511
|
615 endfor
|
505
|
616
|
523
|
617 if len(matches) > 0
|
26779
|
618 # Skip over next [...] items
|
|
619 ++idx
|
654
|
620 while 1
|
26779
|
621 if idx >= len(items)
|
|
622 return matches # No further items, return the result.
|
654
|
623 endif
|
26779
|
624 if items[idx][0] != '['
|
|
625 break
|
654
|
626 endif
|
26779
|
627 ++idx
|
654
|
628 endwhile
|
505
|
629
|
26779
|
630 # More items following. For each of the possible members find the
|
|
631 # matching following members.
|
|
632 return SearchMembers(matches, items[idx :], all)
|
511
|
633 endif
|
|
634
|
26779
|
635 # Failed to find anything.
|
511
|
636 return []
|
26779
|
637 enddef
|
|
638
|
|
639 def SearchMembers( # {{{1
|
27492
|
640 matches: list<dict<any>>,
|
|
641 items: list<string>,
|
|
642 all: bool): list<dict<string>>
|
523
|
643
|
26779
|
644 # For matching members, find matches for following items.
|
|
645 # When "all" is true find all, otherwise just return 1 if there is any member.
|
|
646 var res: list<dict<string>>
|
|
647 for i: number in len(matches)->range()
|
|
648 var typename: string = ''
|
|
649 var line: string
|
|
650 if matches[i]->has_key('dict')
|
|
651 if matches[i]['dict']->has_key('typename')
|
|
652 typename = matches[i]['dict']['typename']
|
|
653 elseif matches[i]['dict']->has_key('typeref')
|
|
654 typename = matches[i]['dict']['typeref']
|
648
|
655 endif
|
26779
|
656 line = "\t" .. matches[i]['dict']['cmd']
|
648
|
657 else
|
26779
|
658 line = matches[i]['tagline']
|
|
659 var eb: number = matchend(line, '\ttypename:')
|
|
660 var e: number = charidx(line, eb)
|
800
|
661 if e < 0
|
26779
|
662 eb = matchend(line, '\ttyperef:')
|
|
663 e = charidx(line, eb)
|
800
|
664 endif
|
648
|
665 if e > 0
|
26779
|
666 # Use typename field
|
|
667 typename = line->matchstr('[^\t]*', eb)
|
648
|
668 endif
|
|
669 endif
|
716
|
670
|
648
|
671 if typename != ''
|
27492
|
672 res = res->extend(StructMembers(typename, items, all))
|
523
|
673 else
|
26779
|
674 # Use the search command (the declaration itself).
|
|
675 var sb: number = line->match('\t\zs/^')
|
|
676 var s: number = charidx(line, sb)
|
523
|
677 if s > 0
|
26779
|
678 var e: number = line
|
|
679 ->charidx(match(line, '\<' .. matches[i]['match'] .. '\>', sb))
|
|
680 if e > 0
|
27492
|
681 res = res->extend(line[s : e - 1]->Nextitem(items, 0, all))
|
26779
|
682 endif
|
523
|
683 endif
|
|
684 endif
|
26779
|
685 if !all && len(res) > 0
|
716
|
686 break
|
|
687 endif
|
523
|
688 endfor
|
|
689 return res
|
26779
|
690 enddef
|
|
691 #}}}1
|
3237
|
692
|
26779
|
693 # vim: noet sw=2 sts=2
|