brain: Support undo, via `u` key
7bf70d2d4a754c671096315c592a67f64ef48bcf
1 parent
685d9773
crates/brain/src/session.rs
+26 -2
| 173 | 173 | let mut session = Session { |
|
| 174 | 174 | terminal, |
|
| 175 | 175 | commits: slots, |
|
| 176 | 176 | range, |
|
| 177 | 177 | history_status, |
|
| 178 | + | accepted_hunks: Vec::new(), |
|
| 178 | 179 | current: 0, |
|
| 179 | 180 | visibility: Visibility::default(), |
|
| 180 | 181 | mode, |
|
| 181 | 182 | parked: None, |
|
| 182 | 183 | list: ListState::new(), |
| 230 | 231 | commits: Vec<CommitSlot>, |
|
| 231 | 232 | /// User-facing revision range under review. |
|
| 232 | 233 | range: String, |
|
| 233 | 234 | /// Lazily cached global progress and remaining statistics for history. |
|
| 234 | 235 | history_status: Option<HistoryStatus>, |
|
| 236 | + | /// Hunk acceptances available to undo, in acceptance order. |
|
| 237 | + | accepted_hunks: Vec<(usize, rustik_diff::HunkId)>, |
|
| 235 | 238 | /// Index of the commit currently displayed. |
|
| 236 | 239 | current: usize, |
|
| 237 | 240 | /// Session-wide accepted-hunk visibility. |
|
| 238 | 241 | visibility: Visibility, |
|
| 239 | 242 | /// Active view: the commit list, the diff, or a hunk's comment box. |
| 364 | 367 | match event { |
|
| 365 | 368 | Event::Key(Key::Char('a')) => { |
|
| 366 | 369 | self.toggle_focused_hunk(area)?; |
|
| 367 | 370 | Ok(Control::Redraw) |
|
| 368 | 371 | } |
|
| 372 | + | Event::Key(Key::Char('u')) => { |
|
| 373 | + | self.undo_last_hunk_accept()?; |
|
| 374 | + | Ok(Control::Redraw) |
|
| 375 | + | } |
|
| 369 | 376 | Event::Key(Key::Char('A')) => { |
|
| 370 | 377 | self.toggle_focused_file(area)?; |
|
| 371 | 378 | Ok(Control::Redraw) |
|
| 372 | 379 | } |
|
| 373 | 380 | Event::Key(Key::Char('v')) => { |
| 488 | 495 | return Ok(()); |
|
| 489 | 496 | }; |
|
| 490 | 497 | let accepted = loaded.review.is_accepted(hunk); |
|
| 491 | 498 | if accepted { |
|
| 492 | 499 | self.reject_hunks(BTreeSet::from([(self.current, hunk)]))?; |
|
| 500 | + | self.accepted_hunks |
|
| 501 | + | .retain(|accepted| *accepted != (self.current, hunk)); |
|
| 493 | 502 | } else { |
|
| 494 | 503 | self.brain.accept([(identity, patch)])?; |
|
| 504 | + | self.accepted_hunks.push((self.current, hunk)); |
|
| 495 | 505 | } |
|
| 496 | 506 | self.reconcile_reviews()?; |
|
| 497 | 507 | if !accepted && !self.visibility.show_accepted { |
|
| 498 | 508 | self.current()?.viewer.apply(DiffAction::NextHunk, area); |
|
| 499 | 509 | } |
|
| 500 | 510 | ||
| 501 | 511 | Ok(()) |
|
| 502 | 512 | } |
|
| 513 | + | /// Rejects the most recently accepted hunk that remains accepted. |
|
| 514 | + | fn undo_last_hunk_accept(&mut self) -> Result<(), Error> { |
|
| 515 | + | while let Some((index, hunk)) = self.accepted_hunks.pop() { |
|
| 516 | + | let accepted = self.load(index)?.review.is_accepted(hunk); |
|
| 517 | + | if accepted { |
|
| 518 | + | self.reject_hunks(BTreeSet::from([(index, hunk)]))?; |
|
| 519 | + | self.reconcile_reviews()?; |
|
| 520 | + | break; |
|
| 521 | + | } |
|
| 522 | + | } |
|
| 523 | + | Ok(()) |
|
| 524 | + | } |
|
| 503 | 525 | ||
| 504 | 526 | /// Persists acceptance or rejection of every hunk in the focused file. |
|
| 505 | 527 | fn toggle_focused_file(&mut self, area: Rect) -> Result<(), Error> { |
|
| 506 | 528 | let loaded = self.current()?; |
|
| 507 | 529 | let Some(file) = loaded.viewer.focused_file() else { |
| 530 | 552 | .collect::<Vec<_>>(); |
|
| 531 | 553 | if accepted { |
|
| 532 | 554 | let rejected = hunk_ids |
|
| 533 | 555 | .into_iter() |
|
| 534 | 556 | .map(|hunk| (self.current, hunk)) |
|
| 535 | - | .collect(); |
|
| 536 | - | self.reject_hunks(rejected)?; |
|
| 557 | + | .collect::<BTreeSet<_>>(); |
|
| 558 | + | self.reject_hunks(rejected.clone())?; |
|
| 559 | + | self.accepted_hunks |
|
| 560 | + | .retain(|accepted| !rejected.contains(accepted)); |
|
| 537 | 561 | } else { |
|
| 538 | 562 | self.brain.accept(hunks)?; |
|
| 539 | 563 | } |
|
| 540 | 564 | self.reconcile_reviews()?; |
|
| 541 | 565 | Ok(()) |
crates/brain/src/session/status.rs
+1 -0
| 82 | 82 | const COMMIT_ACTIONS: &[(&str, &str)] = &[ |
|
| 83 | 83 | (TAB_KEY, "history"), |
|
| 84 | 84 | ("j/k", "hunk"), |
|
| 85 | 85 | ("n/p", "file"), |
|
| 86 | 86 | ("a", "accept hunk"), |
|
| 87 | + | ("u", "undo"), |
|
| 87 | 88 | ("c", "comment"), |
|
| 88 | 89 | ]; |
|
| 89 | 90 | /// Action hints shown while browsing hunk comments. |
|
| 90 | 91 | const HUNK_BROWSE_ACTIONS: &[(&str, &str)] = &[ |
|
| 91 | 92 | (TAB_KEY, "history"), |