summaryrefslogtreecommitdiff
path: root/src/result_test.go
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2022-08-02 13:44:55 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2022-08-02 21:48:19 +0900
commitf0bfeba733f755a81b2e2d327268f2dabae8f684 (patch)
treef772989dd01c4f7cb7f69937932c0ded5d4b0ece /src/result_test.go
parentc3a7a24eeaa5b7451e652df68683b5e8b24cde6b (diff)
downloadfzf-f0bfeba733f755a81b2e2d327268f2dabae8f684.tar.gz
Add new tiebreak: 'chunk'
Favors the line with shorter matched chunk. A chunk is a set of consecutive non-whitespace characters. Unlike the default `length`, this new scheme works well with tabular input. # length prefers item #1, because the whole line is shorter, # chunk prefers item #2, because the matched chunk ("foo") is shorter fzf --height=6 --header-lines=2 --tiebreak=chunk --reverse --query=fo << "EOF" N | Field1 | Field2 | Field3 - | ------ | ------ | ------ 1 | hello | foobar | baz 2 | world | foo | bazbaz EOF If the input does not contain any spaces, `chunk` is equivalent to `length`. But we're not going to set it as the default because it is computationally more expensive. Close #2285 Close #2537 - Not the exact solution to --tiebreak=length not taking --nth into account, but this should work. And the added benefit is that it works well even when --nth is not provided. - Adding a bonus point to the last character of a word didn't turn out great. The order of the result suddenly changes when you type in the last character in the word producing a jarring effect.
Diffstat (limited to 'src/result_test.go')
-rw-r--r--src/result_test.go23
1 files changed, 20 insertions, 3 deletions
diff --git a/src/result_test.go b/src/result_test.go
index 2fd3127f..a930447a 100644
--- a/src/result_test.go
+++ b/src/result_test.go
@@ -54,9 +54,9 @@ func TestResultRank(t *testing.T) {
// FIXME global
sortCriteria = []criterion{byScore, byLength}
- strs := [][]rune{[]rune("foo"), []rune("foobar"), []rune("bar"), []rune("baz")}
+ str := []rune("foo")
item1 := buildResult(
- withIndex(&Item{text: util.RunesToChars(strs[0])}, 1), []Offset{}, 2)
+ withIndex(&Item{text: util.RunesToChars(str)}, 1), []Offset{}, 2)
if item1.points[3] != math.MaxUint16-2 || // Bonus
item1.points[2] != 3 || // Length
item1.points[1] != 0 || // Unused
@@ -65,7 +65,7 @@ func TestResultRank(t *testing.T) {
t.Error(item1)
}
// Only differ in index
- item2 := buildResult(&Item{text: util.RunesToChars(strs[0])}, []Offset{}, 2)
+ item2 := buildResult(&Item{text: util.RunesToChars(str)}, []Offset{}, 2)
items := []Result{item1, item2}
sort.Sort(ByRelevance(items))
@@ -98,6 +98,23 @@ func TestResultRank(t *testing.T) {
}
}
+func TestChunkTiebreak(t *testing.T) {
+ // FIXME global
+ sortCriteria = []criterion{byScore, byChunk}
+
+ score := 100
+ test := func(input string, offset Offset, chunk string) {
+ item := buildResult(withIndex(&Item{text: util.RunesToChars([]rune(input))}, 1), []Offset{offset}, score)
+ if !(item.points[3] == math.MaxUint16-uint16(score) && item.points[2] == uint16(len(chunk))) {
+ t.Error(item.points)
+ }
+ }
+ test("hello foobar goodbye", Offset{8, 9}, "foobar")
+ test("hello foobar goodbye", Offset{7, 18}, "foobar goodbye")
+ test("hello foobar goodbye", Offset{0, 1}, "hello")
+ test("hello foobar goodbye", Offset{5, 7}, "hello foobar") // TBD
+}
+
func TestColorOffset(t *testing.T) {
// ------------ 20 ---- -- ----
// ++++++++ ++++++++++