From c6e2d2a8f4ca10132401d88b420d82cd6001dfab Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Mon, 8 Sep 2025 22:08:39 -0400 Subject: [PATCH] user class and refactor --- lc.vim | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/lc.vim b/lc.vim index 62f9eac..86faa7a 100644 --- a/lc.vim +++ b/lc.vim @@ -1,34 +1,44 @@ vim9script -def GetMatch(base: string, line: string, add_base: bool): list +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 + 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] )