Extract GetLongestSuffix and add quick test

This commit is contained in:
2025-09-09 15:42:21 -04:00
parent caa42275f6
commit 510ccad870

31
lc.vim
View File

@@ -5,21 +5,32 @@ class Item
const val: string const val: string
endclass endclass
def GetMatch(base: string, line: string, add_base: bool): Item def GetLongestSuffix(hay: string, ndl: string): string
var s = base var s = ndl
while stridx(line, s) == -1 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 Item.new(0, "") 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 Item.new(slen, base .. strpart(line, idx + slen)) return Item.new(slen, base .. m)
else
return Item.new(slen, strpart(line, idx + slen))
endif endif
return Item.new(slen, m)
enddef enddef
def GetMatches(base: string, add_base: bool): list<string> def GetMatches(base: string, add_base: bool): list<string>
@@ -30,9 +41,9 @@ def GetMatches(base: string, add_base: bool): list<string>
->sort() ->sort()
->uniq() ->uniq()
->map( (_, v) => GetMatch(base, v, add_base) ) ->map( (_, v) => GetMatch(base, v, add_base) )
->filter( (_, i) => i.len > 3 ) ->filter( (_, m) => m.len > 3 )
->sort( (x, y) => y.len - x.len ) ->sort( (x, y) => y.len - x.len )
->map( (_, i) => i.val ) ->map( (_, m) => m.val )
enddef enddef
def LineSuffixCompl(findstart: bool, base: string): any def LineSuffixCompl(findstart: bool, base: string): any