Merge pull request #699 from peco/fix-filter-dedup-exclusion

Extract isExcluded helper for negative term check
This commit is contained in:
lestrrat 2026-02-18 14:48:45 +09:00 committed by GitHub
commit 6fdcd40b78
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 69 additions and 16 deletions

View file

@ -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 {

View file

@ -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 {

View file

@ -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
}

View file

@ -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
}