user class and refactor
This commit is contained in:
35
lc.vim
35
lc.vim
@@ -1,34 +1,44 @@
|
||||
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
|
||||
while stridx(line, s) == -1
|
||||
s = strpart(s, 1)
|
||||
if strlen(s) == 0
|
||||
return [0, ""] # vim ignores these
|
||||
return Item.new(0, "")
|
||||
endif
|
||||
endwhile
|
||||
const idx = stridx(line, s)
|
||||
const slen = strlen(s)
|
||||
if add_base
|
||||
return [slen, base .. strpart(line, idx + slen)]
|
||||
return Item.new(slen, base .. strpart(line, idx + slen))
|
||||
else
|
||||
return [slen, strpart(line, idx + slen)]
|
||||
return Item.new(slen, strpart(line, idx + slen))
|
||||
endif
|
||||
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
|
||||
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)
|
||||
enddef
|
||||
|
||||
def DoLSCmpl(): string
|
||||
@@ -39,6 +49,7 @@ def DoLSCmpl(): string
|
||||
->sort()
|
||||
->uniq()
|
||||
->map( (_, v) => GetMatch(base, v, v:false) )
|
||||
->filter( (_, v) => GetMatch(base, v, v:false) )
|
||||
->sort( (x, y) => y[0] - x[0] )
|
||||
->map( (_, v) => v[1] )
|
||||
|
||||
|
Reference in New Issue
Block a user