summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2022-08-03 22:18:26 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2022-08-03 22:18:26 +0900
commit38259d0382eba18b66c03d62e35089ec10c24698 (patch)
treeecd12308c7bfe674d701480b2a1512acb93a5ca7
parentf7e7259910393ea3eeeb3ac78913e881d4a86691 (diff)
downloadfzf-38259d0382eba18b66c03d62e35089ec10c24698.tar.gz
Fix incorrect ordering of `--tiebreak=chunk`
-rw-r--r--CHANGELOG.md4
-rw-r--r--src/core.go16
-rw-r--r--src/pattern.go8
-rw-r--r--src/pattern_test.go22
-rwxr-xr-xtest/test_go.rb10
5 files changed, 37 insertions, 23 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 59de9a9e..a3ea8abd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,10 @@
CHANGELOG
=========
+0.32.1
+------
+- Fixed incorrect ordering of `--tiebreak=chunk`
+
0.32.0
------
- Updated the scoring algorithm
diff --git a/src/core.go b/src/core.go
index 17198f5b..b4bccfbe 100644
--- a/src/core.go
+++ b/src/core.go
@@ -146,18 +146,20 @@ func Run(opts *Options, version string, revision string) {
// Matcher
forward := true
- for _, cri := range opts.Criteria[1:] {
- if cri == byEnd {
+ withPos := false
+ for idx := len(opts.Criteria) - 1; idx > 0; idx-- {
+ switch opts.Criteria[idx] {
+ case byChunk:
+ withPos = true
+ case byEnd:
forward = false
- break
- }
- if cri == byBegin {
- break
+ case byBegin:
+ forward = true
}
}
patternBuilder := func(runes []rune) *Pattern {
return BuildPattern(
- opts.Fuzzy, opts.FuzzyAlgo, opts.Extended, opts.Case, opts.Normalize, forward,
+ opts.Fuzzy, opts.FuzzyAlgo, opts.Extended, opts.Case, opts.Normalize, forward, withPos,
opts.Filter == nil, opts.Nth, opts.Delimiter, runes)
}
matcher := NewMatcher(patternBuilder, sort, opts.Tac, eventBox)
diff --git a/src/pattern.go b/src/pattern.go
index 4a7a87a6..95f23556 100644
--- a/src/pattern.go
+++ b/src/pattern.go
@@ -51,6 +51,7 @@ type Pattern struct {
caseSensitive bool
normalize bool
forward bool
+ withPos bool
text []rune
termSets []termSet
sortable bool
@@ -85,7 +86,7 @@ func clearChunkCache() {
// BuildPattern builds Pattern object from the given arguments
func BuildPattern(fuzzy bool, fuzzyAlgo algo.Algo, extended bool, caseMode Case, normalize bool, forward bool,
- cacheable bool, nth []Range, delimiter Delimiter, runes []rune) *Pattern {
+ withPos bool, cacheable bool, nth []Range, delimiter Delimiter, runes []rune) *Pattern {
var asString string
if extended {
@@ -145,6 +146,7 @@ func BuildPattern(fuzzy bool, fuzzyAlgo algo.Algo, extended bool, caseMode Case,
caseSensitive: caseSensitive,
normalize: normalize,
forward: forward,
+ withPos: withPos,
text: []rune(asString),
termSets: termSets,
sortable: sortable,
@@ -302,13 +304,13 @@ func (p *Pattern) matchChunk(chunk *Chunk, space []Result, slab *util.Slab) []Re
if space == nil {
for idx := 0; idx < chunk.count; idx++ {
- if match, _, _ := p.MatchItem(&chunk.items[idx], false, slab); match != nil {
+ if match, _, _ := p.MatchItem(&chunk.items[idx], p.withPos, slab); match != nil {
matches = append(matches, *match)
}
}
} else {
for _, result := range space {
- if match, _, _ := p.MatchItem(result.item, false, slab); match != nil {
+ if match, _, _ := p.MatchItem(result.item, p.withPos, slab); match != nil {
matches = append(matches, *match)
}
}
diff --git a/src/pattern_test.go b/src/pattern_test.go
index b95d1513..5eb5f6d7 100644
--- a/src/pattern_test.go
+++ b/src/pattern_test.go
@@ -67,7 +67,7 @@ func TestParseTermsEmpty(t *testing.T) {
func TestExact(t *testing.T) {
defer clearPatternCache()
clearPatternCache()
- pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, true,
+ pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, false, true,
[]Range{}, Delimiter{}, []rune("'abc"))
chars := util.ToChars([]byte("aabbcc abc"))
res, pos := algo.ExactMatchNaive(
@@ -83,7 +83,7 @@ func TestExact(t *testing.T) {
func TestEqual(t *testing.T) {
defer clearPatternCache()
clearPatternCache()
- pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("^AbC$"))
+ pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, false, true, []Range{}, Delimiter{}, []rune("^AbC$"))
match := func(str string, sidxExpected int, eidxExpected int) {
chars := util.ToChars([]byte(str))
@@ -106,17 +106,17 @@ func TestEqual(t *testing.T) {
func TestCaseSensitivity(t *testing.T) {
defer clearPatternCache()
clearPatternCache()
- pat1 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("abc"))
+ pat1 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, false, true, false, true, []Range{}, Delimiter{}, []rune("abc"))
clearPatternCache()
- pat2 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("Abc"))
+ pat2 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, false, true, false, true, []Range{}, Delimiter{}, []rune("Abc"))
clearPatternCache()
- pat3 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, false, true, true, []Range{}, Delimiter{}, []rune("abc"))
+ pat3 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, false, true, false, true, []Range{}, Delimiter{}, []rune("abc"))
clearPatternCache()
- pat4 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, false, true, true, []Range{}, Delimiter{}, []rune("Abc"))
+ pat4 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, false, true, false, true, []Range{}, Delimiter{}, []rune("Abc"))
clearPatternCache()
- pat5 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, false, true, true, []Range{}, Delimiter{}, []rune("abc"))
+ pat5 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, false, true, false, true, []Range{}, Delimiter{}, []rune("abc"))
clearPatternCache()
- pat6 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, false, true, true, []Range{}, Delimiter{}, []rune("Abc"))
+ pat6 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, false, true, false, true, []Range{}, Delimiter{}, []rune("Abc"))
if string(pat1.text) != "abc" || pat1.caseSensitive != false ||
string(pat2.text) != "Abc" || pat2.caseSensitive != true ||
@@ -129,7 +129,7 @@ func TestCaseSensitivity(t *testing.T) {
}
func TestOrigTextAndTransformed(t *testing.T) {
- pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("jg"))
+ pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, false, true, []Range{}, Delimiter{}, []rune("jg"))
tokens := Tokenize("junegunn", Delimiter{})
trans := Transform(tokens, []Range{{1, 1}})
@@ -164,7 +164,7 @@ func TestOrigTextAndTransformed(t *testing.T) {
func TestCacheKey(t *testing.T) {
test := func(extended bool, patStr string, expected string, cacheable bool) {
clearPatternCache()
- pat := BuildPattern(true, algo.FuzzyMatchV2, extended, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune(patStr))
+ pat := BuildPattern(true, algo.FuzzyMatchV2, extended, CaseSmart, false, true, false, true, []Range{}, Delimiter{}, []rune(patStr))
if pat.CacheKey() != expected {
t.Errorf("Expected: %s, actual: %s", expected, pat.CacheKey())
}
@@ -188,7 +188,7 @@ func TestCacheKey(t *testing.T) {
func TestCacheable(t *testing.T) {
test := func(fuzzy bool, str string, expected string, cacheable bool) {
clearPatternCache()
- pat := BuildPattern(fuzzy, algo.FuzzyMatchV2, true, CaseSmart, true, true, true, []Range{}, Delimiter{}, []rune(str))
+ pat := BuildPattern(fuzzy, algo.FuzzyMatchV2, true, CaseSmart, true, true, false, true, []Range{}, Delimiter{}, []rune(str))
if pat.CacheKey() != expected {
t.Errorf("Expected: %s, actual: %s", expected, pat.CacheKey())
}
diff --git a/test/test_go.rb b/test/test_go.rb
index aa549ad3..6371e0e2 100755
--- a/test/test_go.rb
+++ b/test/test_go.rb
@@ -756,7 +756,7 @@ class TestGoFZF < TestBase
def test_tiebreak_chunk
writelines(tempname, [
- '1 foobarbaz baz',
+ '1 foobarbaz ba',
'2 foobar baz',
'3 foo barbaz'
])
@@ -764,8 +764,14 @@ class TestGoFZF < TestBase
assert_equal [
'3 foo barbaz',
'2 foobar baz',
- '1 foobarbaz baz'
+ '1 foobarbaz ba'
], `#{FZF} -fo --tiebreak=chunk < #{tempname}`.lines(chomp: true)
+
+ assert_equal [
+ '1 foobarbaz ba',
+ '2 foobar baz',
+ '3 foo barbaz'
+ ], `#{FZF} -fba --tiebreak=chunk < #{tempname}`.lines(chomp: true)
end
def test_invalid_cache