Extract GetLongestSuffix and add quick test
This commit is contained in:
		
							
								
								
									
										31
									
								
								lc.vim
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								lc.vim
									
									
									
									
									
								
							@@ -5,21 +5,32 @@ class Item
 | 
			
		||||
    const val: string
 | 
			
		||||
endclass
 | 
			
		||||
 | 
			
		||||
def GetMatch(base: string, line: string, add_base: bool): Item
 | 
			
		||||
    var s = base
 | 
			
		||||
    while stridx(line, s) == -1
 | 
			
		||||
def GetLongestSuffix(hay: string, ndl: string): string
 | 
			
		||||
    var s = ndl
 | 
			
		||||
    var l = len(ndl)
 | 
			
		||||
    while stridx(hay, s) == -1
 | 
			
		||||
        s = strpart(s, 1)
 | 
			
		||||
        if strlen(s) == 0
 | 
			
		||||
            return Item.new(0, "")
 | 
			
		||||
        l = l - 1
 | 
			
		||||
        if l == 0
 | 
			
		||||
            return ""
 | 
			
		||||
        endif
 | 
			
		||||
    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 slen = strlen(s)
 | 
			
		||||
    const  m = strpart(line, idx + slen)
 | 
			
		||||
    if add_base
 | 
			
		||||
        return Item.new(slen, base .. strpart(line, idx + slen))
 | 
			
		||||
    else
 | 
			
		||||
        return Item.new(slen, strpart(line, idx + slen))
 | 
			
		||||
        return Item.new(slen, base .. m)
 | 
			
		||||
    endif
 | 
			
		||||
    return Item.new(slen, m)
 | 
			
		||||
enddef
 | 
			
		||||
 | 
			
		||||
def GetMatches(base: string, add_base: bool): list<string>
 | 
			
		||||
@@ -30,9 +41,9 @@ def GetMatches(base: string, add_base: bool): list<string>
 | 
			
		||||
        ->sort()
 | 
			
		||||
        ->uniq()
 | 
			
		||||
        ->map( (_, v) => GetMatch(base, v, add_base) )
 | 
			
		||||
        ->filter( (_, i) => i.len > 3 )
 | 
			
		||||
        ->filter( (_, m) => m.len > 3 )
 | 
			
		||||
        ->sort( (x, y) => y.len - x.len )
 | 
			
		||||
        ->map( (_, i) => i.val )
 | 
			
		||||
        ->map( (_, m) => m.val )
 | 
			
		||||
enddef
 | 
			
		||||
 | 
			
		||||
def LineSuffixCompl(findstart: bool, base: string): any
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user