diff: Preserve nearby hunk selection

426315a4cd132612a0b7933def1159ac269771c1
Keep focus on the preceding hunk when replacement hides the final
focused hunk, so navigation does not restart at the beginning of the
file.

Assisted-by: Codex:gpt-5.6
Alexis Sellier committed ago 1 parent f44235e9
crates/diff/src/viewer.rs +31 -8
861 861
        files: impl IntoIterator<Item = FileDiff>,
862 862
        area: Rect,
863 863
    ) {
864 864
        let visible_rows = self.layout(area).diff.rows;
865 865
        let anchor = self.focus_anchor(visible_rows);
866 +
        let predecessor = self.focused_hunk_predecessor();
866 867
        let active = self.active_file();
867 868
        self.replace_file_diffs(files);
868 -
        self.restore_view(anchor, active, area);
869 +
        self.restore_view(anchor, predecessor, active, area);
869 870
    }
870 871
871 872
    /// Prepares a repo refetch request for an asynchronous context update.
872 873
    pub fn context_request(&self, change: ContextChange) -> Option<ContextRequest> {
873 874
        let active = self.active_file();
899 900
            return false;
900 901
        }
901 902
        let before = self.state.clone();
902 903
        let visible_rows = self.layout(area).diff.rows;
903 904
        let focus_anchor = self.focus_anchor(visible_rows);
905 +
        let predecessor = self.focused_hunk_predecessor();
904 906
        let active = self.active_file();
905 907
        self.context = update.next_context;
906 908
        self.expanded_context = update.expanded_context;
907 909
        self.replace_file_diffs(update.files);
908 -
        self.restore_view(focus_anchor, active, area);
910 +
        self.restore_view(focus_anchor, predecessor, active, area);
909 911
        self.state != before
910 912
    }
911 913
912 914
    /// Restores focus and scroll after replacing rendered diff contents.
913 915
    fn restore_view(
914 916
        &mut self,
915 917
        focus_anchor: Option<(DiffFocus, usize)>,
918 +
        predecessor: Option<DiffFocus>,
916 919
        active: Option<FileId>,
917 920
        area: Rect,
918 921
    ) {
919 922
        if self.state.focus.is_some_and(|focus| !self.has_focus(focus)) {
920 -
            self.state.focus = active.map(DiffFocus::File);
923 +
            self.state.focus = predecessor
924 +
                .filter(|focus| self.has_focus(*focus))
925 +
                .or_else(|| active.map(DiffFocus::File));
921 926
        }
922 927
        let layout = self.layout(area);
923 928
        let visible_rows = layout.diff.rows;
924 929
        let offset = focus_anchor
925 930
            .and_then(|anchor| self.focus_anchor_offset(anchor))
931 936
        if layout.tree.is_some() {
932 937
            self.sync_file_tree();
933 938
        }
934 939
    }
935 940
936 -
    /// Returns a visible focused hunk and its current terminal row.
941 +
    /// Returns the hunk before the focused hunk in the same file.
942 +
    fn focused_hunk_predecessor(&self) -> Option<DiffFocus> {
943 +
        let Some(DiffFocus::Hunk(focused)) = self.state.focus else {
944 +
            return None;
945 +
        };
946 +
        let file = self
947 +
            .document
948 +
            .files
949 +
            .iter()
950 +
            .find(|file| file.hunks.iter().any(|hunk| hunk.id == focused))?;
951 +
        let index = file.hunks.iter().position(|hunk| hunk.id == focused)?;
952 +
        let predecessor = file.hunks.get(index.checked_sub(1)?)?;
953 +
954 +
        Some(DiffFocus::Hunk(predecessor.id))
955 +
    }
956 +
957 +
    /// Returns a visible focused hunk and its terminal row.
937 958
    fn focus_anchor(&self, visible_rows: usize) -> Option<(DiffFocus, usize)> {
938 959
        let focus = self.state.focus?;
939 960
        self.focus_targets(FocusFilter::Hunk)
940 961
            .into_iter()
941 962
            .find(|target| target.focus == focus)
1924 1945
                .scroll
1925 1946
                .set_offset(target.row, self.scroll_rows(visible_rows), visible_rows);
1926 1947
            return;
1927 1948
        }
1928 1949
1929 -
        let placement = if was_hunk_focused {
1950 +
        let current = self.state.scroll.offset;
1951 +
        let preserved = target.scroll_offset(current, visible_rows, HunkScroll::Preserve);
1952 +
        let placement = if preserved == current || was_hunk_focused {
1930 1953
            HunkScroll::Preserve
1931 1954
        } else {
1932 1955
            HunkScroll::Center
1933 1956
        };
1934 -
        let next = target.scroll_offset(self.state.scroll.offset, visible_rows, placement);
1957 +
        let next = target.scroll_offset(current, visible_rows, placement);
1935 1958
1936 1959
        self.state
1937 1960
            .scroll
1938 1961
            .set_offset(next, self.scroll_rows(visible_rows), visible_rows);
1939 1962
    }
2340 2363
                } else {
2341 2364
                    current
2342 2365
                }
2343 2366
            }
2344 2367
            HunkScroll::Preserve => {
2345 -
                let top = (visible_rows / 4).max(self.sticky_rows(current));
2346 -
                let bottom = visible_rows.saturating_sub(visible_rows / 4);
2368 +
                let top = self.sticky_rows(current).saturating_add(1);
2369 +
                let bottom = visible_rows.saturating_sub(1);
2347 2370
                let screen_top = self.row.saturating_sub(current);
2348 2371
                let screen_bottom = screen_top.saturating_add(height);
2349 2372
2350 2373
                if screen_top < top {
2351 2374
                    self.row.saturating_sub(top)
a.rs +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
a\n\ +0 -0