Skip to content

Commit

Permalink
fix: Off-by-one when text change longer than MAX_WORDS_PER_LINE
Browse files Browse the repository at this point in the history
  • Loading branch information
elldritch authored and Wilfred committed Jul 20, 2024
1 parent efe1b10 commit a896a7f
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/line_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ pub(crate) fn change_positions(lhs_src: &str, rhs_src: &str) -> Vec<MatchedPos>
// have a very large number of words, don't diff
// individual words.
if lhs_words.len() > MAX_WORDS_IN_LINE || rhs_words.len() > MAX_WORDS_IN_LINE {
for lhs_pos in lhs_lp.from_region(lhs_offset, lhs_offset + lhs_part.len()) {
for lhs_pos in
lhs_lp.from_region(lhs_offset, lhs_offset + line_len_in_bytes(&lhs_part))
{
mps.push(MatchedPos {
kind: MatchKind::NovelWord {
highlight: TokenKind::Atom(AtomKind::Normal),
Expand Down Expand Up @@ -283,4 +285,12 @@ mod tests {
assert_eq!(positions.len(), 1);
assert!(positions[0].kind.is_novel());
}

#[test]
fn test_positions_max_words() {
let positions = change_positions(&"foo\n".repeat(MAX_WORDS_IN_LINE + 1), "bar\n");

assert_eq!(positions.len(), MAX_WORDS_IN_LINE + 1);
assert!(positions[0].kind.is_novel());
}
}

0 comments on commit a896a7f

Please sign in to comment.