Skip to content

Commit

Permalink
Allow suffix match on --nth with custom --delimiter
Browse files Browse the repository at this point in the history
When --nth is used with a custom --delimiter, the last delimiter was
included in the search scope, forcing you to write the delimiter in
a suffix-match query. This commit removes the last delimiter from the
search scope.

  # No need to write 'bar,$'
  echo foo,bar,baz | fzf --delimiter , --nth 2 --filter 'bar$'

This can be seen as a breaking change, but I'm gonna say it's a bug fix.

Fix #3983
  • Loading branch information
junegunn committed Feb 12, 2025
1 parent 84e2262 commit 9abf2c8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,13 @@ func (p *Pattern) transformInput(item *Item) []Token {

tokens := Tokenize(item.text.ToString(), p.delimiter)
ret := Transform(tokens, p.nth)
// TODO: We could apply StripLastDelimiter to exclude the last delimiter from
// the search allowing suffix match with a string or a regex delimiter.
// Strip the last delimiter to allow suffix match
if len(ret) > 0 && !p.delimiter.IsAwk() {
chars := ret[len(ret)-1].text
stripped := StripLastDelimiter(chars.ToString(), p.delimiter)
newChars := util.ToChars(stringBytes(stripped))
ret[len(ret)-1].text = &newChars
}
item.transformed = &transformed{p.revision, ret}
return ret
}
Expand Down
5 changes: 5 additions & 0 deletions src/tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ type Delimiter struct {
str *string
}

// IsAwk returns true if the delimiter is an AWK-style delimiter
func (d Delimiter) IsAwk() bool {
return d.regex == nil && d.str == nil
}

// String returns the string representation of a Delimiter.
func (d Delimiter) String() string {
return fmt.Sprintf("Delimiter{regex: %v, str: &%q}", d.regex, *d.str)
Expand Down
6 changes: 6 additions & 0 deletions test/test_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def test_read0
`find . -print0 | #{FZF} --read0 -e -f "^#{lines.last}$"`.chomp
end

def test_nth_suffix_match
assert_equal \
'foo,bar,baz',
`echo foo,bar,baz | #{FZF} -d, -f'bar$' -n2`.chomp
end

def test_with_nth_basic
writelines(['hello world ', 'byebye'])
assert_equal \
Expand Down

0 comments on commit 9abf2c8

Please sign in to comment.