Skip to content
Myr

Add OR operator

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

Close #412

authored by Junegunn Choi · committed by Junegunn Choi · parent a89d899 · Browse files · 4 files+127 added−53 removed
This commit

State

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

Diff stats

4 files+127 added−53 removed

  • other:+127 added−53 removed

4 files+127 added−53 removed

modified:man/man1/fzf.1+7 added
399 unmodified lines
400If you don't prefer fuzzy matching and do not wish to "quote" (prefixing with401\fB'\fR) every word, start fzf with \fB-e\fR or \fB--exact\fR option. Note that402when \fB--exact\fR is set, \fB'\fR-prefix "unquotes" the term.403404.SS OR operator405A single bar character term acts as an OR operator. For example, the following406query matches entries that start with \fBcore\fR and end with either \fBgo\fR,407\fBrb\fR, or \fBpy\fR.408409e.g. \fB^core go$ | rb$ | py$\fR410411.SH AUTHOR412Junegunn Choi (\fIjunegunn.c@gmail.com\fR)413414.SH SEE ALSO
14 unmodified lines
modified:src/pattern_test.go+55 added-23 removed
7 unmodified lines
8)910func TestParseTermsExtended(t *testing.T) {11 terms := parseTerms(true, CaseSmart,12 "aaa 'bbb ^ccc ddd$ !eee !'fff !^ggg !hhh$ ^iii$")12 "| aaa 'bbb ^ccc ddd$ !eee !'fff !^ggg !hhh$ | ^iii$ ^xxx | 'yyy | | zzz$ | !ZZZ |")13 if len(terms) != 9 ||14 terms[0].typ != termFuzzy || terms[0].inv ||15 terms[1].typ != termExact || terms[1].inv ||16 terms[2].typ != termPrefix || terms[2].inv ||17 terms[3].typ != termSuffix || terms[3].inv ||18 terms[4].typ != termFuzzy || !terms[4].inv ||19 terms[5].typ != termExact || !terms[5].inv ||20 terms[6].typ != termPrefix || !terms[6].inv ||21 terms[7].typ != termSuffix || !terms[7].inv ||22 terms[8].typ != termEqual || terms[8].inv {14 terms[0][0].typ != termFuzzy || terms[0][0].inv ||15 terms[1][0].typ != termExact || terms[1][0].inv ||16 terms[2][0].typ != termPrefix || terms[2][0].inv ||17 terms[3][0].typ != termSuffix || terms[3][0].inv ||18 terms[4][0].typ != termFuzzy || !terms[4][0].inv ||19 terms[5][0].typ != termExact || !terms[5][0].inv ||20 terms[6][0].typ != termPrefix || !terms[6][0].inv ||21 terms[7][0].typ != termSuffix || !terms[7][0].inv ||22 terms[7][1].typ != termEqual || terms[7][1].inv ||23 terms[8][0].typ != termPrefix || terms[8][0].inv ||24 terms[8][1].typ != termExact || terms[8][1].inv ||25 terms[8][2].typ != termSuffix || terms[8][2].inv ||26 terms[8][3].typ != termFuzzy || !terms[8][3].inv {27 t.Errorf("%s", terms)28 }25 for idx, term := range terms {29 for idx, termSet := range terms[:8] {30 term := termSet[0]31 if len(term.text) != 3 {32 t.Errorf("%s", term)33 }34 if idx > 0 && len(term.origText) != 4+idx/5 {35 t.Errorf("%s", term)36 }37 }38 for _, term := range terms[8] {39 if len(term.origText) != 4 {40 t.Errorf("%s", term)41 }42 }43}4445func TestParseTermsExtendedExact(t *testing.T) {46 terms := parseTerms(false, CaseSmart,47 "aaa 'bbb ^ccc ddd$ !eee !'fff !^ggg !hhh$")48 if len(terms) != 8 ||39 terms[0].typ != termExact || terms[0].inv || len(terms[0].text) != 3 ||40 terms[1].typ != termFuzzy || terms[1].inv || len(terms[1].text) != 3 ||41 terms[2].typ != termPrefix || terms[2].inv || len(terms[2].text) != 3 ||42 terms[3].typ != termSuffix || terms[3].inv || len(terms[3].text) != 3 ||43 terms[4].typ != termExact || !terms[4].inv || len(terms[4].text) != 3 ||44 terms[5].typ != termFuzzy || !terms[5].inv || len(terms[5].text) != 3 ||45 terms[6].typ != termPrefix || !terms[6].inv || len(terms[6].text) != 3 ||46 terms[7].typ != termSuffix || !terms[7].inv || len(terms[7].text) != 3 {49 terms[0][0].typ != termExact || terms[0][0].inv || len(terms[0][0].text) != 3 ||50 terms[1][0].typ != termFuzzy || terms[1][0].inv || len(terms[1][0].text) != 3 ||51 terms[2][0].typ != termPrefix || terms[2][0].inv || len(terms[2][0].text) != 3 ||52 terms[3][0].typ != termSuffix || terms[3][0].inv || len(terms[3][0].text) != 3 ||53 terms[4][0].typ != termExact || !terms[4][0].inv || len(terms[4][0].text) != 3 ||54 terms[5][0].typ != termFuzzy || !terms[5][0].inv || len(terms[5][0].text) != 3 ||55 terms[6][0].typ != termPrefix || !terms[6][0].inv || len(terms[6][0].text) != 3 ||56 terms[7][0].typ != termSuffix || !terms[7][0].inv || len(terms[7][0].text) != 3 {57 t.Errorf("%s", terms)58 }59}60
9 unmodified lines
70 clearPatternCache()71 pattern := BuildPattern(true, true, CaseSmart, true,72 []Range{}, Delimiter{}, []rune("'abc"))73 sidx, eidx := algo.ExactMatchNaive(64 pattern.caseSensitive, pattern.forward, []rune("aabbcc abc"), pattern.terms[0].text)74 pattern.caseSensitive, pattern.forward, []rune("aabbcc abc"), pattern.termSets[0][0].text)75 if sidx != 7 || eidx != 10 {66 t.Errorf("%s / %d / %d", pattern.terms, sidx, eidx)76 t.Errorf("%s / %d / %d", pattern.termSets, sidx, eidx)77 }78}7980func TestEqual(t *testing.T) {
2 unmodified lines
83 pattern := BuildPattern(true, true, CaseSmart, true, []Range{}, Delimiter{}, []rune("^AbC$"))8485 match := func(str string, sidxExpected int, eidxExpected int) {86 sidx, eidx := algo.EqualMatch(77 pattern.caseSensitive, pattern.forward, []rune(str), pattern.terms[0].text)87 pattern.caseSensitive, pattern.forward, []rune(str), pattern.termSets[0][0].text)88 if sidx != sidxExpected || eidx != eidxExpected {79 t.Errorf("%s / %d / %d", pattern.terms, sidx, eidx)89 t.Errorf("%s / %d / %d", pattern.termSets, sidx, eidx)90 }91 }92 match("ABC", -1, -1)93 match("AbC", 0, 3)
45 unmodified lines
139 t.Error("Invalid match result", matches)140 }141 }142}143144func TestCacheKey(t *testing.T) {145 test := func(extended bool, patStr string, expected string, cacheable bool) {146 pat := BuildPattern(true, extended, CaseSmart, true, []Range{}, Delimiter{}, []rune(patStr))147 if pat.CacheKey() != expected {148 t.Errorf("Expected: %s, actual: %s", expected, pat.CacheKey())149 }150 if pat.cacheable != cacheable {151 t.Errorf("Expected: %s, actual: %s (%s)", cacheable, pat.cacheable, patStr)152 }153 clearPatternCache()154 }155 test(false, "foo !bar", "foo !bar", true)156 test(false, "foo | bar !baz", "foo | bar !baz", true)157 test(true, "foo bar baz", "foo bar baz", true)158 test(true, "foo !bar", "foo", false)159 test(true, "foo !bar baz", "foo baz", false)160 test(true, "foo | bar baz", "baz", false)161 test(true, "foo | bar | baz", "", false)162 test(true, "foo | bar !baz", "", false)163 test(true, "| | | foo", "foo", true)164}
modified:src/pattern.go+57 added-30 removed
34 unmodified lines
35 caseSensitive bool36 origText []rune37}3839type termSet []term4041// Pattern represents search pattern42type Pattern struct {43 fuzzy bool44 extended bool45 caseSensitive bool46 forward bool47 text []rune46 terms []term47 hasInvTerm bool48 termSets []termSet49 cacheable bool50 delimiter Delimiter51 nth []Range52 procFun map[termType]func(bool, bool, []rune, []rune) (int, int)53}
35 unmodified lines
89 if found {90 return cached91 }9291 caseSensitive, hasInvTerm := true, false92 terms := []term{}93 caseSensitive, cacheable := true, true94 termSets := []termSet{}9596 if extended {95 terms = parseTerms(fuzzy, caseMode, asString)96 for _, term := range terms {97 if term.inv {98 hasInvTerm = true97 termSets = parseTerms(fuzzy, caseMode, asString)98 Loop: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 scope103 if idx > 0 || term.inv {104 cacheable = false105 break Loop106 }107 }108 }109 } else {110 lowerString := strings.ToLower(asString)
9 unmodified lines
120 extended: extended,121 caseSensitive: caseSensitive,122 forward: forward,123 text: []rune(asString),116 terms: terms,117 hasInvTerm: hasInvTerm,124 termSets: termSets,125 cacheable: cacheable,126 nth: nth,127 delimiter: delimiter,128 procFun: make(map[termType]func(bool, bool, []rune, []rune) (int, int))}129
6 unmodified lines
136 _patternCache[asString] = ptr137 return ptr138}139132func parseTerms(fuzzy bool, caseMode Case, str string) []term {140func parseTerms(fuzzy bool, caseMode Case, str string) []termSet {141 tokens := _splitRegex.Split(str, -1)134 terms := []term{}142 sets := []termSet{}143 set := termSet{}144 switchSet := false145 for _, token := range tokens {146 typ, inv, text := termFuzzy, false, token147 lowerText := strings.ToLower(text)148 caseSensitive := caseMode == CaseRespect ||
5 unmodified lines
154 if !fuzzy {155 typ = termExact156 }157158 if text == "|" {159 switchSet = false160 continue161 }162163 if strings.HasPrefix(text, "!") {164 inv = true165 text = text[1:]166 }
20 unmodified lines
187 text = text[:len(text)-1]188 }189190 if len(text) > 0 {176 terms = append(terms, term{191 if switchSet {192 sets = append(sets, set)193 set = termSet{}194 }195 set = append(set, term{196 typ: typ,197 inv: inv,198 text: []rune(text),199 caseSensitive: caseSensitive,200 origText: origText})201 switchSet = true202 }203 }204 if len(set) > 0 {205 sets = append(sets, set)206 }184 return terms207 return sets208}209210// IsEmpty returns true if the pattern is effectively empty211func (p *Pattern) IsEmpty() bool {212 if !p.extended {213 return len(p.text) == 0214 }192 return len(p.terms) == 0215 return len(p.termSets) == 0216}217218// AsString returns the search query in string type219func (p *Pattern) AsString() string {
5 unmodified lines
225 if !p.extended {226 return p.AsString()227 }228 cacheableTerms := []string{}206 for _, term := range p.terms {207 if term.inv {208 continue229 for _, termSet := range p.termSets {230 if len(termSet) == 1 && !termSet[0].inv {231 cacheableTerms = append(cacheableTerms, string(termSet[0].origText))232 }210 cacheableTerms = append(cacheableTerms, string(term.origText))233 }234 return strings.Join(cacheableTerms, " ")235}236
2 unmodified lines
239 space := chunk240241 // ChunkCache: Exact match242 cacheKey := p.CacheKey()221 if !p.hasInvTerm { // Because we're excluding Inv-term from cache key243 if p.cacheable {244 if cached, found := _cache.Find(chunk, cacheKey); found {245 return cached246 }247 }
16 unmodified lines
264 }265266 matches := p.matchChunk(space)267246 if !p.hasInvTerm {268 if p.cacheable {269 _cache.Add(chunk, cacheKey, matches)270 }271 return matches272}
8 unmodified lines
281 }282 }283 } else {284 for _, item := range *chunk {263 if offsets := p.extendedMatch(item); len(offsets) == len(p.terms) {285 if offsets := p.extendedMatch(item); len(offsets) == len(p.termSets) {286 matches = append(matches, dupItem(item, offsets))287 }288 }289 }
6 unmodified lines
296 sidx, _, _ := p.basicMatch(item)297 return sidx >= 0298 }299 offsets := p.extendedMatch(item)278 return len(offsets) == len(p.terms)300 return len(offsets) == len(p.termSets)301}302303func dupItem(item *Item, offsets []Offset) *Item {304 sort.Sort(ByOrder(offsets))
17 unmodified lines
322323func (p *Pattern) extendedMatch(item *Item) []Offset {324 input := p.prepareInput(item)325 offsets := []Offset{}326Loop:327 for _, termSet := range p.termSets {304 for _, term := range p.terms {305 pfun := p.procFun[term.typ]306 if sidx, eidx, tlen := p.iter(pfun, input, term.caseSensitive, p.forward, term.text); sidx >= 0 {307 if term.inv {328 for _, term := range termSet {329 pfun := p.procFun[term.typ]330 if sidx, eidx, tlen := p.iter(pfun, input, term.caseSensitive, p.forward, term.text); sidx >= 0 {331 if term.inv {332 break Loop333 }334 offsets = append(offsets, Offset{int32(sidx), int32(eidx), int32(tlen)})335 break336 } else if term.inv {337 offsets = append(offsets, Offset{0, 0, 0})338 break339 }310 offsets = append(offsets, Offset{int32(sidx), int32(eidx), int32(tlen)})311 } else if term.inv {312 offsets = append(offsets, Offset{0, 0, 0})340 }341 }342 return offsets343}
27 unmodified lines
modified:README.md+8 added
125 unmodified lines
126If you don't prefer fuzzy matching and do not wish to "quote" every word,127start fzf with `-e` or `--exact` option. Note that when `--exact` is set,128`'`-prefix "unquotes" the term.129130A single bar character term acts as an OR operator. For example, the following131query matches entries that start with `core` and end with either `go`, `rb`,132or `py`.133134```135^core go$ | rb$ | py$136```137138#### Environment variables139140- `FZF_DEFAULT_COMMAND`141 - Default command to use when input is tty
249 unmodified lines