diff: Restore per-file hunk focus
732061dccba14d409c1052f839c302a434302bc6
Remember the last visible hunk selected in each file so file navigation returns reviewers to their prior position. Prune stale selections when replacement diffs remove hunks. Assisted-by: Codex:gpt-5.6-sol
1 parent
d686f085
crates/diff/src/viewer.rs
+86 -24
| 239 | 239 | expanded_context: BTreeSet<RowId>, |
|
| 240 | 240 | /// Viewer options. |
|
| 241 | 241 | options: DiffOptions, |
|
| 242 | 242 | /// Mutable viewport state. |
|
| 243 | 243 | state: DiffState, |
|
| 244 | + | /// Last focused visible hunk for each file. |
|
| 245 | + | hunk_focus: BTreeMap<FileId, HunkId>, |
|
| 244 | 246 | /// Highlighted row cache. |
|
| 245 | 247 | highlights: BTreeMap<HighlightKey, Line>, |
|
| 246 | 248 | /// Requests already handed to a worker. |
|
| 247 | 249 | pending_highlights: BTreeSet<HighlightKey>, |
|
| 248 | 250 | /// Per-hunk source context reused across highlight-request batches. |
| 678 | 680 | document, |
|
| 679 | 681 | summaries, |
|
| 680 | 682 | expanded_context: BTreeSet::new(), |
|
| 681 | 683 | options, |
|
| 682 | 684 | state, |
|
| 685 | + | hunk_focus: BTreeMap::new(), |
|
| 683 | 686 | highlights: BTreeMap::new(), |
|
| 684 | 687 | pending_highlights: BTreeSet::new(), |
|
| 685 | 688 | hunk_highlight_sources: BTreeMap::new(), |
|
| 686 | 689 | blob_sources: BTreeMap::new(), |
|
| 687 | 690 | worktree_sources: BTreeMap::new(), |
| 824 | 827 | pub const fn state(&self) -> &DiffState { |
|
| 825 | 828 | &self.state |
|
| 826 | 829 | } |
|
| 827 | 830 | ||
| 828 | 831 | /// Replaces the viewer state. |
|
| 829 | - | pub fn set_state(&mut self, state: DiffState) { |
|
| 832 | + | pub fn set_state(&mut self, mut state: DiffState) { |
|
| 833 | + | let focus = state.focus.take(); |
|
| 830 | 834 | self.state = state; |
|
| 835 | + | self.set_focus(focus); |
|
| 831 | 836 | } |
|
| 832 | 837 | ||
| 833 | 838 | /// Replaces existing file diffs in place, matching them by id. |
|
| 834 | 839 | pub fn replace_file_diffs(&mut self, files: impl IntoIterator<Item = FileDiff>) { |
|
| 835 | 840 | for replacement in files { |
| 854 | 859 | // simply misses the lookup and falls back to its hunk source. |
|
| 855 | 860 | self.word_diffs.clear(); |
|
| 856 | 861 | self.rebuild_index(); |
|
| 857 | 862 | } |
|
| 858 | 863 | ||
| 859 | - | /// Replaces file diffs while keeping a visible focused hunk on its screen row. |
|
| 864 | + | /// Replaces visibility-filtered file diffs while preserving hunk focus and scroll. |
|
| 860 | 865 | pub fn replace_file_diffs_anchored( |
|
| 861 | 866 | &mut self, |
|
| 862 | 867 | files: impl IntoIterator<Item = FileDiff>, |
|
| 863 | 868 | area: Rect, |
|
| 864 | 869 | ) { |
|
| 870 | + | let hunk_focus = self.hunk_focus.clone(); |
|
| 865 | 871 | let visible_rows = self.layout(area).diff.rows; |
|
| 872 | + | let focus = self.state.focus; |
|
| 866 | 873 | let anchor = self.focus_anchor(visible_rows); |
|
| 867 | 874 | let predecessor = self.focused_hunk_predecessor(); |
|
| 868 | 875 | let active = self.active_file(); |
|
| 869 | 876 | self.replace_file_diffs(files); |
|
| 870 | - | self.restore_view(anchor, predecessor, active, area); |
|
| 877 | + | // Restore remembered entries before the view so a focus recorded while |
|
| 878 | + | // restoring a visible predecessor remains the newest value. |
|
| 879 | + | self.hunk_focus.extend(hunk_focus); |
|
| 880 | + | self.restore_view(focus, anchor, predecessor, active, area); |
|
| 871 | 881 | } |
|
| 872 | 882 | ||
| 873 | 883 | /// Prepares a repo refetch request for an asynchronous context update. |
|
| 874 | 884 | pub fn context_request(&self, change: ContextChange) -> Option<ContextRequest> { |
|
| 875 | 885 | let active = self.active_file(); |
| 899 | 909 | pub fn apply_context_update(&mut self, update: ContextUpdate, area: Rect) -> bool { |
|
| 900 | 910 | if update.files.is_empty() { |
|
| 901 | 911 | return false; |
|
| 902 | 912 | } |
|
| 903 | 913 | let before = self.state.clone(); |
|
| 914 | + | let focus = self.state.focus; |
|
| 904 | 915 | let visible_rows = self.layout(area).diff.rows; |
|
| 905 | 916 | let focus_anchor = self.focus_anchor(visible_rows); |
|
| 906 | 917 | let predecessor = self.focused_hunk_predecessor(); |
|
| 907 | 918 | let active = self.active_file(); |
|
| 908 | 919 | self.context = update.next_context; |
|
| 909 | 920 | self.expanded_context = update.expanded_context; |
|
| 910 | 921 | self.replace_file_diffs(update.files); |
|
| 911 | - | self.restore_view(focus_anchor, predecessor, active, area); |
|
| 922 | + | self.restore_view(focus, focus_anchor, predecessor, active, area); |
|
| 912 | 923 | self.state != before |
|
| 913 | 924 | } |
|
| 914 | 925 | ||
| 915 | 926 | /// Restores focus and scroll after replacing rendered diff contents. |
|
| 916 | 927 | fn restore_view( |
|
| 917 | 928 | &mut self, |
|
| 929 | + | focus: Option<DiffFocus>, |
|
| 918 | 930 | focus_anchor: Option<(DiffFocus, usize)>, |
|
| 919 | 931 | predecessor: Option<DiffFocus>, |
|
| 920 | 932 | active: Option<FileId>, |
|
| 921 | 933 | area: Rect, |
|
| 922 | 934 | ) { |
|
| 923 | - | if self.state.focus.is_some_and(|focus| !self.has_focus(focus)) { |
|
| 924 | - | self.state.focus = predecessor |
|
| 925 | - | .filter(|focus| self.has_focus(*focus)) |
|
| 926 | - | .or_else(|| active.map(DiffFocus::File)); |
|
| 935 | + | if focus.is_some_and(|focus| !self.has_focus(focus)) { |
|
| 936 | + | self.set_focus( |
|
| 937 | + | predecessor |
|
| 938 | + | .filter(|focus| self.has_focus(*focus)) |
|
| 939 | + | .or_else(|| active.map(DiffFocus::File)), |
|
| 940 | + | ); |
|
| 927 | 941 | } |
|
| 928 | 942 | let layout = self.layout(area); |
|
| 929 | 943 | let visible_rows = layout.diff.rows; |
|
| 930 | 944 | let offset = focus_anchor |
|
| 931 | 945 | .and_then(|anchor| self.focus_anchor_offset(anchor)) |
| 973 | 987 | .into_iter() |
|
| 974 | 988 | .find(|target| target.focus == focus) |
|
| 975 | 989 | .map(|target| target.row.saturating_sub(screen_row)) |
|
| 976 | 990 | } |
|
| 977 | 991 | ||
| 978 | - | /// Returns whether a focus target still exists in the current document. |
|
| 992 | + | /// Returns whether a focus target is visible in the current document. |
|
| 979 | 993 | fn has_focus(&self, focus: DiffFocus) -> bool { |
|
| 980 | 994 | match focus { |
|
| 981 | - | DiffFocus::File(file) => self |
|
| 982 | - | .document |
|
| 983 | - | .files |
|
| 984 | - | .iter() |
|
| 985 | - | .any(|candidate| candidate.id == file), |
|
| 986 | - | DiffFocus::Hunk(hunk) => self |
|
| 987 | - | .document |
|
| 988 | - | .files |
|
| 989 | - | .iter() |
|
| 990 | - | .flat_map(|file| &file.hunks) |
|
| 991 | - | .any(|candidate| candidate.id == hunk), |
|
| 995 | + | DiffFocus::File(file) => self.index.files.iter().any(|indexed| { |
|
| 996 | + | self.document |
|
| 997 | + | .files |
|
| 998 | + | .get(indexed.file_index) |
|
| 999 | + | .is_some_and(|candidate| candidate.id == file) |
|
| 1000 | + | }), |
|
| 1001 | + | DiffFocus::Hunk(hunk) => self.index.files.iter().any(|indexed| { |
|
| 1002 | + | self.document |
|
| 1003 | + | .files |
|
| 1004 | + | .get(indexed.file_index) |
|
| 1005 | + | .is_some_and(|file| file.hunks.iter().any(|candidate| candidate.id == hunk)) |
|
| 1006 | + | }), |
|
| 992 | 1007 | } |
|
| 993 | 1008 | } |
|
| 994 | 1009 | ||
| 995 | 1010 | /// Returns the files targeted by a context change. |
|
| 996 | 1011 | fn context_file_ids(&self, active: Option<FileId>) -> Vec<FileId> { |
| 1006 | 1021 | self.state.scroll = Viewport::from_offset(scroll); |
|
| 1007 | 1022 | } |
|
| 1008 | 1023 | ||
| 1009 | 1024 | /// Sets the focused file or hunk. |
|
| 1010 | 1025 | pub fn set_focus(&mut self, focus: Option<DiffFocus>) { |
|
| 1026 | + | let focus = focus.filter(|focus| self.has_focus(*focus)); |
|
| 1027 | + | 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 | + | }) |
|
| 1033 | + | { |
|
| 1034 | + | self.hunk_focus.insert(file.id, hunk); |
|
| 1035 | + | } |
|
| 1011 | 1036 | self.state.focus = focus; |
|
| 1012 | 1037 | } |
|
| 1013 | 1038 | ||
| 1014 | 1039 | /// Returns the file containing the current focus. |
|
| 1015 | 1040 | pub fn focused_file(&self) -> Option<FileId> { |
| 1057 | 1082 | ||
| 1058 | 1083 | (candidate.id == file).then_some(indexed.start) |
|
| 1059 | 1084 | }) else { |
|
| 1060 | 1085 | return; |
|
| 1061 | 1086 | }; |
|
| 1062 | - | self.state.focus = Some(DiffFocus::File(file)); |
|
| 1087 | + | self.set_focus(Some(DiffFocus::File(file))); |
|
| 1063 | 1088 | self.state |
|
| 1064 | 1089 | .scroll |
|
| 1065 | 1090 | .set_offset(row, self.scroll_rows(visible_rows), visible_rows); |
|
| 1066 | 1091 | } |
|
| 1067 | 1092 |
| 1191 | 1216 | .scroll(scroll, self.scroll_rows(diff.rows), diff.rows); |
|
| 1192 | 1217 | if self.state.scroll.offset != previous |
|
| 1193 | 1218 | && let Some(file) = self.visible_file() |
|
| 1194 | 1219 | { |
|
| 1195 | 1220 | if !matches!(self.state.focus, Some(DiffFocus::Hunk(_))) { |
|
| 1196 | - | self.state.focus = Some(DiffFocus::File(file)); |
|
| 1221 | + | self.set_focus(Some(DiffFocus::File(file))); |
|
| 1197 | 1222 | } |
|
| 1198 | 1223 | if layout.tree.is_some() { |
|
| 1199 | 1224 | self.file_tree.select_file(file); |
|
| 1200 | 1225 | } |
|
| 1201 | 1226 | } |
| 1740 | 1765 | start: rows, |
|
| 1741 | 1766 | }); |
|
| 1742 | 1767 | rows += self.file_rows(file); |
|
| 1743 | 1768 | } |
|
| 1744 | 1769 | self.index = RowIndex { files, rows }; |
|
| 1770 | + | let document = &self.document; |
|
| 1771 | + | self.hunk_focus.retain(|file, hunk| { |
|
| 1772 | + | document.files.iter().any(|candidate| { |
|
| 1773 | + | candidate.id == *file |
|
| 1774 | + | && candidate.hunks.iter().any(|candidate| candidate.id == *hunk) |
|
| 1775 | + | }) |
|
| 1776 | + | }); |
|
| 1777 | + | let focus = self.state.focus; |
|
| 1778 | + | self.set_focus(focus); |
|
| 1745 | 1779 | } |
|
| 1746 | 1780 | ||
| 1747 | 1781 | /// Lays out the commit preamble for the given body width. |
|
| 1748 | 1782 | fn preamble(commit: Option<&CommitHeader>, cols: usize) -> Vec<Line> { |
|
| 1749 | 1783 | let Some(commit) = commit else { |
| 1970 | 2004 | }; |
|
| 1971 | 2005 | let Some(target) = next.and_then(|next| targets.get(next)) else { |
|
| 1972 | 2006 | return; |
|
| 1973 | 2007 | }; |
|
| 1974 | 2008 | let was_hunk_focused = matches!(self.state.focus, Some(DiffFocus::Hunk(_))); |
|
| 2009 | + | let focus = if filter == FocusFilter::File { |
|
| 2010 | + | let DiffFocus::File(file_id) = target.focus else { |
|
| 2011 | + | return; |
|
| 2012 | + | }; |
|
| 2013 | + | let remembered = self.hunk_focus.get(&file_id).copied(); |
|
| 2014 | + | self.document |
|
| 2015 | + | .files |
|
| 2016 | + | .iter() |
|
| 2017 | + | .find(|file| file.id == file_id) |
|
| 2018 | + | .and_then(|file| { |
|
| 2019 | + | remembered |
|
| 2020 | + | .filter(|hunk| file.hunks.iter().any(|candidate| candidate.id == *hunk)) |
|
| 2021 | + | .or_else(|| file.hunks.first().map(|hunk| hunk.id)) |
|
| 2022 | + | }) |
|
| 2023 | + | .map_or(target.focus, DiffFocus::Hunk) |
|
| 2024 | + | } else { |
|
| 2025 | + | target.focus |
|
| 2026 | + | }; |
|
| 2027 | + | let target = if matches!(focus, DiffFocus::Hunk(_)) { |
|
| 2028 | + | self.focus_targets(FocusFilter::Hunk) |
|
| 2029 | + | .into_iter() |
|
| 2030 | + | .find(|target| target.focus == focus) |
|
| 2031 | + | } else { |
|
| 2032 | + | Some(*target) |
|
| 2033 | + | }; |
|
| 2034 | + | let Some(target) = target else { |
|
| 2035 | + | return; |
|
| 2036 | + | }; |
|
| 1975 | 2037 | ||
| 1976 | - | self.state.focus = Some(target.focus); |
|
| 1977 | - | if filter == FocusFilter::File { |
|
| 2038 | + | self.set_focus(Some(focus)); |
|
| 2039 | + | if filter == FocusFilter::File && matches!(focus, DiffFocus::File(_)) { |
|
| 1978 | 2040 | self.state |
|
| 1979 | 2041 | .scroll |
|
| 1980 | 2042 | .set_offset(target.row, self.scroll_rows(visible_rows), visible_rows); |
|
| 1981 | 2043 | return; |
|
| 1982 | 2044 | } |
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
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