Fix #344 - Backward scan when `--tiebreak=end`
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent 9017e29 · Browse files · 6 files·+112 added−58 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+112 added−58 removed
- tests:+11 added−0 removed
- other:+101 added−58 removed
Provenance
- Commit
6444322- Tree
0520ba3- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
9017e29
6 files changed+112 added−58 removed
modified:src/algo/algo_test.go+37 added-19 removed
3 unmodified lines4 "strings"5 "testing"6 )7 8-func assertMatch(t *testing.T, fun func(bool, []rune, []rune) (int, int), caseSensitive bool, input string, pattern string, sidx int, eidx int) {8+func assertMatch(t *testing.T, fun func(bool, bool, []rune, []rune) (int, int), caseSensitive bool, forward bool, input string, pattern string, sidx int, eidx int) {9 if !caseSensitive {10 pattern = strings.ToLower(pattern)11 }12- s, e := fun(caseSensitive, []rune(input), []rune(pattern))12+ s, e := fun(caseSensitive, forward, []rune(input), []rune(pattern))13 if s != sidx {14 t.Errorf("Invalid start index: %d (expected: %d, %s / %s)", s, sidx, input, pattern)15 }16 if e != eidx {1 unmodified line18 }19 }20 21 func TestFuzzyMatch(t *testing.T) {22- assertMatch(t, FuzzyMatch, false, "fooBarbaz", "oBZ", 2, 9)23- assertMatch(t, FuzzyMatch, true, "fooBarbaz", "oBZ", -1, -1)24- assertMatch(t, FuzzyMatch, true, "fooBarbaz", "oBz", 2, 9)25- assertMatch(t, FuzzyMatch, true, "fooBarbaz", "fooBarbazz", -1, -1)22+ assertMatch(t, FuzzyMatch, false, true, "fooBarbaz", "oBZ", 2, 9)23+ assertMatch(t, FuzzyMatch, true, true, "fooBarbaz", "oBZ", -1, -1)24+ assertMatch(t, FuzzyMatch, true, true, "fooBarbaz", "oBz", 2, 9)25+ assertMatch(t, FuzzyMatch, true, true, "fooBarbaz", "fooBarbazz", -1, -1)26 }27 28+func TestFuzzyMatchBackward(t *testing.T) {29+ assertMatch(t, FuzzyMatch, false, true, "foobar fb", "fb", 0, 4)30+ assertMatch(t, FuzzyMatch, false, false, "foobar fb", "fb", 7, 9)31+}32+33 func TestExactMatchNaive(t *testing.T) {34+ for _, dir := range []bool{true, false} {29- assertMatch(t, ExactMatchNaive, false, "fooBarbaz", "oBA", 2, 5)30- assertMatch(t, ExactMatchNaive, true, "fooBarbaz", "oBA", -1, -1)31- assertMatch(t, ExactMatchNaive, true, "fooBarbaz", "fooBarbazz", -1, -1)35+ assertMatch(t, ExactMatchNaive, false, dir, "fooBarbaz", "oBA", 2, 5)36+ assertMatch(t, ExactMatchNaive, true, dir, "fooBarbaz", "oBA", -1, -1)37+ assertMatch(t, ExactMatchNaive, true, dir, "fooBarbaz", "fooBarbazz", -1, -1)38+ }39 }40 41+func TestExactMatchNaiveBackward(t *testing.T) {42+ assertMatch(t, FuzzyMatch, false, true, "foobar foob", "oo", 1, 3)43+ assertMatch(t, FuzzyMatch, false, false, "foobar foob", "oo", 8, 10)44+}45+46 func TestPrefixMatch(t *testing.T) {47+ for _, dir := range []bool{true, false} {35- assertMatch(t, PrefixMatch, false, "fooBarbaz", "Foo", 0, 3)36- assertMatch(t, PrefixMatch, true, "fooBarbaz", "Foo", -1, -1)37- assertMatch(t, PrefixMatch, false, "fooBarbaz", "baz", -1, -1)48+ assertMatch(t, PrefixMatch, false, dir, "fooBarbaz", "Foo", 0, 3)49+ assertMatch(t, PrefixMatch, true, dir, "fooBarbaz", "Foo", -1, -1)50+ assertMatch(t, PrefixMatch, false, dir, "fooBarbaz", "baz", -1, -1)51+ }52 }53 54 func TestSuffixMatch(t *testing.T) {55+ for _, dir := range []bool{true, false} {41- assertMatch(t, SuffixMatch, false, "fooBarbaz", "Foo", -1, -1)42- assertMatch(t, SuffixMatch, false, "fooBarbaz", "baz", 6, 9)43- assertMatch(t, SuffixMatch, true, "fooBarbaz", "Baz", -1, -1)56+ assertMatch(t, SuffixMatch, false, dir, "fooBarbaz", "Foo", -1, -1)57+ assertMatch(t, SuffixMatch, false, dir, "fooBarbaz", "baz", 6, 9)58+ assertMatch(t, SuffixMatch, true, dir, "fooBarbaz", "Baz", -1, -1)59+ }60 }61 62 func TestEmptyPattern(t *testing.T) {63+ for _, dir := range []bool{true, false} {47- assertMatch(t, FuzzyMatch, true, "foobar", "", 0, 0)48- assertMatch(t, ExactMatchNaive, true, "foobar", "", 0, 0)49- assertMatch(t, PrefixMatch, true, "foobar", "", 0, 0)50- assertMatch(t, SuffixMatch, true, "foobar", "", 6, 6)64+ assertMatch(t, FuzzyMatch, true, dir, "foobar", "", 0, 0)65+ assertMatch(t, ExactMatchNaive, true, dir, "foobar", "", 0, 0)66+ assertMatch(t, PrefixMatch, true, dir, "foobar", "", 0, 0)67+ assertMatch(t, SuffixMatch, true, dir, "foobar", "", 6, 6)68+ }69 }modified:src/algo/algo.go+41 added-19 removed
13 unmodified lines14 *15 * In short: They try to do as little work as possible.16 */17 18+func runeAt(runes []rune, index int, max int, forward bool) rune {19+ if forward {20+ return runes[index]21+ }22+ return runes[max-index-1]23+}24+25 // FuzzyMatch performs fuzzy-match19-func FuzzyMatch(caseSensitive bool, runes []rune, pattern []rune) (int, int) {26+func FuzzyMatch(caseSensitive bool, forward bool, runes []rune, pattern []rune) (int, int) {27 if len(pattern) == 0 {28 return 0, 029 }30 9 unmodified lines40 pidx := 041 sidx := -142 eidx := -143 44+ lenRunes := len(runes)45+ lenPattern := len(pattern)46+37- for index, char := range runes {47+ for index := range runes {48+ char := runeAt(runes, index, lenRunes, forward)49 // This is considerably faster than blindly applying strings.ToLower to the50 // whole string51 if !caseSensitive {52 // Partially inlining `unicode.ToLower`. Ugly, but makes a noticeable4 unmodified lines57 } else if char > unicode.MaxASCII {58 char = unicode.To(unicode.LowerCase, char)59 }60 }50- if char == pattern[pidx] {61+ pchar := runeAt(pattern, pidx, lenPattern, forward)62+ if char == pchar {63 if sidx < 0 {64 sidx = index65 }54- if pidx++; pidx == len(pattern) {66+ if pidx++; pidx == lenPattern {67 eidx = index + 168 break69 }70 }1 unmodified line72 73 if sidx >= 0 && eidx >= 0 {74 pidx--75 for index := eidx - 1; index >= sidx; index-- {64- char := runes[index]76+ char := runeAt(runes, index, lenRunes, forward)77 if !caseSensitive {78 if char >= 'A' && char <= 'Z' {79 char += 3280 } else if char > unicode.MaxASCII {81 char = unicode.To(unicode.LowerCase, char)82 }83 }72- if char == pattern[pidx] {84+85+ pchar := runeAt(pattern, pidx, lenPattern, forward)86+ if char == pchar {87 if pidx--; pidx < 0 {88 sidx = index89 break90 }91 }92 }93+ if forward {79- return sidx, eidx94+ return sidx, eidx95+ }96+ return lenRunes - eidx, lenRunes - sidx97 }98 return -1, -199 }100 3 unmodified lines104 // strings and patterns are not very long.105 //106 // We might try to implement better algorithms in the future:107 // http://en.wikipedia.org/wiki/String_searching_algorithm91-func ExactMatchNaive(caseSensitive bool, runes []rune, pattern []rune) (int, int) {108+func ExactMatchNaive(caseSensitive bool, forward bool, runes []rune, pattern []rune) (int, int) {109 if len(pattern) == 0 {110 return 0, 0111 }112 96- numRunes := len(runes)97- plen := len(pattern)98- if numRunes < plen {113+ lenRunes := len(runes)114+ lenPattern := len(pattern)115+116+ if lenRunes < lenPattern {117 return -1, -1118 }119 120 pidx := 0103- for index := 0; index < numRunes; index++ {104- char := runes[index]121+ for index := 0; index < lenRunes; index++ {122+ char := runeAt(runes, index, lenRunes, forward)123 if !caseSensitive {124 if char >= 'A' && char <= 'Z' {125 char += 32126 } else if char > unicode.MaxASCII {127 char = unicode.To(unicode.LowerCase, char)128 }129 }112- if pattern[pidx] == char {130+ pchar := runeAt(pattern, pidx, lenPattern, forward)131+ if pchar == char {132 pidx++114- if pidx == plen {115- return index - plen + 1, index + 1133+ if pidx == lenPattern {134+ if forward {135+ return index - lenPattern + 1, index + 1136+ }137+ return lenRunes - (index + 1), lenRunes - (index - lenPattern + 1)138 }139 } else {140 index -= pidx141 pidx = 02 unmodified lines144 return -1, -1145 }146 147 // PrefixMatch performs prefix-match126-func PrefixMatch(caseSensitive bool, runes []rune, pattern []rune) (int, int) {148+func PrefixMatch(caseSensitive bool, forward bool, runes []rune, pattern []rune) (int, int) {149 if len(runes) < len(pattern) {150 return -1, -1151 }152 9 unmodified lines162 return 0, len(pattern)163 }164 165 // SuffixMatch performs suffix-match144-func SuffixMatch(caseSensitive bool, input []rune, pattern []rune) (int, int) {166+func SuffixMatch(caseSensitive bool, forward bool, input []rune, pattern []rune) (int, int) {167 runes := util.TrimRight(input)168 trimmedLen := len(runes)169 diff := trimmedLen - len(pattern)170 if diff < 0 {12 unmodified lines183 return trimmedLen - len(pattern), trimmedLen184 }185 186 // EqualMatch performs equal-match165-func EqualMatch(caseSensitive bool, runes []rune, pattern []rune) (int, int) {187+func EqualMatch(caseSensitive bool, forward bool, runes []rune, pattern []rune) (int, int) {188 if len(runes) != len(pattern) {189 return -1, -1190 }191 runesStr := string(runes)8 unmodified linesmodified:src/core.go+2 added-1 removed
141 unmodified lines142 143 // Matcher144 patternBuilder := func(runes []rune) *Pattern {145 return BuildPattern(146- opts.Mode, opts.Case, opts.Nth, opts.Delimiter, runes)146+ opts.Mode, opts.Case, opts.Tiebreak != byEnd,147+ opts.Nth, opts.Delimiter, runes)148 }149 matcher := NewMatcher(patternBuilder, sort, opts.Tac, eventBox)150 151 // Filtering mode123 unmodified linesmodified:src/pattern_test.go+11 added-11 removed
56 unmodified lines57 58 func TestExact(t *testing.T) {59 defer clearPatternCache()60 clearPatternCache()61- pattern := BuildPattern(ModeExtended, CaseSmart,61+ pattern := BuildPattern(ModeExtended, CaseSmart, true,62 []Range{}, Delimiter{}, []rune("'abc"))63 sidx, eidx := algo.ExactMatchNaive(64- pattern.caseSensitive, []rune("aabbcc abc"), pattern.terms[0].text)64+ pattern.caseSensitive, pattern.forward, []rune("aabbcc abc"), pattern.terms[0].text)65 if sidx != 7 || eidx != 10 {66 t.Errorf("%s / %d / %d", pattern.terms, sidx, eidx)67 }68 }69 70 func TestEqual(t *testing.T) {71 defer clearPatternCache()72 clearPatternCache()73- pattern := BuildPattern(ModeExtended, CaseSmart, []Range{}, Delimiter{}, []rune("^AbC$"))73+ pattern := BuildPattern(ModeExtended, CaseSmart, true, []Range{}, Delimiter{}, []rune("^AbC$"))74 75 match := func(str string, sidxExpected int, eidxExpected int) {76 sidx, eidx := algo.EqualMatch(77- pattern.caseSensitive, []rune(str), pattern.terms[0].text)77+ pattern.caseSensitive, pattern.forward, []rune(str), pattern.terms[0].text)78 if sidx != sidxExpected || eidx != eidxExpected {79 t.Errorf("%s / %d / %d", pattern.terms, sidx, eidx)80 }81 }3 unmodified lines85 86 func TestCaseSensitivity(t *testing.T) {87 defer clearPatternCache()88 clearPatternCache()89- pat1 := BuildPattern(ModeFuzzy, CaseSmart, []Range{}, Delimiter{}, []rune("abc"))89+ pat1 := BuildPattern(ModeFuzzy, CaseSmart, true, []Range{}, Delimiter{}, []rune("abc"))90 clearPatternCache()91- pat2 := BuildPattern(ModeFuzzy, CaseSmart, []Range{}, Delimiter{}, []rune("Abc"))91+ pat2 := BuildPattern(ModeFuzzy, CaseSmart, true, []Range{}, Delimiter{}, []rune("Abc"))92 clearPatternCache()93- pat3 := BuildPattern(ModeFuzzy, CaseIgnore, []Range{}, Delimiter{}, []rune("abc"))93+ pat3 := BuildPattern(ModeFuzzy, CaseIgnore, true, []Range{}, Delimiter{}, []rune("abc"))94 clearPatternCache()95- pat4 := BuildPattern(ModeFuzzy, CaseIgnore, []Range{}, Delimiter{}, []rune("Abc"))95+ pat4 := BuildPattern(ModeFuzzy, CaseIgnore, true, []Range{}, Delimiter{}, []rune("Abc"))96 clearPatternCache()97- pat5 := BuildPattern(ModeFuzzy, CaseRespect, []Range{}, Delimiter{}, []rune("abc"))97+ pat5 := BuildPattern(ModeFuzzy, CaseRespect, true, []Range{}, Delimiter{}, []rune("abc"))98 clearPatternCache()99- pat6 := BuildPattern(ModeFuzzy, CaseRespect, []Range{}, Delimiter{}, []rune("Abc"))99+ pat6 := BuildPattern(ModeFuzzy, CaseRespect, true, []Range{}, Delimiter{}, []rune("Abc"))100 101 if string(pat1.text) != "abc" || pat1.caseSensitive != false ||102 string(pat2.text) != "Abc" || pat2.caseSensitive != true ||103 string(pat3.text) != "abc" || pat3.caseSensitive != false ||4 unmodified lines108 }109 }110 111 func TestOrigTextAndTransformed(t *testing.T) {112- pattern := BuildPattern(ModeExtended, CaseSmart, []Range{}, Delimiter{}, []rune("jg"))112+ pattern := BuildPattern(ModeExtended, CaseSmart, true, []Range{}, Delimiter{}, []rune("jg"))113 tokens := Tokenize([]rune("junegunn"), Delimiter{})114 trans := Transform(tokens, []Range{Range{1, 1}})115 116 origRunes := []rune("junegunn.choi")16 unmodified linesmodified:src/pattern.go+10 added-8 removed
37 unmodified lines38 // Pattern represents search pattern39 type Pattern struct {40 mode Mode41 caseSensitive bool42+ forward bool43 text []rune44 terms []term45 hasInvTerm bool46 delimiter Delimiter47 nth []Range47- procFun map[termType]func(bool, []rune, []rune) (int, int)48+ procFun map[termType]func(bool, bool, []rune, []rune) (int, int)49 }50 51 var (52 _patternCache map[string]*Pattern17 unmodified lines70 _cache = NewChunkCache()71 }72 73 // BuildPattern builds Pattern object from the given arguments73-func BuildPattern(mode Mode, caseMode Case,74+func BuildPattern(mode Mode, caseMode Case, forward bool,75 nth []Range, delimiter Delimiter, runes []rune) *Pattern {76 77 var asString string78 switch mode {30 unmodified lines109 110 ptr := &Pattern{111 mode: mode,112 caseSensitive: caseSensitive,113+ forward: forward,114 text: []rune(asString),115 terms: terms,116 hasInvTerm: hasInvTerm,117 nth: nth,118 delimiter: delimiter,117- procFun: make(map[termType]func(bool, []rune, []rune) (int, int))}119+ procFun: make(map[termType]func(bool, bool, []rune, []rune) (int, int))}120 121 ptr.procFun[termFuzzy] = algo.FuzzyMatch122 ptr.procFun[termEqual] = algo.EqualMatch123 ptr.procFun[termExact] = algo.ExactMatchNaive165 unmodified lines289 }290 291 func (p *Pattern) fuzzyMatch(item *Item) (int, int) {292 input := p.prepareInput(item)291- return p.iter(algo.FuzzyMatch, input, p.caseSensitive, p.text)293+ return p.iter(algo.FuzzyMatch, input, p.caseSensitive, p.forward, p.text)294 }295 296 func (p *Pattern) extendedMatch(item *Item) []Offset {297 input := p.prepareInput(item)298 offsets := []Offset{}299 for _, term := range p.terms {300 pfun := p.procFun[term.typ]299- if sidx, eidx := p.iter(pfun, input, term.caseSensitive, term.text); sidx >= 0 {301+ if sidx, eidx := p.iter(pfun, input, term.caseSensitive, p.forward, term.text); sidx >= 0 {302 if term.inv {303 break304 }305 offsets = append(offsets, Offset{int32(sidx), int32(eidx)})19 unmodified lines325 item.transformed = ret326 return ret327 }328 327-func (p *Pattern) iter(pfun func(bool, []rune, []rune) (int, int),328- tokens []Token, caseSensitive bool, pattern []rune) (int, int) {329+func (p *Pattern) iter(pfun func(bool, bool, []rune, []rune) (int, int),330+ tokens []Token, caseSensitive bool, forward bool, pattern []rune) (int, int) {331 for _, part := range tokens {332 prefixLength := part.prefixLength331- if sidx, eidx := pfun(caseSensitive, part.text, pattern); sidx >= 0 {333+ if sidx, eidx := pfun(caseSensitive, forward, part.text, pattern); sidx >= 0 {334 return sidx + prefixLength, eidx + prefixLength335 }336 }337 return -1, -11 unmodified linemodified:test/test_go.rb+11 added
525 unmodified lines526 ]527 assert_equal output, `cat #{tempname} | #{FZF} -fh -n2 -d:`.split($/)528 end529 530+ def test_tiebreak_end_backward_scan531+ input = %w[532+ foobar-fb533+ fubar534+ ]535+ writelines tempname, input536+537+ assert_equal input.reverse, `cat #{tempname} | #{FZF} -f fb`.split($/)538+ assert_equal input, `cat #{tempname} | #{FZF} -f fb --tiebreak=end`.split($/)539+ end540+541 def test_invalid_cache542 tmux.send_keys "(echo d; echo D; echo x) | #{fzf '-q d'}", :Enter543 tmux.until { |lines| lines[-2].include? '2/3' }544 tmux.send_keys :BSpace500 unmodified lines