Optimistically update hunk states when staging and unstaging hunks (#25687)

This PR adds an optimistic update when staging or unstaging diff hunks.
In the process, I've also refactored the logic for staging and unstaging
hunks, to consolidate more of it in the `buffer_diff` crate.

I've also changed the way that we treat untracked files. Previously, we
maintained an empty diff for them, so as not to show unwanted
entire-file diff hunks in a regular editor. But then in the project diff
view, we had to account for this, and replace these empty diffs with
entire-file diffs. This form of state management made it more difficult
to store the pending hunks, so now we always use the same
`BufferDiff`/`BufferDiffSnapshot` for untracked files (with a single
hunk spanning the entire buffer), but we just have a special case in
regular buffers, that avoids showing that entire-file hunk.

* [x] Avoid creating a long queue of `set_index` operations when
staging/unstaging rapidly
* [x] Keep pending hunks when diff is recalculated without base text
changes
* [x] Be optimistic even when staging the single hunk in added/deleted
files
* Testing

Release Notes:

- N/A

---------

Co-authored-by: Cole Miller <m@cole-miller.net>
This commit is contained in:
Max Brunsfeld
2025-02-28 20:55:29 +00:00
committed by GitHub
co-authored by Cole Miller
parent 9d8a163f5b
commit 0c2bbb3aa9
14 changed files with 756 additions and 600 deletions
+24 -40
View File
@@ -7,7 +7,7 @@ use crate::{
},
JoinLines,
};
use buffer_diff::{BufferDiff, DiffHunkStatus, DiffHunkStatusKind};
use buffer_diff::{BufferDiff, DiffHunkSecondaryStatus, DiffHunkStatus, DiffHunkStatusKind};
use futures::StreamExt;
use gpui::{
div, BackgroundExecutor, SemanticVersion, TestAppContext, UpdateGlobal, VisualTestContext,
@@ -12555,7 +12555,7 @@ async fn test_addition_reverts(cx: &mut TestAppContext) {
struct Row9.2;
struct Row9.3;
struct Row10;"#},
vec![DiffHunkStatus::added_none(), DiffHunkStatus::added_none()],
vec![DiffHunkStatusKind::Added, DiffHunkStatusKind::Added],
indoc! {r#"struct Row;
struct Row1;
struct Row1.1;
@@ -12593,7 +12593,7 @@ async fn test_addition_reverts(cx: &mut TestAppContext) {
struct Row8;
struct Row9;
struct Row10;"#},
vec![DiffHunkStatus::added_none(), DiffHunkStatus::added_none()],
vec![DiffHunkStatusKind::Added, DiffHunkStatusKind::Added],
indoc! {r#"struct Row;
struct Row1;
struct Row2;
@@ -12640,11 +12640,11 @@ async fn test_addition_reverts(cx: &mut TestAppContext) {
«ˇ// something on bottom»
struct Row10;"#},
vec![
DiffHunkStatus::added_none(),
DiffHunkStatus::added_none(),
DiffHunkStatus::added_none(),
DiffHunkStatus::added_none(),
DiffHunkStatus::added_none(),
DiffHunkStatusKind::Added,
DiffHunkStatusKind::Added,
DiffHunkStatusKind::Added,
DiffHunkStatusKind::Added,
DiffHunkStatusKind::Added,
],
indoc! {r#"struct Row;
ˇstruct Row1;
@@ -12692,10 +12692,7 @@ async fn test_modification_reverts(cx: &mut TestAppContext) {
struct Row99;
struct Row9;
struct Row10;"#},
vec![
DiffHunkStatus::modified_none(),
DiffHunkStatus::modified_none(),
],
vec![DiffHunkStatusKind::Modified, DiffHunkStatusKind::Modified],
indoc! {r#"struct Row;
struct Row1;
struct Row33;
@@ -12722,10 +12719,7 @@ async fn test_modification_reverts(cx: &mut TestAppContext) {
struct Row99;
struct Row9;
struct Row10;"#},
vec![
DiffHunkStatus::modified_none(),
DiffHunkStatus::modified_none(),
],
vec![DiffHunkStatusKind::Modified, DiffHunkStatusKind::Modified],
indoc! {r#"struct Row;
struct Row1;
struct Row33;
@@ -12754,12 +12748,12 @@ async fn test_modification_reverts(cx: &mut TestAppContext) {
struct Row9;
struct Row1011;ˇ"#},
vec![
DiffHunkStatus::modified_none(),
DiffHunkStatus::modified_none(),
DiffHunkStatus::modified_none(),
DiffHunkStatus::modified_none(),
DiffHunkStatus::modified_none(),
DiffHunkStatus::modified_none(),
DiffHunkStatusKind::Modified,
DiffHunkStatusKind::Modified,
DiffHunkStatusKind::Modified,
DiffHunkStatusKind::Modified,
DiffHunkStatusKind::Modified,
DiffHunkStatusKind::Modified,
],
indoc! {r#"struct Row;
ˇstruct Row1;
@@ -12837,10 +12831,7 @@ struct Row10;"#};
ˇ
struct Row8;
struct Row10;"#},
vec![
DiffHunkStatus::deleted_none(),
DiffHunkStatus::deleted_none(),
],
vec![DiffHunkStatusKind::Deleted, DiffHunkStatusKind::Deleted],
indoc! {r#"struct Row;
struct Row2;
@@ -12863,10 +12854,7 @@ struct Row10;"#};
ˇ»
struct Row8;
struct Row10;"#},
vec![
DiffHunkStatus::deleted_none(),
DiffHunkStatus::deleted_none(),
],
vec![DiffHunkStatusKind::Deleted, DiffHunkStatusKind::Deleted],
indoc! {r#"struct Row;
struct Row2;
@@ -12891,10 +12879,7 @@ struct Row10;"#};
struct Row8;ˇ
struct Row10;"#},
vec![
DiffHunkStatus::deleted_none(),
DiffHunkStatus::deleted_none(),
],
vec![DiffHunkStatusKind::Deleted, DiffHunkStatusKind::Deleted],
indoc! {r#"struct Row;
struct Row1;
ˇstruct Row2;
@@ -12919,9 +12904,9 @@ struct Row10;"#};
struct Row8;ˇ»
struct Row10;"#},
vec![
DiffHunkStatus::deleted_none(),
DiffHunkStatus::deleted_none(),
DiffHunkStatus::deleted_none(),
DiffHunkStatusKind::Deleted,
DiffHunkStatusKind::Deleted,
DiffHunkStatusKind::Deleted,
],
indoc! {r#"struct Row;
struct Row1;
@@ -16838,14 +16823,13 @@ pub(crate) fn init_test(cx: &mut TestAppContext, f: fn(&mut AllLanguageSettingsC
#[track_caller]
fn assert_hunk_revert(
not_reverted_text_with_selections: &str,
expected_hunk_statuses_before: Vec<DiffHunkStatus>,
expected_hunk_statuses_before: Vec<DiffHunkStatusKind>,
expected_reverted_text_with_selections: &str,
base_text: &str,
cx: &mut EditorLspTestContext,
) {
cx.set_state(not_reverted_text_with_selections);
cx.set_head_text(base_text);
cx.clear_index_text();
cx.executor().run_until_parked();
let actual_hunk_statuses_before = cx.update_editor(|editor, window, cx| {
@@ -16853,7 +16837,7 @@ fn assert_hunk_revert(
let reverted_hunk_statuses = snapshot
.buffer_snapshot
.diff_hunks_in_range(0..snapshot.buffer_snapshot.len())
.map(|hunk| hunk.status())
.map(|hunk| hunk.status().kind)
.collect::<Vec<_>>();
editor.git_restore(&Default::default(), window, cx);