vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

vimcomplete.vim (3674B)


      1 vim9script
      2 
      3 # Vim completion script
      4 # Language:    Vim script
      5 # Maintainer:  Maxim Kim <habamax@gmail.com>
      6 # Last Change: 2026 Sep 22
      7 #
      8 # Usage:
      9 # setlocal omnifunc=vimcomplete#Complete
     10 #
     11 # Simple complete function for Vim script
     12 
     13 var trigger: string = ""
     14 var prefix: string = ""
     15 
     16 
     17 def GetTrigger(line: string): list<any>
     18     var result = ""
     19     var result_len = 0
     20 
     21     if line =~ '->\k*$'
     22         result = 'function'
     23     elseif line =~ '\v%(^|\s+)\&\k*$'
     24         result = 'option'
     25     elseif line =~ '\vse%[t]\s+(\k+\s+)*no\k*$'
     26         result = 'nooption'
     27         result_len = -2
     28     elseif line =~ '[\[(]\s*$' && !complete_info(["auto"]).auto
     29         result = 'expression'
     30     elseif line =~ '[lvgsb]:\k*$'
     31         result = 'var'
     32         result_len = 2
     33     elseif line !~ '^\s*$' && !complete_info(["auto"]).auto
     34         result = getcompletiontype(line) ?? 'cmdline'
     35     endif
     36     return [result, result_len]
     37 enddef
     38 
     39 export def Complete(findstart: number, base: string): any
     40     if findstart > 0
     41         prefix = ""
     42         var line = getline('.')->strpart(0, col('.') - 1)
     43         var keyword = line->matchstr('\k\+$')
     44         var stx = synstack(line('.'), col('.') - 1)->map('synIDattr(v:val, "name")')->join()
     45         if stx =~? 'Comment' || (stx =~ 'String' && stx !~ 'vimStringInterpolationExpr')
     46             return -2
     47         endif
     48         var trigger_len: number = 0
     49         [trigger, trigger_len] = GetTrigger(line)
     50         if keyword->empty() && trigger->empty()
     51             return -2
     52         endif
     53         prefix = line
     54         return line->len() - keyword->len() - trigger_len
     55     endif
     56 
     57     # getcompletion() uses wildoptions to filter out items
     58     # which is not what users might expect
     59     var save_wildoptions = &wildoptions
     60     &wildoptions = ""
     61     defer () => {
     62         &wildoptions = save_wildoptions
     63     }()
     64 
     65     var items = []
     66     if trigger == 'function'
     67         items = getcompletion(base, 'function')
     68             ->mapnew((_, v) => ({word: v, kind: 'v', menu: 'Function', dup: 0}))
     69     elseif trigger == 'option'
     70         items = getcompletion(base, 'option')
     71             ->mapnew((_, v) => ({word: v, kind: 'v', menu: 'Option', dup: 0}))
     72     elseif trigger == 'nooption'
     73         items = getcompletion(base[2 : ], 'option')
     74             ->mapnew((_, v) => ({word: v, kind: 'v', menu: 'Option', dup: 0}))
     75     elseif trigger == 'var'
     76         items = getcompletion(base, 'var')
     77             ->mapnew((_, v) => ({word: v, kind: 'v', menu: 'Variable', dup: 0}))
     78     elseif trigger == 'expression'
     79         items = getcompletion(base, 'expression')
     80             ->mapnew((_, v) => ({word: v, kind: 'v', menu: 'Expression', dup: 0}))
     81     elseif trigger == 'command'
     82         var commands = getcompletion(base, 'command')
     83             ->mapnew((_, v) => ({word: v, kind: 'v', menu: 'Command', dup: 0}))
     84         var functions = getcompletion(base, 'function')
     85             ->mapnew((_, v) => ({word: v, kind: 'v', menu: 'Function', dup: 0}))
     86         items = commands + functions
     87     else
     88         try
     89             # :! and :term completion is very slow on Windows and WSL, disable it there.
     90             if !((has("win32") || has("win32unix") || exists("$WSLENV")) && getcompletiontype(prefix) == 'shellcmd')
     91                 items = getcompletion(prefix, 'cmdline')
     92                     ->mapnew((_, v) => ({word: v->matchstr('\k\+'), kind: 'v', dup: 0}))
     93             endif
     94         catch /E220/
     95         endtry
     96 
     97         if empty(items) && !empty(base)
     98             items = getcompletion(base, 'expression')
     99                 ->mapnew((_, v) => ({word: v, kind: 'v', menu: 'Expression', dup: 0}))
    100         endif
    101     endif
    102 
    103     return items->empty() ? v:none : items
    104 enddef