Ignore leading whitespaces when calculating 'begin' index

This commit is contained in:
Junegunn Choi 2016-01-14 01:32:03 +09:00
parent 23244bb410
commit 45143f9541
3 changed files with 68 additions and 2 deletions

View File

@ -4,11 +4,12 @@ CHANGELOG
0.11.2 0.11.2
------ ------
- `--tiebreak` now accepts comma-separated list of sort criteria. - `--tiebreak` now accepts comma-separated list of sort criteria
- Each criterion should appear only once in the list - Each criterion should appear only once in the list
- `index` is only allowed at the end of the list - `index` is only allowed at the end of the list
- `index` is implicitly appended to the list when not specified - `index` is implicitly appended to the list when not specified
- Default is `length` (or equivalently `length,index`) - Default is `length` (or equivalently `length,index`)
- `begin` criterion will ignore leading whitespaces when calculating the index
0.11.1 0.11.1
------ ------

View File

@ -92,7 +92,14 @@ func (item *Item) Rank(cache bool) [5]int32 {
} }
case byBegin: case byBegin:
// We can't just look at item.offsets[0][0] because it can be an inverse term // We can't just look at item.offsets[0][0] because it can be an inverse term
val = int32(minBegin) whitePrefixLen := 0
for idx, r := range item.text {
whitePrefixLen = idx
if idx == minBegin || r != ' ' && r != '\t' {
break
}
}
val = int32(minBegin - whitePrefixLen)
case byEnd: case byEnd:
if prevEnd > 0 { if prevEnd > 0 {
val = int32(1 + len(item.text) - prevEnd) val = int32(1 + len(item.text) - prevEnd)

View File

@ -605,6 +605,64 @@ class TestGoFZF < TestBase
assert_equal by_begin_end, `#{FZF} -ffb --tiebreak=end,length < #{tempname}`.split($/) assert_equal by_begin_end, `#{FZF} -ffb --tiebreak=end,length < #{tempname}`.split($/)
end end
def test_tiebreak_white_prefix
writelines tempname, [
'f o o b a r',
' foo bar',
' foobar',
'----foo bar',
'----foobar',
' foo bar',
' foobar--',
' foobar',
'--foo bar',
'--foobar',
'foobar',
]
assert_equal [
' foobar',
' foobar',
'foobar',
' foobar--',
'--foobar',
'----foobar',
' foo bar',
' foo bar',
'--foo bar',
'----foo bar',
'f o o b a r',
], `#{FZF} -ffb < #{tempname}`.split($/)
assert_equal [
' foobar',
' foobar--',
' foobar',
'foobar',
'--foobar',
'----foobar',
' foo bar',
' foo bar',
'--foo bar',
'----foo bar',
'f o o b a r',
], `#{FZF} -ffb --tiebreak=begin < #{tempname}`.split($/)
assert_equal [
' foobar',
' foobar',
'foobar',
' foobar--',
'--foobar',
'----foobar',
' foo bar',
' foo bar',
'--foo bar',
'----foo bar',
'f o o b a r',
], `#{FZF} -ffb --tiebreak=begin,length < #{tempname}`.split($/)
end
def test_tiebreak_length_with_nth def test_tiebreak_length_with_nth
input = %w[ input = %w[
1:hell 1:hell