fixed issue with single letter matches in next

This commit is contained in:
Mikael Fangel 2023-04-04 19:26:57 +02:00 committed by Maas Lalani
parent 582000fa04
commit 111c897007
No known key found for this signature in database
GPG key ID: 5A6ED5CBF1A0A000

View file

@ -65,17 +65,22 @@ func (s *search) NextMatch(m *model) {
// Find the string to highlight.
nextMatch := s.query.FindString(m.content[s.lastMatchLoc:])
s.prevMatch = nextMatch
if nextMatch == "" {
// Start the search from the beginning of the document.
s.lastMatchLoc = 0
m.viewport.GotoTop()
return
}
m.content = m.content[:s.lastMatchLoc] + strings.Replace(m.content[s.lastMatchLoc:], nextMatch, m.matchHighlightStyle.Render(nextMatch), 1)
// Update the postion of the last found match.
nextMatchI := s.query.FindStringIndex(m.content[s.lastMatchLoc:])
s.lastMatchLoc += nextMatchI[1]
leftPadding, _ := utils.LipglossLengthPadding(s.prevMatch, m.matchHighlightStyle)
allMatches := s.query.FindAllSubmatchIndex([]byte(m.content), -1)
for i, subm := range allMatches {
if subm[1]+leftPadding == s.lastMatchLoc {
// Highliht the current match.
m.content = m.content[:allMatches[i+1][0]] + strings.Replace(m.content[allMatches[i+1][0]:], m.content[allMatches[i+1][0]:allMatches[i+1][1]], m.matchHighlightStyle.Render(m.content[allMatches[i+1][0]:allMatches[i+1][1]]), 1)
s.lastMatchLoc = allMatches[i+1][1] + leftPadding
break
}
if i == len(allMatches)-1 {
m.content = m.content[:allMatches[0][0]] + strings.Replace(m.content[allMatches[0][0]:], m.content[allMatches[0][0]:allMatches[0][1]], m.matchHighlightStyle.Render(m.content[allMatches[0][0]:allMatches[0][1]]), 1)
s.lastMatchLoc = allMatches[0][1] + leftPadding
break
}
}
// Update the viewport position.
line := 0