mirror of
https://github.com/peco/peco.git
synced 2026-09-10 07:16:29 -04:00
Merge pull request #699 from peco/fix-filter-dedup-exclusion
Extract isExcluded helper for negative term check
This commit is contained in:
commit
6fdcd40b78
|
|
@ -2,10 +2,21 @@ package filter
|
|||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
|
||||
"github.com/peco/peco/pipeline"
|
||||
)
|
||||
|
||||
// isExcluded reports whether text matches any of the given negative regexps.
|
||||
func isExcluded(negRegexps []*regexp.Regexp, text string) bool {
|
||||
for _, rx := range negRegexps {
|
||||
if rx.MatchString(text) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// newContext initializes the context so that it is suitable
|
||||
// to be passed to `Run()`
|
||||
func newContext(ctx context.Context, query string) context.Context {
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package filter
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
|
@ -458,6 +459,61 @@ func TestLiteralHyphenMatching(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestIsExcluded(t *testing.T) {
|
||||
rxFoo := regexp.MustCompile(`(?i)foo`)
|
||||
rxBar := regexp.MustCompile(`(?i)bar`)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
negRegexps []*regexp.Regexp
|
||||
text string
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "nil regexps never excludes",
|
||||
negRegexps: nil,
|
||||
text: "anything",
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "empty regexps never excludes",
|
||||
negRegexps: []*regexp.Regexp{},
|
||||
text: "anything",
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "single match excludes",
|
||||
negRegexps: []*regexp.Regexp{rxFoo},
|
||||
text: "contains foo here",
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "no match does not exclude",
|
||||
negRegexps: []*regexp.Regexp{rxFoo},
|
||||
text: "contains baz here",
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "second regexp matches",
|
||||
negRegexps: []*regexp.Regexp{rxFoo, rxBar},
|
||||
text: "has bar in it",
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "neither regexp matches",
|
||||
negRegexps: []*regexp.Regexp{rxFoo, rxBar},
|
||||
text: "has baz in it",
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
require.Equal(t, tt.want, isExcluded(tt.negRegexps, tt.text))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// testFuzzyMatch tests if non-sorted & sorted Fuzzy filter returns the expected result
|
||||
func testFuzzyMatch(octx context.Context, t *testing.T, filter Filter) {
|
||||
testValues := []struct {
|
||||
|
|
|
|||
|
|
@ -73,14 +73,7 @@ LINE:
|
|||
txt := l.DisplayString()
|
||||
|
||||
// Check negative terms first — skip if any match
|
||||
excluded := false
|
||||
for _, rx := range negRegexps {
|
||||
if rx.MatchString(txt) {
|
||||
excluded = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if excluded {
|
||||
if isExcluded(negRegexps, txt) {
|
||||
continue LINE
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -174,14 +174,7 @@ func (rf *Regexp) applyInternal(ctx context.Context, lines []line.Line, emit fun
|
|||
v := l.DisplayString()
|
||||
|
||||
// Check negative terms first (fail-fast, no index collection)
|
||||
excluded := false
|
||||
for _, rx := range negRegexps {
|
||||
if rx.MatchString(v) {
|
||||
excluded = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if excluded {
|
||||
if isExcluded(negRegexps, v) {
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue