diff: Fix cross-hunk highlight bug
af02fadc5676f2cd8b58e3cc8acf2d19ec146bba
The highlighter was sometimes running across hunks, which doesn't work when you have a hunk opening a string but not closing it.
1 parent
dfff0ae2
crates/diff/src/renderer.rs
+24 -29
| 604 | 604 | if self.highlights.contains_key(&key) || self.pending_highlights.contains(&key) { |
|
| 605 | 605 | continue; |
|
| 606 | 606 | } |
|
| 607 | 607 | self.pending_highlights.insert(key.clone()); |
|
| 608 | 608 | let source = sources |
|
| 609 | - | .entry((meta.file, meta.side)) |
|
| 610 | - | .or_insert_with(|| self.highlight_source(meta.file, meta.side)) |
|
| 609 | + | .entry((meta.file, meta.hunk, meta.side)) |
|
| 610 | + | .or_insert_with(|| self.highlight_source(meta.file, meta.hunk, meta.side)) |
|
| 611 | 611 | .clone(); |
|
| 612 | 612 | requests.push(HighlightRequest { |
|
| 613 | 613 | key, |
|
| 614 | 614 | path: meta.path, |
|
| 615 | 615 | source, |
| 664 | 664 | self.file_row_location(row) |
|
| 665 | 665 | .map(|location| self.render_file_row(location, cols)) |
|
| 666 | 666 | } |
|
| 667 | 667 | ||
| 668 | 668 | /// Returns source-side text for highlighting a file side. |
|
| 669 | - | fn highlight_source(&self, file: FileId, side: DiffSide) -> HighlightSource { |
|
| 669 | + | fn highlight_source(&self, file: FileId, hunk: HunkId, side: DiffSide) -> HighlightSource { |
|
| 670 | 670 | self.document |
|
| 671 | 671 | .files |
|
| 672 | 672 | .iter() |
|
| 673 | 673 | .find(|candidate| candidate.id == file) |
|
| 674 | - | .map(|file| file.highlight_source(side)) |
|
| 674 | + | .map(|file| file.highlight_source(hunk, side)) |
|
| 675 | 675 | .unwrap_or_else(|| HighlightSource::new("", 1)) |
|
| 676 | 676 | } |
|
| 677 | 677 | ||
| 678 | 678 | /// Returns one rendered row inside a file card. |
|
| 679 | 679 | fn render_file_row(&self, location: FileRowLocation<'_>, cols: usize) -> RenderRow { |
| 1309 | 1309 | .clone() |
|
| 1310 | 1310 | .or_else(|| self.header.old_path.clone()), |
|
| 1311 | 1311 | } |
|
| 1312 | 1312 | } |
|
| 1313 | 1313 | ||
| 1314 | - | /// Returns source-side text assembled from this diff's hunks. |
|
| 1315 | - | fn highlight_source(&self, side: DiffSide) -> HighlightSource { |
|
| 1316 | - | let head = self |
|
| 1317 | - | .hunks |
|
| 1318 | - | .first() |
|
| 1319 | - | .map(|hunk| hunk.source_start(side)) |
|
| 1320 | - | .unwrap_or(1); |
|
| 1321 | - | let mut line_no = head; |
|
| 1322 | - | let mut text = String::new(); |
|
| 1323 | - | ||
| 1324 | - | for hunk in &self.hunks { |
|
| 1325 | - | let hunk_line = hunk.source_start(side); |
|
| 1326 | - | while line_no < hunk_line { |
|
| 1327 | - | text.push('\n'); |
|
| 1328 | - | line_no += 1; |
|
| 1329 | - | } |
|
| 1330 | - | for line in &hunk.lines { |
|
| 1331 | - | if line.kind.appears_on(side) { |
|
| 1332 | - | text.push_str(&line.text); |
|
| 1333 | - | text.push('\n'); |
|
| 1334 | - | line_no += 1; |
|
| 1335 | - | } |
|
| 1336 | - | } |
|
| 1337 | - | } |
|
| 1338 | - | HighlightSource::new(text, head) |
|
| 1314 | + | /// Returns source-side text assembled from one hunk. |
|
| 1315 | + | fn highlight_source(&self, hunk: HunkId, side: DiffSide) -> HighlightSource { |
|
| 1316 | + | self.hunks |
|
| 1317 | + | .iter() |
|
| 1318 | + | .find(|candidate| candidate.id == hunk) |
|
| 1319 | + | .map(|hunk| hunk.highlight_source(side)) |
|
| 1320 | + | .unwrap_or_else(|| HighlightSource::new("", 1)) |
|
| 1339 | 1321 | } |
|
| 1340 | 1322 | ||
| 1341 | 1323 | /// Returns a file header line. |
|
| 1342 | 1324 | fn header_line(&self, cols: usize, palette: DiffPalette) -> Line { |
|
| 1343 | 1325 | let mut left = Line::blank(); |
| 1436 | 1418 | match side { |
|
| 1437 | 1419 | DiffSide::Old => self.header.old_line_no, |
|
| 1438 | 1420 | DiffSide::New => self.header.new_line_no, |
|
| 1439 | 1421 | } |
|
| 1440 | 1422 | } |
|
| 1423 | + | ||
| 1424 | + | /// Returns source-side text assembled from this hunk. |
|
| 1425 | + | fn highlight_source(&self, side: DiffSide) -> HighlightSource { |
|
| 1426 | + | let mut text = String::new(); |
|
| 1427 | + | ||
| 1428 | + | for line in &self.lines { |
|
| 1429 | + | if line.kind.appears_on(side) { |
|
| 1430 | + | text.push_str(&line.text); |
|
| 1431 | + | text.push('\n'); |
|
| 1432 | + | } |
|
| 1433 | + | } |
|
| 1434 | + | HighlightSource::new(text, self.source_start(side)) |
|
| 1435 | + | } |
|
| 1441 | 1436 | } |
|
| 1442 | 1437 | ||
| 1443 | 1438 | impl DiffLineKind { |
|
| 1444 | 1439 | /// Returns whether this line exists on a source side. |
|
| 1445 | 1440 | fn appears_on(self, side: DiffSide) -> bool { |
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
src/a.rs
+0 -0
src/b.rs
+0 -0
README.md
+0 -0