File: vimcomplete.vim

package info (click to toggle)
vim 2%3A9.1.2103-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 93,456 kB
  • sloc: ansic: 433,730; cpp: 6,399; makefile: 4,597; sh: 2,397; java: 2,312; xml: 2,099; python: 1,595; perl: 1,419; awk: 730; lisp: 501; cs: 458; objc: 369; sed: 8; csh: 6; haskell: 1
file content (96 lines) | stat: -rw-r--r-- 3,376 bytes parent folder | download
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
vim9script

# Vim completion script
# Language:    Vim script
# Maintainer:  Maxim Kim <habamax@gmail.com>
# Last Change: 2025-10-15
#
# Usage:
# setlocal omnifunc=vimcomplete#Complete
#
# Simple complete function for Vim script

var trigger: string = ""
var prefix: string = ""


def GetTrigger(line: string): list<any>
    var result = ""
    var result_len = 0

    if line =~ '->\k*$'
        result = 'function'
    elseif line =~ '\v%(^|\s+)\&\k*$'
        result = 'option'
    elseif line =~ '\vse%[t]\s+(\k+\s+)*no\k*$'
        result = 'nooption'
        result_len = -2
    elseif line =~ '[\[(]\s*$'
        result = 'expression'
    elseif line =~ '[lvgsb]:\k*$'
        result = 'var'
        result_len = 2
    elseif line !~ '^\s*$'
        result = getcompletiontype(line) ?? 'cmdline'
    endif
    return [result, result_len]
enddef

export def Complete(findstart: number, base: string): any
    if findstart > 0
        prefix = ""
        var line = getline('.')->strpart(0, col('.') - 1)
        var keyword = line->matchstr('\k\+$')
        var stx = synstack(line('.'), col('.') - 1)->map('synIDattr(v:val, "name")')->join()
        if stx =~? 'Comment' || (stx =~ 'String' && stx !~ 'vimStringInterpolationExpr')
            return -2
        endif
        var trigger_len: number = 0
        [trigger, trigger_len] = GetTrigger(line)
        if keyword->empty() && trigger->empty()
            return -2
        endif
        prefix = line
        return line->len() - keyword->len() - trigger_len
    endif

    var items = []
    if trigger == 'function'
        items = getcompletion(base, 'function')
            ->mapnew((_, v) => ({word: v, kind: 'v', menu: 'Function', dup: 0}))
    elseif trigger == 'option'
        items = getcompletion(base, 'option')
            ->mapnew((_, v) => ({word: v, kind: 'v', menu: 'Option', dup: 0}))
    elseif trigger == 'nooption'
        items = getcompletion(base[2 : ], 'option')
            ->mapnew((_, v) => ({word: v, kind: 'v', menu: 'Option', dup: 0}))
    elseif trigger == 'var'
        items = getcompletion(base, 'var')
            ->mapnew((_, v) => ({word: v, kind: 'v', menu: 'Variable', dup: 0}))
    elseif trigger == 'expression'
        items = getcompletion(base, 'expression')
            ->mapnew((_, v) => ({word: v, kind: 'v', menu: 'Expression', dup: 0}))
    elseif trigger == 'command'
        var commands = getcompletion(base, 'command')
            ->mapnew((_, v) => ({word: v, kind: 'v', menu: 'Command', dup: 0}))
        var functions = getcompletion(base, 'function')
            ->mapnew((_, v) => ({word: v, kind: 'v', menu: 'Function', dup: 0}))
        items = commands + functions
    else
        try
            # :! and :term completion is very slow on Windows and WSL, disable it there.
            if !((has("win32") || has("win32unix") || exists("$WSLENV")) && getcompletiontype(prefix) == 'shellcmd')
                items = getcompletion(prefix, 'cmdline')
                    ->mapnew((_, v) => ({word: v->matchstr('\k\+'), kind: 'v', dup: 0}))
            endif
        catch /E220/
        endtry

        if empty(items) && !empty(base)
            items = getcompletion(base, 'expression')
                ->mapnew((_, v) => ({word: v, kind: 'v', menu: 'Expression', dup: 0}))
        endif
    endif

    return items->empty() ? v:none : items
enddef