diff: Fix sticky collapsed file header rendering
565c2b8146d7956a0b8ba89bfccbd5b6d1793b48
1 parent
7ff8b84d
crates/diff/src/viewer.rs
+38 -25
| 500 | 500 | self.rebuild_index(); |
|
| 501 | 501 | } |
|
| 502 | 502 | ||
| 503 | 503 | /// Prepares a repo refetch request for an asynchronous context update. |
|
| 504 | 504 | pub fn context_request(&self, change: ContextChange) -> Option<ContextRequest> { |
|
| 505 | - | let active = self.active_context_file(); |
|
| 505 | + | let active = self.active_file(); |
|
| 506 | 506 | let file_ids = self.context_file_ids(active); |
|
| 507 | 507 | let plan = self.context.plan(change, &file_ids, &self.document)?; |
|
| 508 | 508 | if plan.files.is_empty() { |
|
| 509 | 509 | return None; |
|
| 510 | 510 | } |
| 531 | 531 | self.replace_file_diffs(update.files); |
|
| 532 | 532 | self.set_state(state); |
|
| 533 | 533 | self.apply(DiffAction::Resize, area) == ViewChange::Changed |
|
| 534 | 534 | } |
|
| 535 | 535 | ||
| 536 | - | /// Returns the active file targeted by context changes. |
|
| 537 | - | fn active_context_file(&self) -> Option<FileId> { |
|
| 538 | - | self.focused_file().or_else(|| self.visible_file()) |
|
| 539 | - | } |
|
| 540 | - | ||
| 541 | 536 | /// Returns the files targeted by a context change. |
|
| 542 | 537 | fn context_file_ids(&self, active: Option<FileId>) -> Vec<FileId> { |
|
| 543 | 538 | active |
|
| 544 | 539 | .filter(|id| self.document.files.iter().any(|file| file.id == *id)) |
|
| 545 | 540 | .or_else(|| self.document.files.first().map(|file| file.id)) |
| 574 | 569 | .find(|file| file.hunks.iter().any(|candidate| candidate.id == hunk)) |
|
| 575 | 570 | .map(|file| file.id), |
|
| 576 | 571 | } |
|
| 577 | 572 | } |
|
| 578 | 573 | ||
| 574 | + | /// Returns the focused file, or the first visible file when focus is unset. |
|
| 575 | + | fn active_file(&self) -> Option<FileId> { |
|
| 576 | + | self.focused_file().or_else(|| self.visible_file()) |
|
| 577 | + | } |
|
| 578 | + | ||
| 579 | 579 | /// Returns the file occupying the first visible diff row. |
|
| 580 | 580 | fn visible_file(&self) -> Option<FileId> { |
|
| 581 | 581 | self.file_row_location(self.state.scroll.offset) |
|
| 582 | 582 | .map(|location| location.file.id) |
|
| 583 | 583 | .or_else(|| self.document.files.first().map(|file| file.id)) |
| 731 | 731 | let previous = self.state.scroll.offset; |
|
| 732 | 732 | self.state |
|
| 733 | 733 | .scroll |
|
| 734 | 734 | .scroll(scroll, self.scroll_rows(diff.rows), diff.rows); |
|
| 735 | 735 | if self.state.scroll.offset != previous |
|
| 736 | - | && layout.tree.is_some() |
|
| 737 | 736 | && let Some(file) = self.visible_file() |
|
| 738 | 737 | { |
|
| 739 | 738 | self.state.focus = Some(DiffFocus::File(file)); |
|
| 740 | - | self.file_tree.select_file(file); |
|
| 739 | + | if layout.tree.is_some() { |
|
| 740 | + | self.file_tree.select_file(file); |
|
| 741 | + | } |
|
| 741 | 742 | } |
|
| 742 | 743 | } |
|
| 743 | 744 | DiffAction::Resize => { |
|
| 744 | 745 | self.state |
|
| 745 | 746 | .scroll |
| 763 | 764 | DiffAction::PreviousHunk => { |
|
| 764 | 765 | self.focus(diff.rows, -1, FocusFilter::Hunk); |
|
| 765 | 766 | self.sync_file_tree(); |
|
| 766 | 767 | } |
|
| 767 | 768 | DiffAction::ToggleFocusedFile => { |
|
| 768 | - | let had_focus = self.focused_file().is_some(); |
|
| 769 | - | self.toggle_focused_file(); |
|
| 770 | - | if had_focus { |
|
| 769 | + | let active_file = self.active_file(); |
|
| 770 | + | self.toggle_active_file(); |
|
| 771 | + | if active_file.is_some() { |
|
| 771 | 772 | self.sync_file_tree(); |
|
| 772 | 773 | } |
|
| 773 | 774 | } |
|
| 774 | 775 | DiffAction::CollapseFocusedFile => { |
|
| 775 | - | let had_focus = self.focused_file().is_some(); |
|
| 776 | + | let focused_file = self.focused_file(); |
|
| 776 | 777 | self.collapse_focused_file(); |
|
| 777 | - | if had_focus { |
|
| 778 | + | if focused_file.is_some() { |
|
| 778 | 779 | self.sync_file_tree(); |
|
| 779 | 780 | } |
|
| 780 | 781 | } |
|
| 781 | 782 | DiffAction::ToggleFileTree => match self.file_tree_visibility { |
|
| 782 | 783 | FileTreeVisibility::Hidden => { |
| 823 | 824 | self.file_tree.expand_folder(); |
|
| 824 | 825 | } |
|
| 825 | 826 | } |
|
| 826 | 827 | ||
| 827 | 828 | if before.invalidates_index(&self.state) { |
|
| 829 | + | let collapsed_file_anchor = self |
|
| 830 | + | .state |
|
| 831 | + | .collapsed_files |
|
| 832 | + | .iter() |
|
| 833 | + | .find(|file| !before.collapsed_files.contains(file)) |
|
| 834 | + | .copied(); |
|
| 828 | 835 | self.rebuild_index(); |
|
| 829 | - | self.state |
|
| 830 | - | .scroll |
|
| 831 | - | .clamp(self.scroll_rows(diff.rows), diff.rows); |
|
| 836 | + | if let Some(file) = collapsed_file_anchor { |
|
| 837 | + | self.show_file(file, diff.rows); |
|
| 838 | + | } else { |
|
| 839 | + | self.state |
|
| 840 | + | .scroll |
|
| 841 | + | .clamp(self.scroll_rows(diff.rows), diff.rows); |
|
| 842 | + | } |
|
| 832 | 843 | } |
|
| 833 | 844 | if self.state == before |
|
| 834 | 845 | && self.file_tree_visibility == before_visibility |
|
| 835 | 846 | && *self.file_tree.state() == before_file_tree |
|
| 836 | 847 | { |
| 1368 | 1379 | } |
|
| 1369 | 1380 | let location = self.file_row_location(scroll)?; |
|
| 1370 | 1381 | if scroll <= location.start { |
|
| 1371 | 1382 | return None; |
|
| 1372 | 1383 | } |
|
| 1384 | + | if self.state.collapsed_files.contains(&location.file.id) { |
|
| 1385 | + | return None; |
|
| 1386 | + | } |
|
| 1373 | 1387 | let file_bottom = location.end.saturating_sub(location.start + 1); |
|
| 1374 | 1388 | let rows = file_bottom |
|
| 1375 | 1389 | .saturating_sub(location.local) |
|
| 1376 | 1390 | .min(STICKY_HEADER_ROWS) |
|
| 1377 | 1391 | .min(visible_rows); |
| 1396 | 1410 | .and_then(|focus| targets.iter().position(|target| target.focus == focus)) |
|
| 1397 | 1411 | .or_else(|| { |
|
| 1398 | 1412 | if filter != FocusFilter::File { |
|
| 1399 | 1413 | return None; |
|
| 1400 | 1414 | } |
|
| 1401 | - | self.file_row_location(self.state.scroll.offset) |
|
| 1402 | - | .and_then(|location| { |
|
| 1403 | - | targets |
|
| 1404 | - | .iter() |
|
| 1405 | - | .position(|target| target.focus == DiffFocus::File(location.file.id)) |
|
| 1406 | - | }) |
|
| 1415 | + | self.active_file().and_then(|file| { |
|
| 1416 | + | targets |
|
| 1417 | + | .iter() |
|
| 1418 | + | .position(|target| target.focus == DiffFocus::File(file)) |
|
| 1419 | + | }) |
|
| 1407 | 1420 | }); |
|
| 1408 | 1421 | let next = match (current, delta.is_negative()) { |
|
| 1409 | 1422 | (Some(index), true) => index.checked_sub(1), |
|
| 1410 | 1423 | (Some(index), false) => index.checked_add(1).filter(|index| *index < targets.len()), |
|
| 1411 | 1424 | (None, true) => targets.len().checked_sub(1), |
| 1461 | 1474 | } |
|
| 1462 | 1475 | } |
|
| 1463 | 1476 | targets |
|
| 1464 | 1477 | } |
|
| 1465 | 1478 | ||
| 1466 | - | /// Toggles the file containing the current focus. |
|
| 1467 | - | fn toggle_focused_file(&mut self) { |
|
| 1468 | - | let Some(file) = self.focused_file() else { |
|
| 1479 | + | /// Toggles the active file between collapsed and expanded states. |
|
| 1480 | + | fn toggle_active_file(&mut self) { |
|
| 1481 | + | let Some(file) = self.active_file() else { |
|
| 1469 | 1482 | return; |
|
| 1470 | 1483 | }; |
|
| 1471 | 1484 | if !self.state.collapsed_files.remove(&file) { |
|
| 1472 | 1485 | self.state.collapsed_files.insert(file); |
|
| 1473 | 1486 | self.state.focus = Some(DiffFocus::File(file)); |
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
crates/diff/tests/golden/sticky_header_before_next_file_64x5.txt
+0 -0
src/a.rs
+0 -0
src/b.rs
+0 -0
README.md
+0 -0
src/app/a.rs
+0 -0
src/lib/b.rs
+0 -0
README.md
+0 -0
a.rs
+0 -0
b.rs
+0 -0
assets/css/style.css
+0 -0
assets/css/style.css
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0