Keep separators at start of the croped string

This commit is contained in:
ManyTheFish 2022-03-30 14:00:06 +02:00
parent 29c5f76d7f
commit bd30ee97b8

View File

@ -247,6 +247,15 @@ impl<'t> Matcher<'t, '_> {
last_token_position += 1;
}
// same for start
while let Some(_separator_kind) = first_token_position
.checked_sub(1)
.and_then(|i| self.tokens.get(i))
.and_then(|t| t.is_separator())
{
first_token_position -= 1;
}
(first_token_position, last_token_position)
}
@ -563,7 +572,7 @@ mod tests {
// should crop the phrase instead of croping around the match.
assert_eq!(
&matcher.format(highlight, crop),
"Split The World is a book written by Emily Henry. …"
". Split The World is a book written by Emily Henry. …"
);
// Text containing some matches.
@ -574,7 +583,7 @@ mod tests {
// no highlight should return 10 last words with a marker at the start.
assert_eq!(
&matcher.format(highlight, crop),
"future to build a world with the boy she loves."
" future to build a world with the boy she loves."
);
// Text containing all matches.
@ -585,7 +594,7 @@ mod tests {
// no highlight should return 10 last words with a marker at the start.
assert_eq!(
&matcher.format(highlight, crop),
"she loves. Emily Henry: The Love That Split The World."
" she loves. Emily Henry: The Love That Split The World."
);
// Text containing a match unordered and a match ordered.
@ -596,7 +605,7 @@ mod tests {
// crop should return 10 last words with a marker at the start.
assert_eq!(
&matcher.format(highlight, crop),
"void void void void void split the world void void"
" void void void void void split the world void void"
);
// Text containing matches with diferent density.
@ -607,7 +616,7 @@ mod tests {
// crop should return 10 last words with a marker at the start.
assert_eq!(
&matcher.format(highlight, crop),
"void void void void void split the world void void"
" void void void void void split the world void void"
);
// Text containing matches with same word.
@ -618,7 +627,7 @@ mod tests {
// crop should return 10 last words with a marker at the start.
assert_eq!(
&matcher.format(highlight, crop),
"void void void void void split the world void void"
" void void void void void split the world void void"
);
}
@ -665,7 +674,7 @@ mod tests {
// both should return 10 last words with a marker at the start and highlighted matches.
assert_eq!(
&matcher.format(highlight, crop),
"future to build a <em>world</em> with <em>the</em> boy she loves."
" future to build a <em>world</em> with <em>the</em> boy she loves."
);
// Text containing all matches.
@ -674,7 +683,7 @@ mod tests {
let tokens: Vec<_> = analyzed.tokens().collect();
let mut matcher = builder.build(&tokens[..], text);
// both should return 10 last words with a marker at the start and highlighted matches.
assert_eq!(&matcher.format(highlight, crop), "she loves. Emily Henry: <em>The</em> Love That <em>Split</em> <em>The</em> <em>World</em>.");
assert_eq!(&matcher.format(highlight, crop), " she loves. Emily Henry: <em>The</em> Love That <em>Split</em> <em>The</em> <em>World</em>.");
// Text containing a match unordered and a match ordered.
let text = "The world split void void void void void void void void void split the world void void";
@ -684,7 +693,7 @@ mod tests {
// crop should return 10 last words with a marker at the start.
assert_eq!(
&matcher.format(highlight, crop),
"void void void void void <em>split</em> <em>the</em> <em>world</em> void void"
" void void void void void <em>split</em> <em>the</em> <em>world</em> void void"
);
}
@ -707,13 +716,13 @@ mod tests {
builder.crop_size(2);
let mut matcher = builder.build(&tokens[..], text);
// because crop size < query size, partially format matches.
assert_eq!(&matcher.format(highlight, crop), "split the …");
assert_eq!(&matcher.format(highlight, crop), " split the …");
// set a smaller crop size
builder.crop_size(1);
let mut matcher = builder.build(&tokens[..], text);
// because crop size < query size, partially format matches.
assert_eq!(&matcher.format(highlight, crop), "split …");
assert_eq!(&matcher.format(highlight, crop), " split …");
// set a smaller crop size
builder.crop_size(0);