diff --git a/milli/src/search/matches/mod.rs b/milli/src/search/matches/mod.rs index 9ab1ef50f..816f5e273 100644 --- a/milli/src/search/matches/mod.rs +++ b/milli/src/search/matches/mod.rs @@ -376,8 +376,10 @@ impl<'t> Matcher<'t, '_> { } formatted.push(self.highlight_prefix); - formatted.push(&self.text[token.byte_start..token.byte_end]); + formatted.push(&self.text[token.byte_start..][..m.match_len]); formatted.push(self.highlight_suffix); + formatted + .push(&self.text[token.byte_start + m.match_len..token.byte_end]); byte_index = token.byte_end; } @@ -516,6 +518,17 @@ mod tests { &matcher.format(highlight, crop), "Natalie risk her future to build a world with the boy she loves." ); + + // Text containing some matches by prefix. + let text = "Natalie risk her future to build a worldle with the boy she loves."; + let analyzed = analyzer.analyze(&text); + let tokens: Vec<_> = analyzed.tokens().collect(); + let mut matcher = builder.build(&tokens[..], text); + // no crop should return complete text with highlighted matches. + assert_eq!( + &matcher.format(highlight, crop), + "Natalie risk her future to build a worldle with the boy she loves." + ); } #[test]