summaryrefslogtreecommitdiffstats
path: root/runtime/autoload/ccomplete.vim
blob: 3bddba7a9510921dc1bf21f07e9e8f22646441da (plain)
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
vim9script noclear

# Vim completion script
# Language:     C
# Maintainer:   Bram Moolenaar <Bram@vim.org>
#		Rewritten in Vim9 script by github user lacygoill
# Last Change:  2022 Jan 31

var prepended: string
var grepCache: dict<list<dict<any>>>

# This function is used for the 'omnifunc' option.
export def Complete(findstart: bool, abase: string): any # {{{1
  if findstart
    # Locate the start of the item, including ".", "->" and "[...]".
    var line: string = getline('.')
    var start: number = charcol('.') - 1
    var lastword: number = -1
    while start > 0
      if line[start - 1] =~ '\w'
        --start
      elseif line[start - 1] =~ '\.'
        if lastword == -1
          lastword = start
        endif
        --start
      elseif start > 1 && line[start - 2] == '-'
        && line[start - 1] == '>'
        if lastword == -1
          lastword = start
        endif
        start -= 2
      elseif line[start - 1] == ']'
        # Skip over [...].
        var n: number = 0
        --start
        while start > 0
          --start
          if line[start] == '['
            if n == 0
              break
            endif
            --n
          elseif line[start] == ']'  # nested []
            ++n
          endif
        endwhile
      else
        break
      endif
    endwhile

    # Return the column of the last word, which is going to be changed.
    # Remember the text that comes before it in prepended.
    if lastword == -1
      prepended = ''
      return byteidx(line, start)
    endif
    prepended = line[start : lastword - 1]
    return byteidx(line, lastword)
  endif

  # Return list of matches.

  var base: string = prepended .. abase

  # Don't do anything for an empty base, would result in all the tags in the
  # tags file.
  if base == ''
    return []
  endif

  # init cache for vimgrep to empty
  grepCache = {}

  # Split item in words, keep empty word after "." or "->".
  # "aa" -> ['aa'], "aa." -> ['aa', ''], "aa.bb" -> ['aa', 'bb'], etc.
  # We can't use split, because we need to skip nested [...].
  # "aa[...]" -> ['aa', '[...]'], "aa.bb[...]" -> ['aa', 'bb', '[...]'], etc.
  var items: list<string>
  var s: number = 0
  var arrays: number = 0
  while 1
    var e: number = base->charidx(match(base, '\.\|->\|\[', s))
    if e < 0
      if s == 0 || base[s - 1] != ']'
        items->add(base[s :])
      endif
      break
    endif
    if s == 0 || base[s - 1] != ']'
      items->add(base[s : e - 1])
    endif
    if base[e] == '.'
      # skip over '.'
      s = e + 1
    elseif base[e] == '-'
      # skip over '->'
      s = e + 2
    else
      # Skip over [...].
      var n: number = 0
      s = e
      ++e
      while e < strcharlen(base)
        if base[e] == ']'
          if n == 0
            break
          endif
          --n
        elseif base[e] == '['  # nested [...]
          ++n
        endif
        ++e
      endwhile
      ++e
      items->add(base[s : e - 1])
      ++arrays
      s = e
    endif
  endwhile

  # Find the variable items[0].
  # 1. in current function (like with "gd")
  # 2. in tags file(s) (like with ":tag")
  # 3. in current file (like with "gD")
  var res: list<dict<any>>
  if items[0]->searchdecl(false, true) == 0
    # Found, now figure out the type.
    # TODO: join previous line if it makes sense
    var line: string = getline('.')
    var col: number = charcol('.')
    if line[: col - 1]->stridx(';') >= 0
      # Handle multiple declarations on the same line.
      var col2: number = col - 1
      while line[col2] != ';'
        --col2
      endwhile
      line = line[col2 + 1 :]
      col -= col2
    endif
    if line[: col - 1]->stridx(',') >= 0
      # Handle multiple declarations on the same line in a function
      # declaration.
      var col2: number = col - 1
      while line[col2] != ','
        --col2
      endwhile
      if line[col2 + 1 : col - 1] =~ ' *[^ ][^ ]*  *[^ ]'
        line = line[col2 + 1 :]
        col -= col2
      endif
    endif
    if len(items) == 1
      # Completing one word and it's a local variable: May add '[', '.' or
      # '->'.
      var match: string = items[0]
      var kind: string = 'v'
      if match(line, '\<' .. match .. '\s*\[') > 0
        match ..= '['
      else
        res = line[: col - 1]->Nextitem([''], 0, true)
        if len(res) > 0
          # There are members, thus add "." or "->".
          if match(line, '\*[ \t(]*' .. match .. '\>') > 0
            match ..= '->'
          else
            match ..= '.'
          endif
        endif
      endif
      res = [{match: match, tagline: '', kind: kind, info: line}]
    elseif len(items) == arrays + 1
      # Completing one word and it's a local array variable: build tagline
      # from declaration line
      var match: string = items[0]
      var kind: string = 'v'
      var tagline: string = "\t/^" .. line .. '$/'
      res = [{match: match, tagline: tagline, kind: kind, info: line}]
    else
      # Completing "var.", "var.something", etc.
      res = line[: col - 1]->Nextitem(items[1 :], 0, true)
    endif
  endif

  if len(items) == 1 || len(items) == arrays + 1
    # Only one part, no "." or "->": complete from tags file.
    var tags: list<dict<any>>
    if len(items) == 1
      tags = taglist('^' .. base)
    else
      tags = taglist('^' .. items[0] .. '$')
    endif

    tags
      # Remove members, these can't appear without something in front.
      ->filter((_, v: dict<any>): bool =>
                v->has_key('kind') ? v.kind != 'm' : true)
      # Remove static matches in other files.
      ->filter((_, v: dict<any>): bool =>
                 !v->has_key('static')
              || !v['static']
              || bufnr('%') == bufnr(v['filename']))

    res = res->extend(tags->map((_, v: dict<any>) => Tag2item(v)))
  endif

  if len(res) == 0
    # Find the variable in the tags file(s)
    var diclist: list<dict<any>> = taglist('^' .. items[0] .. '$')
      # Remove members, these can't appear without something in front.
      ->filter((_, v: dict<string>): bool =>
                v->has_key('kind') ? v.kind != 'm' : true)

    res = []
    for i: number in len(diclist)->range()
      # New ctags has the "typeref" field.  Patched version has "typename".
      if diclist[i]->has_key('typename')
        res = res->extend(diclist[i]['typename']->StructMembers(items[1 :], true))
      elseif diclist[i]->has_key('typeref')
        res = res->extend(diclist[i]['typeref']->StructMembers(items[1 :], true))
      endif

      # For a variable use the command, which must be a search pattern that
      # shows the declaration of the variable.
      if diclist[i]['kind'] == 'v'
        var line: string = diclist[i]['cmd']
        if line[: 1] == '/^'
          var col: number = line->charidx(match(line, '\<' .. items[0] .. '\>'))
          res = res->extend(line[2 : col - 1]->Nextitem(items[1 :], 0, true))
        endif
      endif
    endfor
  endif

  if len(res) == 0 && items[0]->searchdecl(true) == 0
    # Found, now figure out the type.
    # TODO: join previous line if it makes sense
    var line: string = getline('.')
    var col: number = charcol('.')
    res = line[: col - 1]->Nextitem(items[1 :], 0, true)
  endif

  # If the last item(s) are [...] they need to be added to the matches.
  var last: number = len(items) - 1
  var brackets: string = ''
  while last >= 0
    if items[last][0] != '['
      break
    endif
    brackets = items[last] .. brackets
    --last
  endwhile

  return res->map((_, v: dict<any>): dict<string> => Tagline2item(v, brackets))
