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