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:
co-authored by
Cole Miller
parent
9d8a163f5b
commit
0c2bbb3aa9
+25
-45
@@ -52,7 +52,7 @@ pub use actions::{AcceptEditPrediction, OpenExcerpts, OpenExcerptsSplit};
|
||||
use aho_corasick::AhoCorasick;
|
||||
use anyhow::{anyhow, Context as _, Result};
|
||||
use blink_manager::BlinkManager;
|
||||
use buffer_diff::{DiffHunkSecondaryStatus, DiffHunkStatus};
|
||||
use buffer_diff::DiffHunkStatus;
|
||||
use client::{Collaborator, ParticipantIndex};
|
||||
use clock::ReplicaId;
|
||||
use collections::{BTreeMap, HashMap, HashSet, VecDeque};
|
||||
@@ -7720,11 +7720,6 @@ impl Editor {
|
||||
cx: &mut Context<Editor>,
|
||||
) {
|
||||
let mut revert_changes = HashMap::default();
|
||||
let snapshot = self.buffer.read(cx).snapshot(cx);
|
||||
let Some(project) = &self.project else {
|
||||
return;
|
||||
};
|
||||
|
||||
let chunk_by = self
|
||||
.snapshot(window, cx)
|
||||
.hunks_for_ranges(ranges.into_iter())
|
||||
@@ -7735,15 +7730,7 @@ impl Editor {
|
||||
for hunk in &hunks {
|
||||
self.prepare_restore_change(&mut revert_changes, hunk, cx);
|
||||
}
|
||||
Self::do_stage_or_unstage(
|
||||
project,
|
||||
false,
|
||||
buffer_id,
|
||||
hunks.into_iter(),
|
||||
&snapshot,
|
||||
window,
|
||||
cx,
|
||||
);
|
||||
self.do_stage_or_unstage(false, buffer_id, hunks.into_iter(), window, cx);
|
||||
}
|
||||
drop(chunk_by);
|
||||
if !revert_changes.is_empty() {
|
||||
@@ -7788,7 +7775,6 @@ impl Editor {
|
||||
let original_text = diff
|
||||
.read(cx)
|
||||
.base_text()
|
||||
.as_ref()?
|
||||
.as_rope()
|
||||
.slice(hunk.diff_base_byte_range.clone());
|
||||
let buffer_snapshot = buffer.snapshot();
|
||||
@@ -13524,7 +13510,7 @@ impl Editor {
|
||||
snapshot: &MultiBufferSnapshot,
|
||||
) -> bool {
|
||||
let mut hunks = self.diff_hunks_in_ranges(ranges, &snapshot);
|
||||
hunks.any(|hunk| hunk.secondary_status != DiffHunkSecondaryStatus::None)
|
||||
hunks.any(|hunk| hunk.status().has_secondary_hunk())
|
||||
}
|
||||
|
||||
pub fn toggle_staged_selected_diff_hunks(
|
||||
@@ -13565,15 +13551,11 @@ impl Editor {
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
let snapshot = self.buffer.read(cx).snapshot(cx);
|
||||
let Some(project) = &self.project else {
|
||||
return;
|
||||
};
|
||||
|
||||
let chunk_by = self
|
||||
.diff_hunks_in_ranges(&ranges, &snapshot)
|
||||
.chunk_by(|hunk| hunk.buffer_id);
|
||||
for (buffer_id, hunks) in &chunk_by {
|
||||
Self::do_stage_or_unstage(project, stage, buffer_id, hunks, &snapshot, window, cx);
|
||||
self.do_stage_or_unstage(stage, buffer_id, hunks, window, cx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13647,16 +13629,20 @@ impl Editor {
|
||||
}
|
||||
|
||||
fn do_stage_or_unstage(
|
||||
project: &Entity<Project>,
|
||||
&self,
|
||||
stage: bool,
|
||||
buffer_id: BufferId,
|
||||
hunks: impl Iterator<Item = MultiBufferDiffHunk>,
|
||||
snapshot: &MultiBufferSnapshot,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) {
|
||||
let Some(project) = self.project.as_ref() else {
|
||||
return;
|
||||
};
|
||||
let Some(buffer) = project.read(cx).buffer_for_id(buffer_id, cx) else {
|
||||
log::debug!("no buffer for id");
|
||||
return;
|
||||
};
|
||||
let Some(diff) = self.buffer.read(cx).diff_for(buffer_id) else {
|
||||
return;
|
||||
};
|
||||
let buffer_snapshot = buffer.read(cx).snapshot();
|
||||
@@ -13670,37 +13656,31 @@ impl Editor {
|
||||
log::debug!("no git repo for buffer id");
|
||||
return;
|
||||
};
|
||||
let Some(diff) = snapshot.diff_for_buffer_id(buffer_id) else {
|
||||
log::debug!("no diff for buffer id");
|
||||
return;
|
||||
};
|
||||
|
||||
let new_index_text = if !stage && diff.is_single_insertion || stage && !file_exists {
|
||||
log::debug!("removing from index");
|
||||
None
|
||||
} else {
|
||||
diff.new_secondary_text_for_stage_or_unstage(
|
||||
let new_index_text = diff.update(cx, |diff, cx| {
|
||||
diff.stage_or_unstage_hunks(
|
||||
stage,
|
||||
hunks.filter_map(|hunk| {
|
||||
if stage && hunk.secondary_status == DiffHunkSecondaryStatus::None {
|
||||
return None;
|
||||
} else if !stage
|
||||
&& hunk.secondary_status == DiffHunkSecondaryStatus::HasSecondaryHunk
|
||||
{
|
||||
return None;
|
||||
}
|
||||
Some((hunk.buffer_range.clone(), hunk.diff_base_byte_range.clone()))
|
||||
}),
|
||||
&hunks
|
||||
.map(|hunk| buffer_diff::DiffHunk {
|
||||
buffer_range: hunk.buffer_range,
|
||||
diff_base_byte_range: hunk.diff_base_byte_range,
|
||||
secondary_status: hunk.secondary_status,
|
||||
row_range: 0..0, // unused
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
&buffer_snapshot,
|
||||
file_exists,
|
||||
cx,
|
||||
)
|
||||
};
|
||||
});
|
||||
|
||||
if file_exists {
|
||||
let buffer_store = project.read(cx).buffer_store().clone();
|
||||
buffer_store
|
||||
.update(cx, |buffer_store, cx| buffer_store.save_buffer(buffer, cx))
|
||||
.detach_and_log_err(cx);
|
||||
}
|
||||
|
||||
let recv = repo
|
||||
.read(cx)
|
||||
.set_index_text(&path, new_index_text.map(|rope| rope.to_string()));
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -26,7 +26,7 @@ use crate::{
|
||||
FILE_HEADER_HEIGHT, GIT_BLAME_MAX_AUTHOR_CHARS_DISPLAYED, MAX_LINE_LEN,
|
||||
MULTI_BUFFER_EXCERPT_HEADER_HEIGHT,
|
||||
};
|
||||
use buffer_diff::{DiffHunkSecondaryStatus, DiffHunkStatus, DiffHunkStatusKind};
|
||||
use buffer_diff::{DiffHunkStatus, DiffHunkStatusKind};
|
||||
use client::ParticipantIndex;
|
||||
use collections::{BTreeMap, HashMap, HashSet};
|
||||
use file_icons::FileIcons;
|
||||
@@ -4365,7 +4365,7 @@ impl EditorElement {
|
||||
hunk_bounds,
|
||||
cx.theme().colors().version_control_modified,
|
||||
Corners::all(px(0.)),
|
||||
DiffHunkSecondaryStatus::None,
|
||||
DiffHunkStatus::modified_none(),
|
||||
))
|
||||
}
|
||||
DisplayDiffHunk::Unfolded {
|
||||
@@ -4377,19 +4377,19 @@ impl EditorElement {
|
||||
hunk_hitbox.bounds,
|
||||
cx.theme().colors().version_control_added,
|
||||
Corners::all(px(0.)),
|
||||
status.secondary,
|
||||
*status,
|
||||
),
|
||||
DiffHunkStatusKind::Modified => (
|
||||
hunk_hitbox.bounds,
|
||||
cx.theme().colors().version_control_modified,
|
||||
Corners::all(px(0.)),
|
||||
status.secondary,
|
||||
*status,
|
||||
),
|
||||
DiffHunkStatusKind::Deleted if !display_row_range.is_empty() => (
|
||||
hunk_hitbox.bounds,
|
||||
cx.theme().colors().version_control_deleted,
|
||||
Corners::all(px(0.)),
|
||||
status.secondary,
|
||||
*status,
|
||||
),
|
||||
DiffHunkStatusKind::Deleted => (
|
||||
Bounds::new(
|
||||
@@ -4401,19 +4401,18 @@ impl EditorElement {
|
||||
),
|
||||
cx.theme().colors().version_control_deleted,
|
||||
Corners::all(1. * line_height),
|
||||
status.secondary,
|
||||
*status,
|
||||
),
|
||||
}),
|
||||
};
|
||||
|
||||
if let Some((hunk_bounds, background_color, corner_radii, secondary_status)) =
|
||||
if let Some((hunk_bounds, mut background_color, corner_radii, secondary_status)) =
|
||||
hunk_to_paint
|
||||
{
|
||||
let background_color = if secondary_status != DiffHunkSecondaryStatus::None {
|
||||
background_color.opacity(if is_light { 0.2 } else { 0.32 })
|
||||
} else {
|
||||
background_color.opacity(1.0)
|
||||
};
|
||||
if secondary_status.has_secondary_hunk() {
|
||||
background_color =
|
||||
background_color.opacity(if is_light { 0.2 } else { 0.32 });
|
||||
}
|
||||
window.paint_quad(quad(
|
||||
hunk_bounds,
|
||||
corner_radii,
|
||||
@@ -6728,12 +6727,11 @@ impl Element for EditorElement {
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let background_color =
|
||||
if diff_status.secondary == DiffHunkSecondaryStatus::None {
|
||||
background_color.opacity(staged_opacity)
|
||||
} else {
|
||||
background_color.opacity(unstaged_opacity)
|
||||
};
|
||||
let background_color = if diff_status.has_secondary_hunk() {
|
||||
background_color.opacity(unstaged_opacity)
|
||||
} else {
|
||||
background_color.opacity(staged_opacity)
|
||||
};
|
||||
|
||||
highlighted_rows
|
||||
.entry(start_row + DisplayRow(ix as u32))
|
||||
@@ -8780,65 +8778,62 @@ fn diff_hunk_controls(
|
||||
.rounded_b_lg()
|
||||
.bg(cx.theme().colors().editor_background)
|
||||
.gap_1()
|
||||
.when(status.secondary == DiffHunkSecondaryStatus::None, |el| {
|
||||
el.child(
|
||||
Button::new("unstage", "Unstage")
|
||||
.tooltip({
|
||||
let focus_handle = editor.focus_handle(cx);
|
||||
move |window, cx| {
|
||||
Tooltip::for_action_in(
|
||||
"Unstage Hunk",
|
||||
&::git::ToggleStaged,
|
||||
&focus_handle,
|
||||
.child(if status.has_secondary_hunk() {
|
||||
Button::new(("stage", row as u64), "Stage")
|
||||
.alpha(if status.is_pending() { 0.66 } else { 1.0 })
|
||||
.tooltip({
|
||||
let focus_handle = editor.focus_handle(cx);
|
||||
move |window, cx| {
|
||||
Tooltip::for_action_in(
|
||||
"Stage Hunk",
|
||||
&::git::ToggleStaged,
|
||||
&focus_handle,
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
}
|
||||
})
|
||||
.on_click({
|
||||
let editor = editor.clone();
|
||||
move |_event, window, cx| {
|
||||
editor.update(cx, |editor, cx| {
|
||||
editor.stage_or_unstage_diff_hunks(
|
||||
true,
|
||||
&[hunk_range.start..hunk_range.start],
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
}
|
||||
})
|
||||
.on_click({
|
||||
let editor = editor.clone();
|
||||
move |_event, window, cx| {
|
||||
editor.update(cx, |editor, cx| {
|
||||
editor.stage_or_unstage_diff_hunks(
|
||||
false,
|
||||
&[hunk_range.start..hunk_range.start],
|
||||
window,
|
||||
cx,
|
||||
);
|
||||
});
|
||||
}
|
||||
}),
|
||||
)
|
||||
})
|
||||
.when(status.secondary != DiffHunkSecondaryStatus::None, |el| {
|
||||
el.child(
|
||||
Button::new("stage", "Stage")
|
||||
.tooltip({
|
||||
let focus_handle = editor.focus_handle(cx);
|
||||
move |window, cx| {
|
||||
Tooltip::for_action_in(
|
||||
"Stage Hunk",
|
||||
&::git::ToggleStaged,
|
||||
&focus_handle,
|
||||
);
|
||||
});
|
||||
}
|
||||
})
|
||||
} else {
|
||||
Button::new(("unstage", row as u64), "Unstage")
|
||||
.alpha(if status.is_pending() { 0.66 } else { 1.0 })
|
||||
.tooltip({
|
||||
let focus_handle = editor.focus_handle(cx);
|
||||
move |window, cx| {
|
||||
Tooltip::for_action_in(
|
||||
"Unstage Hunk",
|
||||
&::git::ToggleStaged,
|
||||
&focus_handle,
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
}
|
||||
})
|
||||
.on_click({
|
||||
let editor = editor.clone();
|
||||
move |_event, window, cx| {
|
||||
editor.update(cx, |editor, cx| {
|
||||
editor.stage_or_unstage_diff_hunks(
|
||||
false,
|
||||
&[hunk_range.start..hunk_range.start],
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
}
|
||||
})
|
||||
.on_click({
|
||||
let editor = editor.clone();
|
||||
move |_event, window, cx| {
|
||||
editor.update(cx, |editor, cx| {
|
||||
editor.stage_or_unstage_diff_hunks(
|
||||
true,
|
||||
&[hunk_range.start..hunk_range.start],
|
||||
window,
|
||||
cx,
|
||||
);
|
||||
});
|
||||
}
|
||||
}),
|
||||
)
|
||||
);
|
||||
});
|
||||
}
|
||||
})
|
||||
})
|
||||
.child(
|
||||
Button::new("discard", "Restore")
|
||||
|
||||
@@ -185,7 +185,7 @@ impl ProposedChangesEditor {
|
||||
} else {
|
||||
branch_buffer = location.buffer.update(cx, |buffer, cx| buffer.branch(cx));
|
||||
new_diffs.push(cx.new(|cx| {
|
||||
let mut diff = BufferDiff::new(branch_buffer.read(cx));
|
||||
let mut diff = BufferDiff::new(&branch_buffer.read(cx).snapshot(), cx);
|
||||
let _ = diff.set_base_text(
|
||||
location.buffer.clone(),
|
||||
branch_buffer.read(cx).text_snapshot(),
|
||||
|
||||
Reference in New Issue
Block a user