diff --git a/src/pattern.go b/src/pattern.go index 93640cb69af..8e6966c34bd 100644 --- a/src/pattern.go +++ b/src/pattern.go @@ -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 } diff --git a/src/tokenizer.go b/src/tokenizer.go index aaddd17d396..573a3576fbb 100644 --- a/src/tokenizer.go +++ b/src/tokenizer.go @@ -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) diff --git a/test/test_filter.rb b/test/test_filter.rb index 718c6e57753..3e604412495 100644 --- a/test/test_filter.rb +++ b/test/test_filter.rb @@ -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 \