enddef

def GetAddition( # {{{1
    line: string,
    match: string,
    memarg: list<dict<any>>,
    bracket: bool): string
  # Guess if the item is an array.
  if bracket && match(line, match .. '\s*\[') > 0
    return '['
  endif

  # Check if the item has members.
  if SearchMembers(memarg, [''], false)->len() > 0
    # If there is a '*' before the name use "->".
    if match(line, '\*[ \t(]*' .. match .. '\>') > 0
      return '->'
    else
      return '.'
    endif
  endif
  return ''
enddef

def Tag2item(val: dict<any>): dict<any> # {{{1
# Turn the tag info "val" into an item for completion.
# "val" is is an item in the list returned by taglist().
# If it is a variable we may add "." or "->".  Don't do it for other types,
# such as a typedef, by not including the info that GetAddition() uses.
  var res: dict<any> = {match: val['name']}

  res['extra'] = Tagcmd2extra(val['cmd'], val['name'], val['filename'])

  var s: string = Dict2info(val)
  if s != ''
    res['info'] = s
  endif

  res['tagline'] = ''
  if val->has_key('kind')
    var kind: string = val['kind']
    res['kind'] = kind
    if kind == 'v'
      res['tagline'] = "\t" .. val['cmd']
      res['dict'] = val
    elseif kind == 'f'
      res['match'] = val['name'] .. '('
    endif
  endif

  return res
