brain: Scope full-file state to each viewer

f69f4172229e654518f5496badde1c3242bd4632
Derive the footer mode from the active viewer context so commit switches
and failed context requests cannot leave stale full-file state.

Assisted-by: Codex:gpt-5.6-sol
Alexis Sellier committed ago 1 parent 02d8e58b
crates/brain/src/review.rs +2 -2
8 8
9 9
use std::collections::BTreeSet;
10 10
11 11
use rustik_diff::HunkId;
12 12
13 -
/// Session-wide view preferences controlling what the diff shows.
13 +
/// Visibility modes reported by the active review view.
14 14
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
15 15
pub struct Visibility {
16 16
    /// Show accepted hunks instead of hiding them.
17 17
    pub show_accepted: bool,
18 -
    /// Show the full file instead of just hunks with surrounding context.
18 +
    /// Show the active file's full source instead of bounded hunk context.
19 19
    pub full_file: bool,
20 20
}
21 21
22 22
/// Per-hunk acceptance state for one commit.
23 23
#[derive(Clone, Debug, Default)]
crates/brain/src/session.rs +40 -13
66 66
            Self::Hunk(_) => "hunk",
67 67
        }
68 68
    }
69 69
}
70 70
71 +
/// Returns a footer visibility snapshot reconciled with the active viewer.
72 +
fn active_visibility(
73 +
    mut visibility: Visibility,
74 +
    mode: &Mode,
75 +
    loaded: Option<&CommitReview>,
76 +
) -> Visibility {
77 +
    visibility.full_file =
78 +
        matches!(mode, Mode::Diff) && loaded.is_some_and(CommitReview::is_full_file);
79 +
    visibility
80 +
}
81 +
71 82
/// Terminal event channel shared by pre-session prompts and the session loop.
72 83
///
73 84
/// Reading terminal input consumes it, so one input worker must feed every
74 85
/// consumer; creating a second stream would steal keystrokes from the first.
75 86
pub struct Events {
175 186
        commits: slots,
176 187
        range,
177 188
        history_status,
178 189
        accepted_hunks: Vec::new(),
179 190
        current: 0,
180 -
        visibility: Visibility::default(),
191 +
        show_accepted: false,
181 192
        mode,
182 193
        parked: None,
183 194
        list: ListState::new(),
184 195
        options,
185 196
        backend,
244 255
    history_status: Option<HistoryStatus>,
245 256
    /// Hunk acceptances available to undo, in acceptance order.
246 257
    accepted_hunks: Vec<(usize, rustik_diff::HunkId)>,
247 258
    /// Index of the commit currently displayed.
248 259
    current: usize,
249 -
    /// Session-wide accepted-hunk visibility.
250 -
    visibility: Visibility,
260 +
    /// Whether accepted hunks are visible across commit viewers.
261 +
    show_accepted: bool,
251 262
    /// Active view: the commit list, the diff, or a hunk's comment box.
252 263
    mode: Mode,
253 264
    /// Hunk view kept alive while another tab is active, for Tab round-trips.
254 265
    parked: Option<Box<HunkView>>,
255 266
    /// Selection and scroll state for the commit-list tab.
331 342
                    Mode::Diff => {
332 343
                        if let Some(change) = context_change(&event) {
333 344
                            // Context expansion is asynchronous and may not
334 345
                            // redraw immediately, so it bypasses the ordinary
335 346
                            // diff input path.
336 -
                            if change == ContextChange::ToggleFile {
337 -
                                self.visibility.full_file = !self.visibility.full_file;
338 -
                            }
339 347
                            self.request_context(change);
340 348
                            return Ok(Update::Continue);
341 349
                        }
342 350
                        let area = viewer_area(self.terminal.area());
343 351
                        self.handle_key(event, area)?
396 404
            Event::Key(Key::Char('A')) => {
397 405
                self.toggle_focused_file(area)?;
398 406
                Ok(Control::Redraw)
399 407
            }
400 408
            Event::Key(Key::Char('v')) => {
401 -
                self.visibility.show_accepted = !self.visibility.show_accepted;
409 +
                self.show_accepted = !self.show_accepted;
402 410
                self.refresh_visibility()?;
403 411
                Ok(Control::Redraw)
404 412
            }
405 413
            Event::Key(Key::Char('j')) => self.apply(DiffAction::NextHunk, area),
406 414
            Event::Key(Key::Char('k')) => self.apply(DiffAction::PreviousHunk, area),
522 530
        } else {
523 531
            self.brain.accept([(identity, patch)])?;
524 532
            self.accepted_hunks.push((self.current, hunk));
525 533
        }
526 534
        self.reconcile_reviews()?;
527 -
        if !accepted && !self.visibility.show_accepted {
535 +
        if !accepted && !self.show_accepted {
528 536
            self.current()?.viewer.apply(DiffAction::NextHunk, area);
529 537
        }
530 538
531 539
        Ok(())
532 540
    }
958 966
    /// Rebuilds the current commit's files to match accepted-hunk visibility.
959 967
    ///
960 968
    /// The original decoded document is kept in `CommitReview::document`; the
961 969
    /// viewer receives a filtered copy when accepted hunks are hidden.
962 970
    fn refresh_visibility(&mut self) -> Result<(), Error> {
963 -
        let visibility = self.visibility;
971 +
        let show_accepted = self.show_accepted;
964 972
        let area = viewer_area(self.terminal.area());
965 973
        let loaded = self.current()?;
966 974
967 -
        let files: Vec<FileDiff> = if visibility.show_accepted {
975 +
        let files: Vec<FileDiff> = if show_accepted {
968 976
            loaded.document.files.clone()
969 977
        } else {
970 978
            loaded
971 979
                .document
972 980
                .files
1011 1019
1012 1020
    /// Reconciles review state and refreshes the active viewer when needed.
1013 1021
    fn reconcile_reviews(&mut self) -> Result<(), Error> {
1014 1022
        self.reconcile_state();
1015 1023
        self.history_status = None;
1016 -
        if !self.visibility.show_accepted && !matches!(self.mode, Mode::Commits) {
1024 +
        if !self.show_accepted && !matches!(self.mode, Mode::Commits) {
1017 1025
            self.refresh_visibility()?;
1018 1026
        }
1019 1027
        Ok(())
1020 1028
    }
1021 1029
1236 1244
            .set_cursor_visible(matches!(self.mode, Mode::Hunk(ref view) if view.is_editing()))
1237 1245
    }
1238 1246
1239 1247
    /// Returns the footer chrome for the current session mode.
1240 1248
    fn chrome(&self) -> Chrome {
1249 +
        let loaded = self.commits[self.current].loaded.as_ref();
1250 +
        let visibility = active_visibility(
1251 +
            Visibility {
1252 +
                show_accepted: self.show_accepted,
1253 +
                full_file: false,
1254 +
            },
1255 +
            &self.mode,
1256 +
            loaded,
1257 +
        );
1241 1258
        let (context, progress, stats) = if matches!(self.mode, Mode::Commits) {
1242 1259
            let status = self.history_status.as_ref();
1243 1260
            (
1244 1261
                StatusContext::Range(self.range.clone()),
1245 1262
                status.map_or_else(ReviewProgress::default, |status| status.progress),
1272 1289
                mode: self.mode.label(),
1273 1290
                context,
1274 1291
                progress,
1275 1292
                stats,
1276 1293
                palette: self.options.diff_palette,
1277 -
                visibility: self.visibility,
1294 +
                visibility,
1278 1295
                error: self.error.clone(),
1279 1296
            },
1280 1297
            actions: self.action_context(),
1281 1298
        }
1282 1299
    }
1292 1309
    }
1293 1310
}
1294 1311
1295 1312
#[cfg(test)]
1296 1313
mod tests {
1314 +
    use std::collections::BTreeMap;
1297 1315
    use std::fs;
1298 1316
    use std::path::{Path, PathBuf};
1299 1317
    use std::process::Command;
1300 1318
    use std::time::{SystemTime, UNIX_EPOCH};
1301 1319
1320 +
    use rustik_diff::{ContextChange, Decode as _};
1321 +
1302 1322
    use rustik_repo::Rev;
1303 1323
    use rustik_tui::{Point, Size};
1304 1324
1305 1325
    use super::*;
1326 +
    use crate::comments::Comments;
1327 +
    use crate::review::Review;
1306 1328
1307 1329
    /// In-memory terminal backend with the production backend's error type.
1308 1330
    struct IoTestBackend {
1309 1331
        /// Fixed terminal size.
1310 1332
        size: Size,
1456 1478
            commits: vec![slot],
1457 1479
            range: "HEAD^..HEAD".to_owned(),
1458 1480
            history_status: None,
1459 1481
            accepted_hunks: Vec::new(),
1460 1482
            current: 0,
1461 -
            visibility: Visibility::default(),
1483 +
            show_accepted: false,
1462 1484
            mode: Mode::Diff,
1463 1485
            parked: None,
1464 1486
            list: ListState::new(),
1465 1487
            options: DiffOptions::default(),
1466 1488
            backend,
1540 1562
        assert_eq!(
1541 1563
            terminal_interrupt(ActionContext::HunkEdit, &event),
1542 1564
            Control::Continue
1543 1565
        );
1544 1566
    }
1567 +
1568 +
    #[test]
1569 +
    fn footer_full_file_state_follows_the_selected_commit_viewer() {
1570 +
        let patch = "\
1571 +
a.rs +0 -0
crates/brain/src/session/commit.rs +5 -0
79 79
    /// Returns the combined diffstat of files that still need review.
80 80
    pub(super) fn remaining_stats(&self) -> DiffStat {
81 81
        commit_remaining_stats(&self.document, self.review.accepted())
82 82
    }
83 83
84 +
    /// Returns whether this commit's active file is expanded to its full source.
85 +
    pub(super) fn is_full_file(&self) -> bool {
86 +
        self.viewer.is_full_file()
87 +
    }
88 +
84 89
    /// Builds one commit's review state from its split diff document.
85 90
    ///
86 91
    /// Single-hunk patches and their patch ids are computed here, once, so
87 92
    /// later reconciliations only hash residual hunks and comment lookups can
88 93
    /// match on stable ids.
a.rs +0 -0
crates/diff/src/viewer.rs +6 -0
883 883
        // restoring a visible predecessor remains the newest value.
884 884
        self.hunk_focus.extend(hunk_focus);
885 885
        self.restore_view(focus, anchor, predecessor, active, area);
886 886
    }
887 887
888 +
    /// Returns whether the active file is expanded to its full source.
889 +
    pub fn is_full_file(&self) -> bool {
890 +
        self.active_file()
891 +
            .is_some_and(|file| self.context.is_expanded(file))
892 +
    }
893 +
888 894
    /// Prepares a repo refetch request for an asynchronous context update.
889 895
    pub fn context_request(&self, change: ContextChange) -> Option<ContextRequest> {
890 896
        let active = self.active_file();
891 897
        let file_ids = self.context_file_ids(active);
892 898
        let plan = self.context.plan(change, &file_ids, &self.document)?;
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