From dea60b11bca22f4bf886ae7026bac44521038010 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sat, 1 Aug 2015 23:13:24 +0900 Subject: [PATCH] Only consider the lengths of the relevant parts when --nth is set --- src/item.go | 11 ++++++++++- test/test_go.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/item.go b/src/item.go index 1eeb180..68f5297 100644 --- a/src/item.go +++ b/src/item.go @@ -63,7 +63,16 @@ func (i *Item) Rank(cache bool) Rank { var tiebreak uint16 switch rankTiebreak { case byLength: - tiebreak = uint16(len(*i.text)) + // It is guaranteed that .transformed in not null in normal execution + if i.transformed != nil { + lenSum := 0 + for _, token := range *i.transformed { + lenSum += len(*token.text) + } + tiebreak = uint16(lenSum) + } else { + tiebreak = uint16(len(*i.text)) + } case byBegin: // We can't just look at i.offsets[0][0] because it can be an inverse term tiebreak = uint16(minBegin) diff --git a/test/test_go.rb b/test/test_go.rb index 75fc01c..c21c94e 100644 --- a/test/test_go.rb +++ b/test/test_go.rb @@ -501,6 +501,32 @@ class TestGoFZF < TestBase assert_equal input, `cat #{tempname} | #{FZF} -f"!z" -x --tiebreak end`.split($/) end + def test_tiebreak_length_with_nth + input = %w[ + 1:hell + 123:hello + 12345:he + 1234567:h + ] + writelines tempname, input + + output = %w[ + 1:hell + 12345:he + 123:hello + 1234567:h + ] + assert_equal output, `cat #{tempname} | #{FZF} -fh`.split($/) + + output = %w[ + 1234567:h + 12345:he + 1:hell + 123:hello + ] + assert_equal output, `cat #{tempname} | #{FZF} -fh -n2 -d:`.split($/) + end + def test_invalid_cache tmux.send_keys "(echo d; echo D; echo x) | #{fzf '-q d'}", :Enter tmux.until { |lines| lines[-2].include? '2/3' }