mirror of
https://github.com/Llewellynvdm/fzf.git
synced 2024-11-26 14:56:30 +00:00
Fix compatibility issues with OR operator and inverse terms
This commit is contained in:
parent
e7e86b68f4
commit
31278bcc68
@ -63,6 +63,9 @@ func (item *Item) Rank(cache bool) Rank {
|
|||||||
matchlen += end - begin
|
matchlen += end - begin
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if matchlen == 0 {
|
||||||
|
matchlen = math.MaxUint16
|
||||||
|
}
|
||||||
var tiebreak uint16
|
var tiebreak uint16
|
||||||
switch rankTiebreak {
|
switch rankTiebreak {
|
||||||
case byLength:
|
case byLength:
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package fzf
|
package fzf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"sort"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -42,7 +43,7 @@ func TestItemRank(t *testing.T) {
|
|||||||
strs := [][]rune{[]rune("foo"), []rune("foobar"), []rune("bar"), []rune("baz")}
|
strs := [][]rune{[]rune("foo"), []rune("foobar"), []rune("bar"), []rune("baz")}
|
||||||
item1 := Item{text: strs[0], index: 1, offsets: []Offset{}}
|
item1 := Item{text: strs[0], index: 1, offsets: []Offset{}}
|
||||||
rank1 := item1.Rank(true)
|
rank1 := item1.Rank(true)
|
||||||
if rank1.matchlen != 0 || rank1.tiebreak != 3 || rank1.index != 1 {
|
if rank1.matchlen != math.MaxUint16 || rank1.tiebreak != 3 || rank1.index != 1 {
|
||||||
t.Error(item1.Rank(true))
|
t.Error(item1.Rank(true))
|
||||||
}
|
}
|
||||||
// Only differ in index
|
// Only differ in index
|
||||||
@ -68,9 +69,9 @@ func TestItemRank(t *testing.T) {
|
|||||||
item6 := Item{text: strs[2], rank: Rank{0, 0, 2}, offsets: []Offset{Offset{1, 2}, Offset{6, 7}}}
|
item6 := Item{text: strs[2], rank: Rank{0, 0, 2}, offsets: []Offset{Offset{1, 2}, Offset{6, 7}}}
|
||||||
items = []*Item{&item1, &item2, &item3, &item4, &item5, &item6}
|
items = []*Item{&item1, &item2, &item3, &item4, &item5, &item6}
|
||||||
sort.Sort(ByRelevance(items))
|
sort.Sort(ByRelevance(items))
|
||||||
if items[0] != &item2 || items[1] != &item1 ||
|
if items[0] != &item6 || items[1] != &item4 ||
|
||||||
items[2] != &item6 || items[3] != &item4 ||
|
items[2] != &item5 || items[3] != &item3 ||
|
||||||
items[4] != &item5 || items[5] != &item3 {
|
items[4] != &item2 || items[5] != &item1 {
|
||||||
t.Error(items)
|
t.Error(items)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -323,21 +323,24 @@ func (p *Pattern) basicMatch(item *Item) (int, int, int) {
|
|||||||
func (p *Pattern) extendedMatch(item *Item) []Offset {
|
func (p *Pattern) extendedMatch(item *Item) []Offset {
|
||||||
input := p.prepareInput(item)
|
input := p.prepareInput(item)
|
||||||
offsets := []Offset{}
|
offsets := []Offset{}
|
||||||
Loop:
|
|
||||||
for _, termSet := range p.termSets {
|
for _, termSet := range p.termSets {
|
||||||
|
var offset *Offset
|
||||||
for _, term := range termSet {
|
for _, term := range termSet {
|
||||||
pfun := p.procFun[term.typ]
|
pfun := p.procFun[term.typ]
|
||||||
if sidx, eidx, tlen := p.iter(pfun, input, term.caseSensitive, p.forward, term.text); sidx >= 0 {
|
if sidx, eidx, tlen := p.iter(pfun, input, term.caseSensitive, p.forward, term.text); sidx >= 0 {
|
||||||
if term.inv {
|
if term.inv {
|
||||||
break Loop
|
continue
|
||||||
}
|
}
|
||||||
offsets = append(offsets, Offset{int32(sidx), int32(eidx), int32(tlen)})
|
offset = &Offset{int32(sidx), int32(eidx), int32(tlen)}
|
||||||
break
|
break
|
||||||
} else if term.inv {
|
} else if term.inv {
|
||||||
offsets = append(offsets, Offset{0, 0, 0})
|
offset = &Offset{0, 0, 0}
|
||||||
break
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if offset != nil {
|
||||||
|
offsets = append(offsets, *offset)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return offsets
|
return offsets
|
||||||
}
|
}
|
||||||
|
@ -933,6 +933,12 @@ class TestGoFZF < TestBase
|
|||||||
assert_equal 4, `seq 123 | #{FZF} -f 13 +e`.lines.length
|
assert_equal 4, `seq 123 | #{FZF} -f 13 +e`.lines.length
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_or_operator
|
||||||
|
assert_equal %w[1 5 10], `seq 10 | #{FZF} -f "1 | 5"`.lines.map(&:chomp)
|
||||||
|
assert_equal %w[1 10 2 3 4 5 6 7 8 9],
|
||||||
|
`seq 10 | #{FZF} -f '1 | !1'`.lines.map(&:chomp)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def writelines path, lines
|
def writelines path, lines
|
||||||
File.unlink path while File.exists? path
|
File.unlink path while File.exists? path
|
||||||
|
Loading…
Reference in New Issue
Block a user