user class and refactor

This commit is contained in:
2025-09-08 22:08:39 -04:00
parent ec4c119181
commit c6e2d2a8f4

35
lc.vim
View File

@@ -1,34 +1,44 @@
vim9script vim9script
def GetMatch(base: string, line: string, add_base: bool): list<any> class Item
const len: number
const val: string
endclass
def GetMatch(base: string, line: string, add_base: bool): Item
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, ""] # vim ignores these return Item.new(0, "")
endif endif
endwhile endwhile
const idx = stridx(line, s) const idx = stridx(line, s)
const slen = strlen(s) const slen = strlen(s)
if add_base if add_base
return [slen, base .. strpart(line, idx + slen)] return Item.new(slen, base .. strpart(line, idx + slen))
else else
return [slen, strpart(line, idx + slen)] return Item.new(slen, strpart(line, idx + slen))
endif endif
enddef enddef
def GetMatches(base: string): list<string>
return getbufinfo()
->map( (_, buf) => getbufline(buf["bufnr"], 1, "$"))
->flattennew()
->sort()
->uniq()
->map( (_, v) => GetMatch(base, v, true) )
->filter( (_, i) => i.len != 0 )
->sort( (x, y) => y.len - x.len )
->map( (_, i) => i.val )
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)
->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(): string
@@ -39,6 +49,7 @@ def DoLSCmpl(): string
->sort() ->sort()
->uniq() ->uniq()
->map( (_, v) => GetMatch(base, v, v:false) ) ->map( (_, v) => GetMatch(base, v, v:false) )
->filter( (_, v) => GetMatch(base, v, v:false) )
->sort( (x, y) => y[0] - x[0] ) ->sort( (x, y) => y[0] - x[0] )
->map( (_, v) => v[1] ) ->map( (_, v) => v[1] )