Compare commits

...

3 Commits

2 changed files with 41 additions and 62 deletions

32
exp.txt
View File

@@ -1,32 +0,0 @@
fo
bar
bar
hello
bar
hello
hello
f
abcd
fo
hello
fo
bcd
hello world and ha ha ha
foo bar and ha ha ha
xdfadf bar and ha ha he
skafjds bar and ha ha ha
hello ha ha ha
foob and ha ha ha
skafjds bar and ha ha ha
fjds bar and ha ha ha
a b c and ha ha ha
var foo = SomeBigName(arg1, arg2, arg3)
var bar = SomeBigName(arg1, arg2, arg3)
var xyz = SomeBigName(arg1, arg2, arg3)
var xdf = Som

71
lc.vim
View File

@@ -1,51 +1,62 @@
vim9script
def GetMatch(base: string, line: string, add_base: bool): list<any>
var s = base
while stridx(line, s) == -1
class Item
const len: number
const val: string
endclass
def GetLongestSuffix(hay: string, ndl: string): string
var s = ndl
var l = len(ndl)
while stridx(hay, s) == -1
s = strpart(s, 1)
if strlen(s) == 0
return [0, ""] # vim ignores these
l = l - 1
if l == 0
return ""
endif
endwhile
return s
enddef
# echo GetLongestSuffix("hello world", "world")->assert_equal("world")
# echo GetLongestSuffix("hello world", "foo world")->assert_equal("o world")
# echo GetLongestSuffix("hello world foo bar blah", "foo world")->assert_equal("o world")
# echo v:errors
def GetMatch(base: string, line: string, add_base: bool): Item
const s = GetLongestSuffix(line, base)
const idx = stridx(line, s)
const slen = strlen(s)
const m = strpart(line, idx + slen)
if add_base
return [slen, base .. strpart(line, idx + slen)]
else
return [slen, strpart(line, idx + slen)]
return Item.new(slen, base .. m)
endif
return Item.new(slen, m)
enddef
def GetMatches(base: string, add_base: bool): list<string>
return getbufinfo()
->filter( (_, bf) => bf.listed )
->map( (_, buf) => getbufline(buf["bufnr"], 1, "$"))
->flattennew()
->sort()
->uniq()
->map( (_, v) => GetMatch(base, v, add_base) )
->filter( (_, m) => m.len > 3 )
->sort( (x, y) => y.len - x.len )
->map( (_, m) => m.val )
enddef
def LineSuffixCompl(findstart: bool, base: string): any
if findstart
return 0
endif
return getbufinfo()
->map( (_, buf) => getbufline(buf["bufnr"], 1, "$"))
->flattennew()
->sort()
->uniq()
->map( (_, v) => GetMatch(base, v, v:true) )
->sort( (x, y) => y[0] - x[0] )
->map( (_, v) => v[1] )
return GetMatches(base, true)
enddef
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 ''
def DoLSCmpl()
GetMatches(getline('.'), false)->complete(col('.'))
enddef
# inoremap <C-X><C-L> <C-R>=<SID>DoLSCmpl()<CR>
inoremap <C-X><C-L> <C-R>=<SID>DoLSCmpl()<CR>
set completefunc=LineSuffixCompl