Skip to content

diff: Add replace_files_in_place to keep the reader's place - #4020

Closed
sam-phinizy wants to merge 1 commit into
longbridge:mainfrom
sam-phinizy:diff-replace
Closed

sam-phinizy wants to merge 1 commit into
longbridge:mainfrom
sam-phinizy:diff-replace

Conversation

@sam-phinizy

Copy link
Copy Markdown

Description

set_files resets the viewport to the top. That's right for a different diff, but not for the same changes presented differently — the patch rebuilt with more context lines read from the files, or with whitespace-only changes left out — where jumping to the top loses the reader's place.

replace_files_in_place replaces the files like set_files, then scrolls back to the source line that was at the top, using the same anchor set_mode and set_context_lines already use (row_position / reveal_line).

Public API

gpui_component (diff::DiffState):

  • pub fn replace_files_in_place(&mut self, files: impl IntoIterator<Item = DiffFile>, cx: &mut Context<Self>) — replaces the files like set_files, keeping the source line at the top of the viewport where it was.

How to Test

cargo test -p gpui-component --lib diff:: — adds replacing_files_in_place_keeps_the_line_at_the_top in diff/scroll_tests.rs: scrolled to line 200 of a long patch, replacing it with the same file leaves the same row at the top.

Checklist

  • I have read the CONTRIBUTING document and followed the guidelines.
  • Reviewed the changes in this PR and confirmed AI generated code (If any) is accurate.
  • Passed cargo run for story tests related to the changes. (No story change; covered by the test above.)
  • Tested macOS, Windows and Linux platforms performance (if the change is platform-specific) — not platform-specific.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@huacnlee

Copy link
Copy Markdown
Member

💡 Small, correct, but a new public method on DiffState.

Head 86edc0f. cargo test -p gpui-component --lib diff: 65 passed. It reuses the existing row_position / reveal_line anchor that set_mode and set_context_lines already use, so behaviour is consistent.

API note: a parallel entry point next to set_files is one more method to keep in sync. A smaller shape could be keeping one set_files and letting callers do let anchor = state.row_position(...); state.set_files(..); state.reveal_line(&anchor) if those are public, or an option on set_files. Leaving the shape to the maintainer.

🤖 Noop (reviewer) · AI agent acting for @huacnlee

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants