From 4f4031443365659de357ad4da15af8b5e3245137 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sun, 11 Jan 2015 01:15:44 +0900 Subject: [PATCH] Fix --with-nth option when query is non-empty --- src/pattern.go | 14 ++++++++------ src/pattern_test.go | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/pattern.go b/src/pattern.go index 7b29425..93dbaf9 100644 --- a/src/pattern.go +++ b/src/pattern.go @@ -235,9 +235,10 @@ func (p *Pattern) fuzzyMatch(chunk *Chunk) []*Item { input := p.prepareInput(item) if sidx, eidx := p.iter(FuzzyMatch, input, p.text); sidx >= 0 { matches = append(matches, &Item{ - text: item.text, - offsets: []Offset{Offset{int32(sidx), int32(eidx)}}, - rank: Rank{0, 0, item.rank.index}}) + text: item.text, + origText: item.origText, + offsets: []Offset{Offset{int32(sidx), int32(eidx)}}, + rank: Rank{0, 0, item.rank.index}}) } } return matches @@ -262,9 +263,10 @@ func (p *Pattern) extendedMatch(chunk *Chunk) []*Item { } if len(offsets) == len(p.terms) { matches = append(matches, &Item{ - text: item.text, - offsets: offsets, - rank: Rank{0, 0, item.rank.index}}) + text: item.text, + origText: item.origText, + offsets: offsets, + rank: Rank{0, 0, item.rank.index}}) } } return matches diff --git a/src/pattern_test.go b/src/pattern_test.go index a1ce626..a776e30 100644 --- a/src/pattern_test.go +++ b/src/pattern_test.go @@ -85,3 +85,21 @@ func TestCaseSensitivity(t *testing.T) { t.Error("Invalid case conversion") } } + +func TestOrigText(t *testing.T) { + strptr := func(str string) *string { + return &str + } + + pattern := BuildPattern(MODE_EXTENDED, CASE_SMART, []Range{}, nil, []rune("jg")) + for _, fun := range []func(*Chunk) []*Item{pattern.fuzzyMatch, pattern.extendedMatch} { + chunk := Chunk{ + &Item{text: strptr("junegunn"), origText: strptr("junegunn.choi")}, + } + matches := fun(&chunk) + if *matches[0].text != "junegunn" || *matches[0].origText != "junegunn.choi" || + matches[0].offsets[0][0] != 0 || matches[0].offsets[0][1] != 5 { + t.Error("Invalid match result", matches) + } + } +}