Fix --with-nth option when query is non-empty

This commit is contained in:
Junegunn Choi 2015-01-11 01:15:44 +09:00
parent f670f4f076
commit 4f40314433
2 changed files with 26 additions and 6 deletions

View File

@ -235,9 +235,10 @@ func (p *Pattern) fuzzyMatch(chunk *Chunk) []*Item {
input := p.prepareInput(item) input := p.prepareInput(item)
if sidx, eidx := p.iter(FuzzyMatch, input, p.text); sidx >= 0 { if sidx, eidx := p.iter(FuzzyMatch, input, p.text); sidx >= 0 {
matches = append(matches, &Item{ matches = append(matches, &Item{
text: item.text, text: item.text,
offsets: []Offset{Offset{int32(sidx), int32(eidx)}}, origText: item.origText,
rank: Rank{0, 0, item.rank.index}}) offsets: []Offset{Offset{int32(sidx), int32(eidx)}},
rank: Rank{0, 0, item.rank.index}})
} }
} }
return matches return matches
@ -262,9 +263,10 @@ func (p *Pattern) extendedMatch(chunk *Chunk) []*Item {
} }
if len(offsets) == len(p.terms) { if len(offsets) == len(p.terms) {
matches = append(matches, &Item{ matches = append(matches, &Item{
text: item.text, text: item.text,
offsets: offsets, origText: item.origText,
rank: Rank{0, 0, item.rank.index}}) offsets: offsets,
rank: Rank{0, 0, item.rank.index}})
} }
} }
return matches return matches

View File

@ -85,3 +85,21 @@ func TestCaseSensitivity(t *testing.T) {
t.Error("Invalid case conversion") 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)
}
}
}