diff: Simplify file tree synchronization
96f8631ac148fb8422e416bf43115c1a1f6b2484
Synchronize the tree cursor once after relevant viewer actions and document restoration. This keeps highlighting unique, clears file selection at commit focus, and preserves explicit folder navigation without render-time ambiguity. Assisted-by: Codex:gpt-5.6-sol
1 parent
f611042b
crates/diff/src/viewer.rs
+32 -29
| 963 | 963 | ||
| 964 | 964 | self.state |
|
| 965 | 965 | .scroll |
|
| 966 | 966 | .set_offset(offset, self.scroll_rows(visible_rows), visible_rows); |
|
| 967 | 967 | if layout.tree.is_some() { |
|
| 968 | - | self.sync_file_tree(); |
|
| 968 | + | let active = self.active_file(); |
|
| 969 | + | self.sync_file_tree(active); |
|
| 969 | 970 | } |
|
| 970 | 971 | } |
|
| 971 | 972 | ||
| 972 | 973 | /// Returns hunk identities before the focused hunk in document order. |
|
| 973 | 974 | fn focused_hunk_predecessors(&self) -> Vec<HunkId> { |
| 1146 | 1147 | self.state |
|
| 1147 | 1148 | .scroll |
|
| 1148 | 1149 | .set_offset(next, self.scroll_rows(visible_rows), visible_rows); |
|
| 1149 | 1150 | } |
|
| 1150 | 1151 | ||
| 1151 | - | /// Updates the file-tree cursor to the focused or visible diff file. |
|
| 1152 | - | fn sync_file_tree(&mut self) { |
|
| 1153 | - | if let Some(file) = self.active_file() { |
|
| 1152 | + | /// Updates the file-tree cursor to match diff focus or the given visible file. |
|
| 1153 | + | fn sync_file_tree(&mut self, file: Option<FileId>) { |
|
| 1154 | + | if matches!(self.state.focus, Some(DiffFocus::Commit)) { |
|
| 1155 | + | self.file_tree.clear_selection(); |
|
| 1156 | + | } else if let Some(file) = file { |
|
| 1154 | 1157 | self.file_tree.select_file(file); |
|
| 1155 | 1158 | } |
|
| 1156 | 1159 | } |
|
| 1157 | 1160 | ||
| 1158 | 1161 | /// Renders the current frame into `buf`. |
| 1204 | 1207 | } |
|
| 1205 | 1208 | } |
|
| 1206 | 1209 | ||
| 1207 | 1210 | /// Renders the file tree. |
|
| 1208 | 1211 | fn render_file_tree(&mut self, area: Rect, buf: &mut Buffer) { |
|
| 1209 | - | let active_file = self.visible_file(); |
|
| 1210 | 1212 | self.file_tree.draw( |
|
| 1211 | - | active_file, |
|
| 1213 | + | None, |
|
| 1212 | 1214 | self.options.diff_palette, |
|
| 1213 | 1215 | self.decorator.as_deref(), |
|
| 1214 | 1216 | area, |
|
| 1215 | 1217 | buf, |
|
| 1216 | 1218 | ); |
| 1272 | 1274 | self.state |
|
| 1273 | 1275 | .scroll |
|
| 1274 | 1276 | .scroll(scroll, self.scroll_rows(diff.rows), diff.rows); |
|
| 1275 | 1277 | if self.state.scroll.offset != previous |
|
| 1276 | 1278 | && let Some(file) = self.visible_file() |
|
| 1279 | + | && !matches!(self.state.focus, Some(DiffFocus::Hunk(_))) |
|
| 1277 | 1280 | { |
|
| 1278 | - | if !matches!(self.state.focus, Some(DiffFocus::Hunk(_))) { |
|
| 1279 | - | self.set_focus(Some(DiffFocus::File(file))); |
|
| 1280 | - | } |
|
| 1281 | - | if layout.tree.is_some() { |
|
| 1282 | - | self.file_tree.select_file(file); |
|
| 1283 | - | } |
|
| 1281 | + | self.set_focus(Some(DiffFocus::File(file))); |
|
| 1284 | 1282 | } |
|
| 1285 | 1283 | } |
|
| 1286 | 1284 | DiffAction::Resize => { |
|
| 1287 | 1285 | self.state |
|
| 1288 | 1286 | .scroll |
|
| 1289 | 1287 | .clamp(self.scroll_rows(diff.rows), diff.rows); |
|
| 1290 | - | if layout.tree.is_some() { |
|
| 1291 | - | self.sync_file_tree(); |
|
| 1292 | - | } |
|
| 1293 | 1288 | } |
|
| 1294 | 1289 | DiffAction::FocusNext => { |
|
| 1295 | 1290 | self.focus(diff.rows, 1, FocusFilter::File); |
|
| 1296 | - | self.sync_file_tree(); |
|
| 1297 | 1291 | } |
|
| 1298 | 1292 | DiffAction::FocusPrevious => { |
|
| 1299 | 1293 | self.focus(diff.rows, -1, FocusFilter::File); |
|
| 1300 | - | self.sync_file_tree(); |
|
| 1301 | 1294 | } |
|
| 1302 | 1295 | DiffAction::NextHunk => { |
|
| 1303 | 1296 | self.focus(diff.rows, 1, FocusFilter::HunkNavigation); |
|
| 1304 | - | if self.state.focus != before.focus { |
|
| 1305 | - | self.sync_file_tree(); |
|
| 1306 | - | } |
|
| 1307 | 1297 | } |
|
| 1308 | 1298 | DiffAction::PreviousHunk => { |
|
| 1309 | 1299 | self.focus(diff.rows, -1, FocusFilter::HunkNavigation); |
|
| 1310 | - | if self.state.focus != before.focus { |
|
| 1311 | - | self.sync_file_tree(); |
|
| 1312 | - | } |
|
| 1313 | 1300 | } |
|
| 1314 | 1301 | DiffAction::ToggleFileTree => match self.file_tree_visibility { |
|
| 1315 | 1302 | FileTreeVisibility::Hidden => { |
|
| 1316 | 1303 | self.file_tree_visibility = FileTreeVisibility::Visible; |
|
| 1317 | - | if self.layout(area).tree.is_some() { |
|
| 1318 | - | self.sync_file_tree(); |
|
| 1319 | - | } |
|
| 1320 | 1304 | } |
|
| 1321 | 1305 | FileTreeVisibility::Auto | FileTreeVisibility::Visible if layout.tree.is_some() => { |
|
| 1322 | 1306 | self.file_tree_visibility = FileTreeVisibility::Hidden; |
|
| 1323 | 1307 | } |
|
| 1324 | 1308 | FileTreeVisibility::Auto => { |
|
| 1325 | 1309 | self.file_tree_visibility = FileTreeVisibility::Visible; |
|
| 1326 | - | if self.layout(area).tree.is_some() { |
|
| 1327 | - | self.sync_file_tree(); |
|
| 1328 | - | } |
|
| 1329 | 1310 | } |
|
| 1330 | 1311 | FileTreeVisibility::Visible => { |
|
| 1331 | 1312 | self.file_tree_visibility = FileTreeVisibility::Hidden; |
|
| 1332 | 1313 | } |
|
| 1333 | 1314 | }, |
| 1355 | 1336 | DiffAction::ExpandFileTreeFolder => { |
|
| 1356 | 1337 | self.file_tree.expand_folder(); |
|
| 1357 | 1338 | } |
|
| 1358 | 1339 | } |
|
| 1359 | 1340 | ||
| 1341 | + | let sync_after_action = match action { |
|
| 1342 | + | DiffAction::Scroll(_) => self.state.scroll != before.scroll, |
|
| 1343 | + | DiffAction::NextHunk | DiffAction::PreviousHunk => self.state.focus != before.focus, |
|
| 1344 | + | DiffAction::MoveFileTree(_) |
|
| 1345 | + | | DiffAction::ShowFileTreeTarget |
|
| 1346 | + | | DiffAction::ActivateFileTreeTarget |
|
| 1347 | + | | DiffAction::CollapseFileTreeFolder |
|
| 1348 | + | | DiffAction::ExpandFileTreeFolder => false, |
|
| 1349 | + | DiffAction::Resize |
|
| 1350 | + | | DiffAction::FocusNext |
|
| 1351 | + | | DiffAction::FocusPrevious |
|
| 1352 | + | | DiffAction::ToggleFileTree => true, |
|
| 1353 | + | }; |
|
| 1354 | + | if sync_after_action && self.layout(area).tree.is_some() { |
|
| 1355 | + | let file = if matches!(action, DiffAction::Scroll(_)) { |
|
| 1356 | + | self.visible_file() |
|
| 1357 | + | } else { |
|
| 1358 | + | self.active_file() |
|
| 1359 | + | }; |
|
| 1360 | + | self.sync_file_tree(file); |
|
| 1361 | + | } |
|
| 1362 | + | ||
| 1360 | 1363 | if self.state == before |
|
| 1361 | 1364 | && self.file_tree_visibility == before_visibility |
|
| 1362 | 1365 | && *self.file_tree.state() == before_file_tree |
|
| 1363 | 1366 | { |
|
| 1364 | 1367 | ViewChange::Unchanged |
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
crates/diff/src/viewer/tree.rs
+9 -5
| 218 | 218 | return false; |
|
| 219 | 219 | }; |
|
| 220 | 220 | self.state.select(id) |
|
| 221 | 221 | } |
|
| 222 | 222 | ||
| 223 | + | /// Clears the file-tree cursor. |
|
| 224 | + | pub(super) fn clear_selection(&mut self) -> bool { |
|
| 225 | + | self.state.clear_selection() |
|
| 226 | + | } |
|
| 227 | + | ||
| 223 | 228 | /// Selects the file-tree row at `point` inside `area`. |
|
| 224 | 229 | pub(super) fn select_at(&mut self, area: Rect, point: Point) -> Option<FileTreeClick> { |
|
| 225 | 230 | let tree_area = Self::area(area)?; |
|
| 226 | 231 | let tree = Tree::new(&self.model, &self.state); |
|
| 227 | 232 | let rows = tree.visible_rows(); |
| 550 | 555 | } |
|
| 551 | 556 | ||
| 552 | 557 | impl StyledFileTree<'_> { |
|
| 553 | 558 | /// Returns whether `id` should render with active styling. |
|
| 554 | 559 | /// |
|
| 555 | - | /// True when the file-tree cursor is on `id` or when `id` represents the |
|
| 556 | - | /// active diff file directly (a file row) or indirectly (a collapsed folder |
|
| 557 | - | /// containing it). |
|
| 560 | + | /// The file-tree cursor takes precedence over the active diff file so only |
|
| 561 | + | /// one visible row is highlighted. |
|
| 558 | 562 | fn is_active(&self, id: usize) -> bool { |
|
| 559 | - | if self.selected == Some(id) { |
|
| 560 | - | return true; |
|
| 563 | + | if let Some(selected) = self.selected { |
|
| 564 | + | return selected == id; |
|
| 561 | 565 | } |
|
| 562 | 566 | let Some(node) = self.model.nodes.get(id) else { |
|
| 563 | 567 | return false; |
|
| 564 | 568 | }; |
|
| 565 | 569 | let Some(active_file) = self.active_file else { |
a.rs
+0 -0
b.rs
+0 -0