Skip to content

Commit 8d30685

Browse files
bartlomiejuclaude
andcommitted
fix: preserve newline-only changes in diff output
Add "\ No newline at end of file" marker (matching git convention) when a line lacks a trailing newline, so that trailing-newline changes are visible in the rendered diff instead of silently disappearing after strip_suffix('\n'). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 6ac9249 commit 8d30685

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

libs/resolver/display.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ impl<'a> DiffBuilder<'a> {
9898
}
9999

100100
fn write_rem_line(&mut self, text: &str) {
101-
let text = text.strip_suffix('\n').unwrap_or(text);
101+
let (text, has_newline) = match text.strip_suffix('\n') {
102+
Some(t) => (t, true),
103+
None => (text, false),
104+
};
102105
write!(
103106
self.output,
104107
"{:width$}{} ",
@@ -110,11 +113,17 @@ impl<'a> DiffBuilder<'a> {
110113
self.output.push_str(&fmt_rem());
111114
self.output.push_str(&fmt_rem_text_highlight(text));
112115
self.output.push('\n');
116+
if !has_newline {
117+
self.write_no_newline_marker();
118+
}
113119
self.orig_line += 1;
114120
}
115121

116122
fn write_add_line(&mut self, text: &str) {
117-
let text = text.strip_suffix('\n').unwrap_or(text);
123+
let (text, has_newline) = match text.strip_suffix('\n') {
124+
Some(t) => (t, true),
125+
None => (text, false),
126+
};
118127
write!(
119128
self.output,
120129
"{:width$}{} ",
@@ -126,8 +135,23 @@ impl<'a> DiffBuilder<'a> {
126135
self.output.push_str(&fmt_add());
127136
self.output.push_str(&fmt_add_text_highlight(text));
128137
self.output.push('\n');
138+
if !has_newline {
139+
self.write_no_newline_marker();
140+
}
129141
self.edit_line += 1;
130142
}
143+
144+
fn write_no_newline_marker(&mut self) {
145+
writeln!(
146+
self.output,
147+
"{:width$}{} {}",
148+
"",
149+
colors::gray(" |"),
150+
colors::gray("\\ No newline at end of file"),
151+
width = self.line_number_width
152+
)
153+
.unwrap();
154+
}
131155
}
132156

133157
fn fmt_add() -> String {
@@ -229,7 +253,9 @@ mod tests {
229253
"console.log(\"Hello World\");",
230254
concat!(
231255
"1 | -console.log('Hello World')\n",
256+
" | \\ No newline at end of file\n",
232257
"1 | +console.log(\"Hello World\");\n",
258+
" | \\ No newline at end of file\n",
233259
),
234260
);
235261

@@ -244,7 +270,9 @@ mod tests {
244270
"6 | -'Hello World'\n",
245271
"2 | +\"Hello World\"\n",
246272
"7 | -)\n",
273+
" | \\ No newline at end of file\n",
247274
"3 | +);\n",
275+
" | \\ No newline at end of file\n",
248276
),
249277
);
250278
}
@@ -254,7 +282,11 @@ mod tests {
254282
run_test(
255283
"test\nsome line text test",
256284
"test\nsome line text test\n",
257-
concat!("2 | -some line text test\n", "2 | +some line text test\n",),
285+
concat!(
286+
"2 | -some line text test\n",
287+
" | \\ No newline at end of file\n",
288+
"2 | +some line text test\n",
289+
),
258290
);
259291
}
260292

0 commit comments

Comments
 (0)