diff: Select file hunks on tree click
23e958132a808092ad050ab885806435c48c6d3f
1 parent
f440a4fc
crates/diff/src/viewer.rs
+49 -34
| 1104 | 1104 | self.state |
|
| 1105 | 1105 | .scroll |
|
| 1106 | 1106 | .set_offset(row, self.scroll_rows(visible_rows), visible_rows); |
|
| 1107 | 1107 | } |
|
| 1108 | 1108 | ||
| 1109 | + | /// Focuses the remembered or first hunk in `file` and keeps it visible. |
|
| 1110 | + | fn focus_file(&mut self, file: FileId, visible_rows: usize) { |
|
| 1111 | + | let was_hunk_focused = matches!(self.state.focus, Some(DiffFocus::Hunk(_))); |
|
| 1112 | + | let remembered = self.hunk_focus.get(&file).copied(); |
|
| 1113 | + | let focus = self |
|
| 1114 | + | .document |
|
| 1115 | + | .files |
|
| 1116 | + | .iter() |
|
| 1117 | + | .find(|candidate| candidate.id == file) |
|
| 1118 | + | .and_then(|file| { |
|
| 1119 | + | remembered |
|
| 1120 | + | .filter(|hunk| file.hunks.iter().any(|candidate| candidate.id == *hunk)) |
|
| 1121 | + | .or_else(|| file.hunks.first().map(|hunk| hunk.id)) |
|
| 1122 | + | }) |
|
| 1123 | + | .map_or(DiffFocus::File(file), DiffFocus::Hunk); |
|
| 1124 | + | let filter = if matches!(focus, DiffFocus::Hunk(_)) { |
|
| 1125 | + | FocusFilter::HunkNavigation |
|
| 1126 | + | } else { |
|
| 1127 | + | FocusFilter::File |
|
| 1128 | + | }; |
|
| 1129 | + | let Some(target) = self |
|
| 1130 | + | .focus_targets(filter) |
|
| 1131 | + | .into_iter() |
|
| 1132 | + | .find(|target| target.focus == focus) |
|
| 1133 | + | else { |
|
| 1134 | + | return; |
|
| 1135 | + | }; |
|
| 1136 | + | ||
| 1137 | + | self.set_focus(Some(focus)); |
|
| 1138 | + | let current = self.state.scroll.offset; |
|
| 1139 | + | let preserved = target.scroll_offset(current, visible_rows, HunkScroll::Preserve); |
|
| 1140 | + | let placement = if preserved == current || was_hunk_focused { |
|
| 1141 | + | HunkScroll::Preserve |
|
| 1142 | + | } else { |
|
| 1143 | + | HunkScroll::Center |
|
| 1144 | + | }; |
|
| 1145 | + | let next = target.scroll_offset(current, visible_rows, placement); |
|
| 1146 | + | self.state |
|
| 1147 | + | .scroll |
|
| 1148 | + | .set_offset(next, self.scroll_rows(visible_rows), visible_rows); |
|
| 1149 | + | } |
|
| 1150 | + | ||
| 1109 | 1151 | /// Updates the file-tree cursor to the focused or visible diff file. |
|
| 1110 | 1152 | fn sync_file_tree(&mut self) { |
|
| 1111 | 1153 | if let Some(file) = self.active_file() { |
|
| 1112 | 1154 | self.file_tree.select_file(file); |
|
| 1113 | 1155 | } |
| 1297 | 1339 | self.show_file(file, diff.rows); |
|
| 1298 | 1340 | } |
|
| 1299 | 1341 | } |
|
| 1300 | 1342 | DiffAction::ShowFileTreeTarget => { |
|
| 1301 | 1343 | if let Some(file) = self.file_tree.selected_file() { |
|
| 1302 | - | self.show_file(file, diff.rows); |
|
| 1344 | + | self.focus_file(file, diff.rows); |
|
| 1303 | 1345 | } |
|
| 1304 | 1346 | } |
|
| 1305 | 1347 | DiffAction::ActivateFileTreeTarget => { |
|
| 1306 | 1348 | if let Some(file) = self.file_tree.activate_target() { |
|
| 1307 | 1349 | self.show_file(file, diff.rows); |
| 2031 | 2073 | (None, FocusFilter::File, false) => Some(0), |
|
| 2032 | 2074 | }; |
|
| 2033 | 2075 | let Some(target) = next.and_then(|next| targets.get(next)) else { |
|
| 2034 | 2076 | return; |
|
| 2035 | 2077 | }; |
|
| 2036 | - | let was_hunk_focused = matches!(self.state.focus, Some(DiffFocus::Hunk(_))); |
|
| 2037 | - | let focus = if filter == FocusFilter::File { |
|
| 2038 | - | let DiffFocus::File(file_id) = target.focus else { |
|
| 2078 | + | if filter == FocusFilter::File { |
|
| 2079 | + | let DiffFocus::File(file) = target.focus else { |
|
| 2039 | 2080 | return; |
|
| 2040 | 2081 | }; |
|
| 2041 | - | let remembered = self.hunk_focus.get(&file_id).copied(); |
|
| 2042 | - | self.document |
|
| 2043 | - | .files |
|
| 2044 | - | .iter() |
|
| 2045 | - | .find(|file| file.id == file_id) |
|
| 2046 | - | .and_then(|file| { |
|
| 2047 | - | remembered |
|
| 2048 | - | .filter(|hunk| file.hunks.iter().any(|candidate| candidate.id == *hunk)) |
|
| 2049 | - | .or_else(|| file.hunks.first().map(|hunk| hunk.id)) |
|
| 2050 | - | }) |
|
| 2051 | - | .map_or(target.focus, DiffFocus::Hunk) |
|
| 2052 | - | } else { |
|
| 2053 | - | target.focus |
|
| 2054 | - | }; |
|
| 2055 | - | let target = if matches!(focus, DiffFocus::Hunk(_)) { |
|
| 2056 | - | self.focus_targets(FocusFilter::HunkNavigation) |
|
| 2057 | - | .into_iter() |
|
| 2058 | - | .find(|target| target.focus == focus) |
|
| 2059 | - | } else { |
|
| 2060 | - | Some(*target) |
|
| 2061 | - | }; |
|
| 2062 | - | let Some(target) = target else { |
|
| 2063 | - | return; |
|
| 2064 | - | }; |
|
| 2065 | - | ||
| 2066 | - | self.set_focus(Some(focus)); |
|
| 2067 | - | if filter == FocusFilter::File && matches!(focus, DiffFocus::File(_)) { |
|
| 2068 | - | self.state |
|
| 2069 | - | .scroll |
|
| 2070 | - | .set_offset(target.row, self.scroll_rows(visible_rows), visible_rows); |
|
| 2082 | + | self.focus_file(file, visible_rows); |
|
| 2071 | 2083 | return; |
|
| 2072 | 2084 | } |
|
| 2085 | + | let was_hunk_focused = matches!(self.state.focus, Some(DiffFocus::Hunk(_))); |
|
| 2086 | + | let focus = target.focus; |
|
| 2087 | + | self.set_focus(Some(focus)); |
|
| 2073 | 2088 | ||
| 2074 | 2089 | let current = self.state.scroll.offset; |
|
| 2075 | 2090 | let preserved = target.scroll_offset(current, visible_rows, HunkScroll::Preserve); |
|
| 2076 | 2091 | let placement = if preserved == current || was_hunk_focused { |
|
| 2077 | 2092 | HunkScroll::Preserve |
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
a.rs
+0 -0
b.rs
+0 -0
a.rs\n",
+0 -0
new.rs\n",
+0 -0
a\n\
+0 -0