diff: Fix bug with leading blank lines in source
7fb23a9aeea2a989263eedc647dd7ae46d2665af
A hunk's per-side source text is what the syntax highlighter tokenizes, and each `HighlightSource` line is addressed by its source line number. Building that text dropped a leading blank line: the `\n` separator was emitted on `!text.is_empty()`, so a blank first line left `text` empty and the next line was also treated as the first, with no separator. Every later line index then sat one line below its real number, so the interactive viewer rendered each highlighted row with the next line's text. The static renderer never runs the highlight worker, which is why only the interactive pager showed it. Collect the present-on-side line texts and `join` them instead, so blank lines survive and indices stay aligned. Assisted-by: Claude:claude-opus-4.7
1 parent
043c8c95
crates/diff/src/highlight.rs
+13 -16
| 165 | 165 | } |
|
| 166 | 166 | ||
| 167 | 167 | impl Hunk { |
|
| 168 | 168 | /// Builds contiguous source text for one hunk side. |
|
| 169 | 169 | pub fn highlight_source(&self, side: DiffSide) -> HighlightSource { |
|
| 170 | - | let mut text = String::new(); |
|
| 171 | - | push_hunk_source_lines(self, side, &mut text); |
|
| 170 | + | let text = side_lines(self, side).collect::<Vec<_>>().join("\n"); |
|
| 172 | 171 | HighlightSource::new(text, hunk_head(self, side)) |
|
| 173 | 172 | } |
|
| 174 | 173 | } |
|
| 175 | 174 | ||
| 176 | 175 | impl FileDiff { |
| 185 | 184 | ||
| 186 | 185 | /// Builds contiguous source text for one file side. |
|
| 187 | 186 | /// |
|
| 188 | 187 | /// Returns `None` when the file's hunks are not contiguous on `side`. |
|
| 189 | 188 | pub fn highlight_source(&self, side: DiffSide) -> Option<HighlightSource> { |
|
| 190 | - | let mut text = String::new(); |
|
| 189 | + | let mut lines: Vec<&str> = Vec::new(); |
|
| 191 | 190 | let mut head = None; |
|
| 192 | 191 | let mut next_line = None; |
|
| 193 | 192 | ||
| 194 | 193 | for hunk in &self.hunks { |
|
| 195 | 194 | let hunk_head = hunk_head(hunk, side); |
|
| 196 | 195 | if next_line.is_some_and(|line| line != hunk_head) { |
|
| 197 | 196 | return None; |
|
| 198 | 197 | } |
|
| 199 | 198 | head.get_or_insert(hunk_head); |
|
| 200 | - | push_hunk_source_lines(hunk, side, &mut text); |
|
| 199 | + | lines.extend(side_lines(hunk, side)); |
|
| 201 | 200 | let len = hunk |
|
| 202 | 201 | .lines |
|
| 203 | 202 | .iter() |
|
| 204 | 203 | .map(|line| source_len(line, side)) |
|
| 205 | 204 | .sum::<u32>(); |
|
| 206 | 205 | next_line = Some(hunk_head + len); |
|
| 207 | 206 | } |
|
| 208 | - | head.map(|head| HighlightSource::new(text, head)) |
|
| 207 | + | head.map(|head| HighlightSource::new(lines.join("\n"), head)) |
|
| 209 | 208 | } |
|
| 210 | 209 | } |
|
| 211 | 210 | ||
| 212 | - | /// Appends nonblank source-side hunk lines to `text`. |
|
| 213 | - | fn push_hunk_source_lines(hunk: &Hunk, side: DiffSide, text: &mut String) { |
|
| 214 | - | for line in &hunk.lines { |
|
| 215 | - | if source_len(line, side) == 0 { |
|
| 216 | - | continue; |
|
| 217 | - | } |
|
| 218 | - | if !text.is_empty() { |
|
| 219 | - | text.push('\n'); |
|
| 220 | - | } |
|
| 221 | - | text.push_str(&line.text); |
|
| 222 | - | } |
|
| 211 | + | /// Yields the source-side text of every line present on `side`. |
|
| 212 | + | /// |
|
| 213 | + | /// Lines are joined with `\n` by callers; blank lines are kept so a |
|
| 214 | + | /// [`HighlightSource`] line index stays aligned with its source line number. |
|
| 215 | + | fn side_lines(hunk: &Hunk, side: DiffSide) -> impl Iterator<Item = &str> { |
|
| 216 | + | hunk.lines |
|
| 217 | + | .iter() |
|
| 218 | + | .filter(move |line| source_len(line, side) != 0) |
|
| 219 | + | .map(|line| line.text.as_str()) |
|
| 223 | 220 | } |
|
| 224 | 221 | ||
| 225 | 222 | /// Returns the first source line for one hunk side. |
|
| 226 | 223 | fn hunk_head(hunk: &Hunk, side: DiffSide) -> u32 { |
|
| 227 | 224 | match side { |
a.css
+0 -0
a.rs\n",
+0 -0