enddef

def Dict2info(dict: dict<any>): string # {{{1
# Use all the items in dictionary for the "info" entry.
  var info: string = ''
  for k: string in dict->keys()->sort()
    info  ..= k .. repeat(' ', 10 - strlen(k))
    if k == 'cmd'
      info ..= dict['cmd']
        ->matchstr('/^\s*\zs.*\ze$/')
        ->substitute('\\\(.\)', '\1', 'g')
    else
      var dictk: any = dict[k]
      if typename(dictk) != 'string'
        info ..= dictk->string()
      else
        info ..= dictk
      endif
    endif
    info ..= "\n"
  endfor
  return info
enddef

def ParseTagline(line: string): dict<any> # {{{1
# Parse a tag line and return a dictionary with items like taglist()
  var l: list<string> = split(line, "\t")
  var d: dict<any>
  if len(l) >= 3
    d['name'] = l[0]
    d['filename'] = l[1]
    d['cmd'] = l[2]
    var n: number = 2
    if l[2] =~ '^/'
      # Find end of cmd, it may contain Tabs.
      while n < len(l) && l[n] !~ '/;"$'
        ++n
        d['cmd'] ..= '  ' .. l[n]
      endwhile
    endif
    for i: number in range(n + 1, len(l) - 1)
      if l[i] == 'file:'
        d['static'] = 1
      elseif l[i] !~ ':'
        d['kind'] = l[i]
      else
        d[l[i]->matchstr('[^:]*')] = l[i]->matchstr(':\zs.*')
      endif
    endfor
  endif

  return d
enddef

def Tagline2item(val: dict<any>, brackets: string): dict<string> # {{{1
# Turn a match item "val" into an item for completion.
# "val['match']" is the matching item.
# "val['tagline']" is the tagline in which the last part was found.
  var line: string = val['tagline']
  var add: string = GetAddition(line, val['match'], [val], brackets == '')
  var res: dict<string> = {word: val['match'] .. brackets .. add}

  if val->has_key('info')
    # Use info from Tag2item().
    res['info'] = val['info']
  else
    # Parse the tag line and add each part to the "info" entry.
    var s: string = ParseTagline(line)->Dict2info()
    if s != ''
      res['info'] = s
    endif
  endif

  if val->has_key('kind')
    res['kind'] = val['kind']
  elseif add == '('
    res['kind'] = 'f'
  else
    var s: string = line->matchstr('\t\(kind:\)\=\zs\S\ze\(\t\|$\)')
    if s != ''
      res['kind'] = s
    endif
  endif

  if val->has_key('extra')
    res['menu'] = val['extra']
    return res
  endif

  # Isolate the command after the tag and filename.
  var s: string = line->matchstr('[^\t]*\t[^\t]*\t\zs\(/^.*$/\|[^\t]*\)\ze\(;"\t\|\t\|$\)')
  if s != ''
    res['menu'] = s->Tagcmd2extra(val['match'], line->matchstr('[^\t]*\t\zs[^\t]*\ze\t'))
  endif
  return res
enddef

def Tagcmd2extra( # {{{1
    cmd: string,
    name: string,
    fname: string): string
