diff: Make commit headers selectable
c25693bd05533ad916d184f9be30732eedc10a07
Treat the commit preamble as the boundary before the first hunk so keyboard navigation can return to the document context instead of stopping on a patch detail. Keep commit focus conditional on commit metadata so patch-only views retain their existing behavior. Assisted-by: Codex:gpt-5.6-sol
1 parent
d03a156f
crates/diff/src/viewer.rs
+70 -35
| 105 | 105 | /// Mutable read-only diff viewport state. |
|
| 106 | 106 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
|
| 107 | 107 | pub struct DiffState { |
|
| 108 | 108 | /// Vertical viewport over the rendered document. |
|
| 109 | 109 | pub scroll: Viewport, |
|
| 110 | - | /// Currently focused file or hunk. |
|
| 110 | + | /// Currently focused commit, file, or hunk. |
|
| 111 | 111 | pub focus: Option<DiffFocus>, |
|
| 112 | 112 | } |
|
| 113 | 113 | ||
| 114 | 114 | /// Focus target inside a diff document. |
|
| 115 | 115 | #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] |
|
| 116 | 116 | pub enum DiffFocus { |
|
| 117 | + | /// Focus the commit header. |
|
| 118 | + | Commit, |
|
| 117 | 119 | /// Focus a file header. |
|
| 118 | 120 | File(FileId), |
|
| 119 | 121 | /// Focus a hunk header. |
|
| 120 | 122 | Hunk(HunkId), |
|
| 121 | 123 | } |
| 672 | 674 | let summaries = document |
|
| 673 | 675 | .files |
|
| 674 | 676 | .iter() |
|
| 675 | 677 | .map(|file| (file.id, FileSummary::from(file))) |
|
| 676 | 678 | .collect(); |
|
| 677 | - | let state = DiffState::default(); |
|
| 679 | + | let state = DiffState { |
|
| 680 | + | focus: document.commit.is_some().then_some(DiffFocus::Commit), |
|
| 681 | + | ..DiffState::default() |
|
| 682 | + | }; |
|
| 678 | 683 | ||
| 679 | 684 | let mut viewer = Self { |
|
| 680 | 685 | document, |
|
| 681 | 686 | summaries, |
|
| 682 | 687 | expanded_context: BTreeSet::new(), |
| 951 | 956 | if layout.tree.is_some() { |
|
| 952 | 957 | self.sync_file_tree(); |
|
| 953 | 958 | } |
|
| 954 | 959 | } |
|
| 955 | 960 | ||
| 956 | - | /// Returns the hunk before the focused hunk in the same file. |
|
| 961 | + | /// Returns the restoration target before the focused hunk. |
|
| 957 | 962 | fn focused_hunk_predecessor(&self) -> Option<DiffFocus> { |
|
| 958 | 963 | let Some(DiffFocus::Hunk(focused)) = self.state.focus else { |
|
| 959 | 964 | return None; |
|
| 960 | 965 | }; |
|
| 961 | 966 | let file = self |
|
| 962 | 967 | .document |
|
| 963 | 968 | .files |
|
| 964 | 969 | .iter() |
|
| 965 | 970 | .find(|file| file.hunks.iter().any(|hunk| hunk.id == focused))?; |
|
| 966 | 971 | let index = file.hunks.iter().position(|hunk| hunk.id == focused)?; |
|
| 967 | - | let predecessor = file.hunks.get(index.checked_sub(1)?)?; |
|
| 972 | + | if let Some(predecessor) = index.checked_sub(1).and_then(|index| file.hunks.get(index)) { |
|
| 973 | + | return Some(DiffFocus::Hunk(predecessor.id)); |
|
| 974 | + | } |
|
| 975 | + | let first_hunk = self |
|
| 976 | + | .document |
|
| 977 | + | .files |
|
| 978 | + | .iter() |
|
| 979 | + | .flat_map(|file| file.hunks.iter()) |
|
| 980 | + | .next(); |
|
| 968 | 981 | ||
| 969 | - | Some(DiffFocus::Hunk(predecessor.id)) |
|
| 982 | + | (self.document.commit.is_some() && first_hunk.is_some_and(|hunk| hunk.id == focused)) |
|
| 983 | + | .then_some(DiffFocus::Commit) |
|
| 970 | 984 | } |
|
| 971 | 985 | ||
| 972 | - | /// Returns a visible focused hunk and its terminal row. |
|
| 986 | + | /// Returns a visible commit or hunk focus and its terminal row. |
|
| 973 | 987 | fn focus_anchor(&self, visible_rows: usize) -> Option<(DiffFocus, usize)> { |
|
| 974 | 988 | let focus = self.state.focus?; |
|
| 975 | - | self.focus_targets(FocusFilter::Hunk) |
|
| 989 | + | self.focus_targets(FocusFilter::HunkNavigation) |
|
| 976 | 990 | .into_iter() |
|
| 977 | 991 | .find(|target| target.focus == focus) |
|
| 978 | 992 | .and_then(|target| target.row.checked_sub(self.state.scroll.offset)) |
|
| 979 | 993 | .filter(|screen_row| *screen_row < visible_rows) |
|
| 980 | 994 | .map(|screen_row| (focus, screen_row)) |
|
| 981 | 995 | } |
|
| 982 | 996 | ||
| 983 | 997 | /// Returns the scroll offset that restores a captured focus anchor. |
|
| 984 | 998 | fn focus_anchor_offset(&self, anchor: (DiffFocus, usize)) -> Option<usize> { |
|
| 985 | 999 | let (focus, screen_row) = anchor; |
|
| 986 | - | self.focus_targets(FocusFilter::Hunk) |
|
| 1000 | + | self.focus_targets(FocusFilter::HunkNavigation) |
|
| 987 | 1001 | .into_iter() |
|
| 988 | 1002 | .find(|target| target.focus == focus) |
|
| 989 | 1003 | .map(|target| target.row.saturating_sub(screen_row)) |
|
| 990 | 1004 | } |
|
| 991 | 1005 | ||
| 992 | 1006 | /// Returns whether a focus target is visible in the current document. |
|
| 993 | 1007 | fn has_focus(&self, focus: DiffFocus) -> bool { |
|
| 994 | 1008 | match focus { |
|
| 1009 | + | DiffFocus::Commit => self.document.commit.is_some(), |
|
| 995 | 1010 | DiffFocus::File(file) => self.index.files.iter().any(|indexed| { |
|
| 996 | 1011 | self.document |
|
| 997 | 1012 | .files |
|
| 998 | 1013 | .get(indexed.file_index) |
|
| 999 | 1014 | .is_some_and(|candidate| candidate.id == file) |
| 1023 | 1038 | ||
| 1024 | 1039 | /// Sets the focused file or hunk. |
|
| 1025 | 1040 | pub fn set_focus(&mut self, focus: Option<DiffFocus>) { |
|
| 1026 | 1041 | let focus = focus.filter(|focus| self.has_focus(*focus)); |
|
| 1027 | 1042 | if let Some(DiffFocus::Hunk(hunk)) = focus |
|
| 1028 | - | && let Some(file) = self.document.files.iter().find(|file| { |
|
| 1029 | - | file.hunks |
|
| 1030 | - | .iter() |
|
| 1031 | - | .any(|candidate| candidate.id == hunk) |
|
| 1032 | - | }) |
|
| 1043 | + | && let Some(file) = self |
|
| 1044 | + | .document |
|
| 1045 | + | .files |
|
| 1046 | + | .iter() |
|
| 1047 | + | .find(|file| file.hunks.iter().any(|candidate| candidate.id == hunk)) |
|
| 1033 | 1048 | { |
|
| 1034 | 1049 | self.hunk_focus.insert(file.id, hunk); |
|
| 1035 | 1050 | } |
|
| 1036 | 1051 | self.state.focus = focus; |
|
| 1037 | 1052 | } |
|
| 1038 | 1053 | ||
| 1039 | 1054 | /// Returns the file containing the current focus. |
|
| 1040 | 1055 | pub fn focused_file(&self) -> Option<FileId> { |
|
| 1041 | 1056 | match self.state.focus? { |
|
| 1057 | + | DiffFocus::Commit => None, |
|
| 1042 | 1058 | DiffFocus::File(file) => Some(file), |
|
| 1043 | 1059 | DiffFocus::Hunk(hunk) => self |
|
| 1044 | 1060 | .document |
|
| 1045 | 1061 | .files |
|
| 1046 | 1062 | .iter() |
| 1240 | 1256 | DiffAction::FocusPrevious => { |
|
| 1241 | 1257 | self.focus(diff.rows, -1, FocusFilter::File); |
|
| 1242 | 1258 | self.sync_file_tree(); |
|
| 1243 | 1259 | } |
|
| 1244 | 1260 | DiffAction::NextHunk => { |
|
| 1245 | - | self.focus(diff.rows, 1, FocusFilter::Hunk); |
|
| 1246 | - | self.sync_file_tree(); |
|
| 1261 | + | self.focus(diff.rows, 1, FocusFilter::HunkNavigation); |
|
| 1262 | + | if self.state.focus != before.focus { |
|
| 1263 | + | self.sync_file_tree(); |
|
| 1264 | + | } |
|
| 1247 | 1265 | } |
|
| 1248 | 1266 | DiffAction::PreviousHunk => { |
|
| 1249 | - | self.focus(diff.rows, -1, FocusFilter::Hunk); |
|
| 1250 | - | self.sync_file_tree(); |
|
| 1267 | + | self.focus(diff.rows, -1, FocusFilter::HunkNavigation); |
|
| 1268 | + | if self.state.focus != before.focus { |
|
| 1269 | + | self.sync_file_tree(); |
|
| 1270 | + | } |
|
| 1251 | 1271 | } |
|
| 1252 | 1272 | DiffAction::ToggleFileTree => match self.file_tree_visibility { |
|
| 1253 | 1273 | FileTreeVisibility::Hidden => { |
|
| 1254 | 1274 | self.file_tree_visibility = FileTreeVisibility::Visible; |
|
| 1255 | 1275 | if self.layout(area).tree.is_some() { |
| 1599 | 1619 | ||
| 1600 | 1620 | /// Returns one rendered row by global row index. |
|
| 1601 | 1621 | fn render_row(&self, row: usize, cols: usize) -> Option<RenderRow> { |
|
| 1602 | 1622 | let cols = cols.max(MIN_RENDER_COLS); |
|
| 1603 | 1623 | if let Some(line) = self.preamble.get(row) { |
|
| 1604 | - | return Some(RenderRow { |
|
| 1605 | - | line: line.clone(), |
|
| 1606 | - | diff: None, |
|
| 1607 | - | }); |
|
| 1624 | + | let line = line.clone(); |
|
| 1625 | + | let line = if self.dims_unfocused(DiffFocus::Commit) { |
|
| 1626 | + | Self::muted_line(line) |
|
| 1627 | + | } else { |
|
| 1628 | + | line |
|
| 1629 | + | }; |
|
| 1630 | + | return Some(RenderRow { line, diff: None }); |
|
| 1608 | 1631 | } |
|
| 1609 | 1632 | self.file_row_location(row) |
|
| 1610 | 1633 | .map(|location| self.render_file_row(location, cols)) |
|
| 1611 | 1634 | } |
|
| 1612 | 1635 |
| 1694 | 1717 | } |
|
| 1695 | 1718 | let expanded = row |
|
| 1696 | 1719 | .diff |
|
| 1697 | 1720 | .as_ref() |
|
| 1698 | 1721 | .is_some_and(|meta| self.expanded_context.contains(&meta.row)); |
|
| 1699 | - | if expanded || self.dims_unfocused_hunk(hunk.id) { |
|
| 1700 | - | row.line = Self::muted_hunk_line(row.line); |
|
| 1722 | + | if expanded || self.dims_unfocused(DiffFocus::Hunk(hunk.id)) { |
|
| 1723 | + | row.line = Self::muted_line(row.line); |
|
| 1701 | 1724 | } |
|
| 1702 | 1725 | return RenderRow { |
|
| 1703 | 1726 | line: wrap_child(row.line), |
|
| 1704 | 1727 | diff: row.diff, |
|
| 1705 | 1728 | }; |
| 1769 | 1792 | self.index = RowIndex { files, rows }; |
|
| 1770 | 1793 | let document = &self.document; |
|
| 1771 | 1794 | self.hunk_focus.retain(|file, hunk| { |
|
| 1772 | 1795 | document.files.iter().any(|candidate| { |
|
| 1773 | 1796 | candidate.id == *file |
|
| 1774 | - | && candidate.hunks.iter().any(|candidate| candidate.id == *hunk) |
|
| 1797 | + | && candidate |
|
| 1798 | + | .hunks |
|
| 1799 | + | .iter() |
|
| 1800 | + | .any(|candidate| candidate.id == *hunk) |
|
| 1775 | 1801 | }) |
|
| 1776 | 1802 | }); |
|
| 1777 | 1803 | let focus = self.state.focus; |
|
| 1778 | 1804 | self.set_focus(focus); |
|
| 1779 | 1805 | } |
| 1990 | 2016 | }) |
|
| 1991 | 2017 | }); |
|
| 1992 | 2018 | let next = match (current, filter, delta.is_negative()) { |
|
| 1993 | 2019 | (Some(index), _, true) => index.checked_sub(1), |
|
| 1994 | 2020 | (Some(index), _, false) => index.checked_add(1).filter(|index| *index < targets.len()), |
|
| 1995 | - | (None, FocusFilter::Hunk, true) => targets |
|
| 2021 | + | (None, FocusFilter::HunkNavigation, true) => targets |
|
| 1996 | 2022 | .partition_point(|target| target.row <= self.state.scroll.offset) |
|
| 1997 | 2023 | .checked_sub(1), |
|
| 1998 | - | (None, FocusFilter::Hunk, false) => { |
|
| 2024 | + | (None, FocusFilter::HunkNavigation, false) => { |
|
| 1999 | 2025 | let index = targets.partition_point(|target| target.row < self.state.scroll.offset); |
|
| 2000 | 2026 | (index < targets.len()).then_some(index) |
|
| 2001 | 2027 | } |
|
| 2002 | 2028 | (None, FocusFilter::File, true) => targets.len().checked_sub(1), |
|
| 2003 | 2029 | (None, FocusFilter::File, false) => Some(0), |
| 2023 | 2049 | .map_or(target.focus, DiffFocus::Hunk) |
|
| 2024 | 2050 | } else { |
|
| 2025 | 2051 | target.focus |
|
| 2026 | 2052 | }; |
|
| 2027 | 2053 | let target = if matches!(focus, DiffFocus::Hunk(_)) { |
|
| 2028 | - | self.focus_targets(FocusFilter::Hunk) |
|
| 2054 | + | self.focus_targets(FocusFilter::HunkNavigation) |
|
| 2029 | 2055 | .into_iter() |
|
| 2030 | 2056 | .find(|target| target.focus == focus) |
|
| 2031 | 2057 | } else { |
|
| 2032 | 2058 | Some(*target) |
|
| 2033 | 2059 | }; |
| 2059 | 2085 | ||
| 2060 | 2086 | /// Returns focusable targets in rendered row order. |
|
| 2061 | 2087 | fn focus_targets(&self, filter: FocusFilter) -> Vec<FocusTarget> { |
|
| 2062 | 2088 | let mut targets = Vec::new(); |
|
| 2063 | 2089 | ||
| 2090 | + | if filter == FocusFilter::HunkNavigation && self.document.commit.is_some() { |
|
| 2091 | + | targets.push(FocusTarget { |
|
| 2092 | + | focus: DiffFocus::Commit, |
|
| 2093 | + | row: 0, |
|
| 2094 | + | height: self.preamble.len(), |
|
| 2095 | + | file_start: 0, |
|
| 2096 | + | }); |
|
| 2097 | + | } |
|
| 2098 | + | ||
| 2064 | 2099 | for indexed in &self.index.files { |
|
| 2065 | 2100 | let Some(file) = self.document.files.get(indexed.file_index) else { |
|
| 2066 | 2101 | continue; |
|
| 2067 | 2102 | }; |
|
| 2068 | 2103 | if filter == FocusFilter::File { |
| 2071 | 2106 | row: indexed.start, |
|
| 2072 | 2107 | height: 1, |
|
| 2073 | 2108 | file_start: indexed.start, |
|
| 2074 | 2109 | }); |
|
| 2075 | 2110 | } |
|
| 2076 | - | if filter == FocusFilter::Hunk { |
|
| 2111 | + | if filter == FocusFilter::HunkNavigation { |
|
| 2077 | 2112 | for (hunk, local) in file.hunk_body_rows(self.hunk_gaps(file.id)) { |
|
| 2078 | 2113 | let leading = hunk |
|
| 2079 | 2114 | .lines |
|
| 2080 | 2115 | .iter() |
|
| 2081 | 2116 | .take_while(|line| self.expanded_context.contains(&line.id)) |
| 2328 | 2363 | /// Returns a marker cell. |
|
| 2329 | 2364 | fn marker_cell(marker: &str, foreground: Color) -> Line { |
|
| 2330 | 2365 | Line::new(Label::new(marker).fg(foreground)).padded(MARKER_WIDTH) |
|
| 2331 | 2366 | } |
|
| 2332 | 2367 | ||
| 2333 | - | /// Returns whether hunk focus should dim this non-focused hunk. |
|
| 2334 | - | fn dims_unfocused_hunk(&self, hunk: HunkId) -> bool { |
|
| 2368 | + | /// Returns whether focus styling should dim an unselected target. |
|
| 2369 | + | fn dims_unfocused(&self, target: DiffFocus) -> bool { |
|
| 2335 | 2370 | self.options.hunk_focus == HunkFocus::DimUnfocused |
|
| 2336 | - | && matches!(self.state.focus, Some(DiffFocus::Hunk(focused)) if focused != hunk) |
|
| 2371 | + | && matches!(self.state.focus, Some(focus) if focus != target) |
|
| 2337 | 2372 | } |
|
| 2338 | 2373 | ||
| 2339 | - | /// Returns a hunk row with every label rendered dimly. |
|
| 2340 | - | fn muted_hunk_line(line: Line) -> Line { |
|
| 2374 | + | /// Returns a row with every label rendered dimly. |
|
| 2375 | + | fn muted_line(line: Line) -> Line { |
|
| 2341 | 2376 | line.into_iter() |
|
| 2342 | 2377 | .map(|label| { |
|
| 2343 | 2378 | let style = label |
|
| 2344 | 2379 | .paint |
|
| 2345 | 2380 | .style() |
| 2474 | 2509 | /// Focus target filter. |
|
| 2475 | 2510 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
|
| 2476 | 2511 | enum FocusFilter { |
|
| 2477 | 2512 | /// File targets only. |
|
| 2478 | 2513 | File, |
|
| 2479 | - | /// Hunk targets only. |
|
| 2480 | - | Hunk, |
|
| 2514 | + | /// Targets traversed by hunk-navigation actions. |
|
| 2515 | + | HunkNavigation, |
|
| 2481 | 2516 | } |
|
| 2482 | 2517 | ||
| 2483 | 2518 | /// Hunk navigation scroll behavior. |
|
| 2484 | 2519 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
|
| 2485 | 2520 | enum HunkScroll { |
a.rs
+0 -0
image.png
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.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
first.rs
+0 -0
second.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.rs\n",
+0 -0
new.rs\n",
+0 -0
a\n\
+0 -0