Skip to content
Myr

Fix invalid cache lookups

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 b208aa6 · Browse files · 3 files+56 added−29 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+56 added−29 removed

  • tests:+32 added−0 removed
  • other:+24 added−29 removed

3 files+56 added−29 removed

modified:src/pattern_test.go+15 added-17 removed
31 unmodified lines
32 terms[8][2].typ != termSuffix || terms[8][2].inv ||33 terms[8][3].typ != termExact || !terms[8][3].inv {34 t.Errorf("%s", terms)35 }36 for idx, termSet := range terms[:8] {36 for _, termSet := range terms[:8] {37 term := termSet[0]38 if len(term.text) != 3 {39 t.Errorf("%s", term)40 }41 if idx > 0 && len(term.origText) != 4+idx/5 {42 t.Errorf("%s", term)43 }41 }45 for _, term := range terms[8] {46 if len(term.origText) != 4 {47 t.Errorf("%s", term)48 }49 }42}4344func TestParseTermsExtendedExact(t *testing.T) {45 terms := parseTerms(false, CaseSmart, false,
112 unmodified lines
158}159160func TestCacheKey(t *testing.T) {161 test := func(extended bool, patStr string, expected string, cacheable bool) {162 clearPatternCache()163 pat := BuildPattern(true, algo.FuzzyMatchV2, extended, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune(patStr))164 if pat.CacheKey() != expected {165 t.Errorf("Expected: %s, actual: %s", expected, pat.CacheKey())166 }
13 unmodified lines
180 test(true, "| | | foo", "foo", true)181}182183func TestCacheable(t *testing.T) {191 test := func(fuzzy bool, str string, cacheable bool) {184 test := func(fuzzy bool, str string, expected string, cacheable bool) {185 clearPatternCache()186 pat := BuildPattern(fuzzy, algo.FuzzyMatchV2, true, CaseSmart, true, true, true, []Range{}, Delimiter{}, []rune(str))187 if pat.CacheKey() != expected {188 t.Errorf("Expected: %s, actual: %s", expected, pat.CacheKey())189 }190 if cacheable != pat.cacheable {191 t.Errorf("Invalid Pattern.cacheable for \"%s\": %v (expected: %v)", str, pat.cacheable, cacheable)192 }193 clearPatternCache()194 }198 test(true, "foo bar", true)199 test(true, "foo 'bar", true)200 test(true, "foo !bar", false)195 test(true, "foo bar", "foo bar", true)196 test(true, "foo 'bar", "foo bar", false)197 test(true, "foo !bar", "foo", false)198202 test(false, "foo bar", true)203 test(false, "foo '", true)204 test(false, "foo 'bar", false)205 test(false, "foo !bar", false)199 test(false, "foo bar", "foo bar", true)200 test(false, "foo 'bar", "foo", false)201 test(false, "foo '", "foo", true)202 test(false, "foo 'bar", "foo", false)203 test(false, "foo !bar", "foo", false)204}
modified:src/pattern.go+9 added-12 removed
8 unmodified lines
9)1011// fuzzy12// 'exact13// ^exact-prefix14// exact-suffix$15// !not-fuzzy16// !'not-exact17// !^not-exact-prefix18// !not-exact-suffix$13// ^prefix-exact14// suffix-exact$15// !inverse-exact16// !'inverse-fuzzy17// !^inverse-prefix-exact18// !inverse-suffix-exact$1920type termType int2122const (
8 unmodified lines
31 typ termType32 inv bool33 text []rune34 caseSensitive bool35 origText []rune35}3637type termSet []term38
60 unmodified lines
99 for _, termSet := range termSets {100 for idx, term := range termSet {101 // If the query contains inverse search terms or OR operators,102 // we cannot cache the search scope104 if !cacheable || idx > 0 || term.inv || !fuzzy && term.typ != termExact {103 if !cacheable || idx > 0 || term.inv || fuzzy && term.typ != termFuzzy || !fuzzy && term.typ != termExact {104 cacheable = false105 break Loop106 }107 }
43 unmodified lines
151 caseMode == CaseSmart && text != lowerText152 if !caseSensitive {153 text = lowerText154 }156 origText := []rune(text)155 if !fuzzy {156 typ = termExact157 }158
42 unmodified lines
201 set = append(set, term{202 typ: typ,203 inv: inv,204 text: textRunes,207 caseSensitive: caseSensitive,208 origText: origText})205 caseSensitive: caseSensitive})206 switchSet = true207 }208 }209 if len(set) > 0 {
22 unmodified lines
232 }233 cacheableTerms := []string{}234 for _, termSet := range p.termSets {235 if len(termSet) == 1 && !termSet[0].inv && (p.fuzzy || termSet[0].typ == termExact) {239 cacheableTerms = append(cacheableTerms, string(termSet[0].origText))236 cacheableTerms = append(cacheableTerms, string(termSet[0].text))237 }238 }239 return strings.Join(cacheableTerms, " ")240}
148 unmodified lines
modified:test/test_go.rb+32 added
717 unmodified lines
718 tmux.until { |lines| lines[-2].include? '1/3' }719 tmux.send_keys :Enter720 end721722 def test_invalid_cache_query_type723 command = %[(echo 'foo\\$bar'; echo 'barfoo'; echo 'foo^bar'; echo \\"foo'1-2\\"; seq 100) | #{fzf}]724725 # Suffix match726 tmux.send_keys command, :Enter727 tmux.until { |lines| lines.match_count == 104 }728 tmux.send_keys 'foo$'729 tmux.until { |lines| lines.match_count == 1 }730 tmux.send_keys 'bar'731 tmux.until { |lines| lines.match_count == 1 }732 tmux.send_keys :Enter733734 # Prefix match735 tmux.prepare736 tmux.send_keys command, :Enter737 tmux.until { |lines| lines.match_count == 104 }738 tmux.send_keys '^bar'739 tmux.until { |lines| lines.match_count == 1 }740 tmux.send_keys 'C-a', 'foo'741 tmux.until { |lines| lines.match_count == 1 }742 tmux.send_keys :Enter743744 # Exact match745 tmux.prepare746 tmux.send_keys command, :Enter747 tmux.until { |lines| lines.match_count == 104 }748 tmux.send_keys "'12"749 tmux.until { |lines| lines.match_count == 1 }750 tmux.send_keys 'C-a', 'foo'751 tmux.until { |lines| lines.match_count == 1 }752 end753754 def test_smart_case_for_each_term755 assert_equal 1, `echo Foo bar | #{FZF} -x -f "foo Fbar" | wc -l`.to_i756 end757
980 unmodified lines