# Turn a command from a tag line to something that is useful in the menu
  var x: string
  if cmd =~ '^/^'
    # The command is a search command, useful to see what it is.
    x = cmd
      ->matchstr('^/^\s*\zs.*\ze$/')
      ->substitute('\<' .. name .. '\>', '@@', '')
      ->substitute('\\\(.\)', '\1', 'g')
      .. ' - ' .. fname
  elseif cmd =~ '^\d*$'
    # The command is a line number, the file name is more useful.
    x = fname .. ' - ' .. cmd
  else
    # Not recognized, use command and file name.
    x = cmd .. ' - ' .. fname
  endif
  return x
enddef

def Nextitem( # {{{1
    lead: string,
    items: list<string>,
    depth: number,
    all: bool): list<dict<string>>
# Find composing type in "lead" and match items[0] with it.
# Repeat this recursively for items[1], if it's there.
# When resolving typedefs "depth" is used to avoid infinite recursion.
# Return the list of matches.

  # Use the text up to the variable name and split it in tokens.
  var tokens: list<string> = split(lead, '\s\+\|\<')

  # Try to recognize the type of the variable.  This is rough guessing...
  var res: list<dict<string>>
  for tidx: number in len(tokens)->range()

    # Skip tokens starting with a non-ID character.
    if tokens[tidx] !~ '^\h'
      continue
    endif

    # Recognize "struct foobar" and "union foobar".
    # Also do "class foobar" when it's C++ after all (doesn't work very well
    # though).
    if (tokens[tidx] == 'struct'
      || tokens[tidx] == 'union'
      || tokens[tidx] == 'class')
      && tidx + 1 < len(tokens)
      res = StructMembers(tokens[tidx] .. ':' .. tokens[tidx + 1], items, all)
      break
    endif

    # TODO: add more reserved words
    if ['int', 'short', 'char', 'float',
        'double', 'static', 'unsigned', 'extern']->index(tokens[tidx]) >= 0
      continue
    endif

    # Use the tags file to find out if this is a typedef.
    var diclist: list<dict<any>> = taglist('^' .. tokens[tidx] .. '$')
    for tagidx: number in len(diclist)->range()
      var item: dict<any> = diclist[tagidx]

      # New ctags has the "typeref" field.  Patched version has "typename".
      if item->has_key('typeref')
        res = res->extend(item['typeref']->StructMembers(items, all))
        continue
      endif
      if item->has_key('typename')
        res = res->extend(item['typename']->StructMembers(items, all))
        continue
      endif

      # Only handle typedefs here.
      if item['kind'] != 't'
        continue
      endif

      # Skip matches local to another file.
      if item->has_key('static') && item['static']
        && bufnr('%') != bufnr(item['filename'])
        continue
      endif

      # For old ctags we recognize "typedef struct aaa" and
      # "typedef union bbb" in the tags file command.
      var cmd: string = item['cmd']
      var ei: number = cmd->charidx(matchend(cmd, 'typedef\s\+'))
      if ei > 1
        var cmdtokens: list<string> = cmd[ei :]->split('\s\+\|\<')
        if len(cmdtokens) > 1
          if cmdtokens[0] == 'struct'
            || cmdtokens[0] == 'union'
            || cmdtokens[0] == 'class'
            var name: string = ''
            # Use the first identifier after the "struct" or "union"
            for ti: number in (len(cmdtokens) - 1)->range()
              if cmdtokens[ti] =~ '^\w'
                name = cmdtokens[ti]
                break
              endif
            endfor
            if name != ''
              res = res->extend(StructMembers(cmdtokens[0] .. ':' .. name, items, all))
            endif
          elseif depth < 10
            # Could be "typedef other_T some_T".
            res = res->extend(cmdtokens[0]->Nextitem(items, depth + 1, all))
          endif
        endif
      endif
    endfor
    if len(res) > 0
      break
    endif
  endfor

  return res
enddef

def StructMembers( # {{{1
    atypename: string,
    items: list<string>,
    all: bool): list<dict<string>>

