fix(diff): preserve collapsed EOF marker

This commit is contained in:
Miguel Palhas
2026-09-05 09:33:58 +01:00
parent f96f94db8f
commit eafeac5c05
3 changed files with 55 additions and 5 deletions
+34 -4
View File
@@ -678,10 +678,11 @@ fn render_diff_ops(ops: &[DiffOp<'_>]) -> Vec<String> {
for op in run.iter().take(keep_head) {
push_diff_op(&mut rendered, *op);
}
rendered.push(format!(
"… {} unchanged lines …",
run.len() - keep_head - keep_tail
));
let collapsed = &run[keep_head..run.len() - keep_tail];
rendered.push(format!("… {} unchanged lines …", collapsed.len()));
if collapsed.iter().any(|op| !op.line.terminated) {
rendered.push("\\ No newline at end of file".into());
}
for op in run.iter().skip(run.len() - keep_tail) {
push_diff_op(&mut rendered, *op);
}
@@ -1055,6 +1056,35 @@ mod tests {
" line11",
" line12",
"… 7 unchanged lines …",
"\\ No newline at end of file",
]
);
}
#[test]
fn structured_diff_preserves_eof_marker_through_trailing_collapse() {
let trailing = (0..10).map(|index| format!("t{index}")).collect::<Vec<_>>();
let old = ["OLD".into()]
.into_iter()
.chain(trailing.iter().cloned())
.collect::<Vec<_>>()
.join("\n");
let new = ["NEW".into()]
.into_iter()
.chain(trailing)
.collect::<Vec<_>>()
.join("\n");
assert_eq!(
render_diff(Some(&old), &new, None),
[
"- OLD",
"+ NEW",
" t0",
" t1",
" t2",
"… 7 unchanged lines …",
"\\ No newline at end of file",
]
);
}