initial version to replace line completion

This commit is contained in:
Balakrishnan Balasubramanian 2023-08-25 12:32:34 -04:00
parent 4e887415c1
commit ec4c119181

31
lc.vim
View File

@ -1,16 +1,20 @@
vim9script vim9script
def GetMatch(base: string, line: string): list<any> def GetMatch(base: string, line: string, add_base: bool): list<any>
var s = base var s = base
while stridx(line, s) == -1 while stridx(line, s) == -1
s = strpart(s, 1) s = strpart(s, 1)
if strlen(s) == 0 if strlen(s) == 0
return [0, ""] return [0, ""] # vim ignores these
endif endif
endwhile endwhile
const idx = stridx(line, s) const idx = stridx(line, s)
const slen = strlen(s) const slen = strlen(s)
return [slen, base .. strpart(line, idx + slen)] if add_base
return [slen, base .. strpart(line, idx + slen)]
else
return [slen, strpart(line, idx + slen)]
endif
enddef enddef
def LineSuffixCompl(findstart: bool, base: string): any def LineSuffixCompl(findstart: bool, base: string): any
@ -22,9 +26,26 @@ def LineSuffixCompl(findstart: bool, base: string): any
->flattennew() ->flattennew()
->sort() ->sort()
->uniq() ->uniq()
->map( (_, v) => GetMatch(base, v) ) ->map( (_, v) => GetMatch(base, v, v:true) )
->sort( (x, y) => y[0] - x[0] ) ->sort( (x, y) => y[0] - x[0] )
->map( (_, v) => v[1] ) ->map( (_, v) => v[1] )
enddef enddef
setlocal completefunc=LineSuffixCompl def DoLSCmpl(): string
const base = getline('.')
const lines = getbufinfo()
->map( (_, buf) => getbufline(buf["bufnr"], 1, "$"))
->flattennew()
->sort()
->uniq()
->map( (_, v) => GetMatch(base, v, v:false) )
->sort( (x, y) => y[0] - x[0] )
->map( (_, v) => v[1] )
complete(col('.'), lines)
return ''
enddef
# inoremap <C-X><C-L> <C-R>=<SID>DoLSCmpl()<CR>
set completefunc=LineSuffixCompl