# Search for members of structure "typename" in tags files.
# Return a list with resulting matches.
# Each match is a dictionary with "match" and "tagline" entries.
# When "all" is true find all, otherwise just return 1 if there is any member.

  # Todo: What about local structures?
  var fnames: string = tagfiles()
    ->map((_, v: string) => escape(v, ' \#%'))
    ->join()
  if fnames == ''
    return []
  endif

  var typename: string = atypename
  var qflist: list<dict<any>>
  var cached: number = 0
  var n: string
  if !all
    n = '1'  # stop at first found match
    if grepCache->has_key(typename)
      qflist = grepCache[typename]
      cached = 1
    endif
  else
    n = ''
  endif
  if !cached
    while 1
      execute 'silent! keepjumps noautocmd '
        .. n .. 'vimgrep ' .. '/\t' .. typename .. '\(\t\|$\)/j '
        .. fnames

      qflist = getqflist()
      if len(qflist) > 0 || match(typename, '::') < 0
        break
      endif
      # No match for "struct:context::name", remove "context::" and try again.
      typename = typename->substitute(':[^:]*::', ':', '')
    endwhile

    if !all
      # Store the result to be able to use it again later.
      grepCache[typename] = qflist
    endif
  endif

  # Skip over [...] items
  var idx: number = 0
  var target: string
  while 1
    if idx >= len(items)
      target = ''  # No further items, matching all members
      break
    endif
    if items[idx][0] != '['
      target = items[idx]
      break
    endif
    ++idx
  endwhile
  # Put matching members in matches[].
  var matches: list<dict<string>>
  for l: dict<any> in qflist
    var memb: string = l['text']->matchstr('[^\t]*')
    if memb =~ '^' .. target
      # Skip matches local to another file.
      if match(l['text'], "\tfile:") < 0
        || bufnr('%') == l['text']->matchstr('\t\zs[^\t]*')->bufnr()
        var item: dict<string> = {match: memb, tagline: l['text']}

        # Add the kind of item.
        var s: string = l['text']->matchstr('\t\(kind:\)\=\zs\S\ze\(\t\|$\)')
        if s != ''
          item['kind'] = s
          if s == 'f'
            item['match'] = memb .. '('
          endif
        endif

        matches->add(item)
      endif
    endif
  endfor

  if len(matches) > 0
    # Skip over next [...] items
    ++idx
    while 1
      if idx >= len(items)
        return matches  # No further items, return the result.
      endif
      if items[idx][0] != '['
        break
      endif
      ++idx
    endwhile

    # More items following.  For each of the possible members find the
    # matching following members.
    return SearchMembers(matches, items[idx :], all)
  endif

  # Failed to find anything.
  return []
enddef

def SearchMembers( # {{{1
    matches: list<dict<any>>,
    items: list<string>,
    all: bool): list<dict<string>>

# For matching members, find matches for following items.
# When "all" is true find all, otherwise just return 1 if there is any member.
  var res: list<dict<string>>
  for i: number in len(matches)->range()
    var typename: string = ''
    var line: string
    if matches[i]->has_key('dict')
      if matches[i]['dict']->has_key('typename')
        typename = matches[i]['dict']['typename']
      elseif matches[i]['dict']->has_key('typeref')
        typename = matches[i]['dict']['typeref']
      endif
      line = "\t" .. matches[i]['dict']['cmd']
    else
      line = matches[i]['tagline']
      var eb: number = matchend(line, '\ttypename:')
      var e: number = charidx(line, eb)
      if e < 0
        eb = matchend(line, '\ttyperef:')
        e = charidx(line, eb)
      endif
      if e > 0
        # Use typename field
        typename = line->matchstr('[^\t]*', eb)
      endif
    endif

    if typename != ''
      res = res->extend(StructMembers(typename, items, all))
    else
      # Use the search command (the declaration itself).
      var sb: number = line->match('\t\zs/^')
      var s: number = charidx(line, sb)
      if s > 0
        var e: number = line
          ->charidx(match(line, '\<' .. matches[i]['match'] .. '\>', sb))
        if e > 0
          res = res->extend(line[s : e - 1]->Nextitem(items, 0, all))
        endif
      endif
    endif
    if !all && len(res) > 0
      break
    endif
  endfor
  return res
enddef
#}}}1

# vim: noet sw=2 sts=2