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 vim9script
def GetMatch(base: string, line: string, add_base: bool): list<any> class Item
var s = base const len: number
while stridx(line, s) == -1 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) s = strpart(s, 1)
if strlen(s) == 0 l = l - 1
return [0, ""] # vim ignores these if l == 0
return ""
endif endif
endwhile 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 idx = stridx(line, s)
const slen = strlen(s) const slen = strlen(s)
const m = strpart(line, idx + slen)
if add_base if add_base
return [slen, base .. strpart(line, idx + slen)] return Item.new(slen, base .. m)
else
return [slen, strpart(line, idx + slen)]
endif 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 enddef
def LineSuffixCompl(findstart: bool, base: string): any def LineSuffixCompl(findstart: bool, base: string): any
if findstart if findstart
return 0 return 0
endif endif
return getbufinfo() return GetMatches(base, true)
->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] )
enddef enddef
def DoLSCmpl(): string def DoLSCmpl()
const base = getline('.') GetMatches(getline('.'), false)->complete(col('.'))
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 enddef
# inoremap <C-X><C-L> <C-R>=<SID>DoLSCmpl()<CR> inoremap <C-X><C-L> <C-R>=<SID>DoLSCmpl()<CR>
set completefunc=LineSuffixCompl set completefunc=LineSuffixCompl