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:
parent
9d8a163f5b
commit
0c2bbb3aa9
14 changed files with 756 additions and 600 deletions
|
@ -73,7 +73,7 @@ impl Anchor {
|
|||
if let Some(base_text) = snapshot
|
||||
.diffs
|
||||
.get(&excerpt.buffer_id)
|
||||
.and_then(|diff| diff.base_text())
|
||||
.map(|diff| diff.base_text())
|
||||
{
|
||||
let self_anchor = self.diff_base_anchor.filter(|a| base_text.can_resolve(a));
|
||||
let other_anchor = other.diff_base_anchor.filter(|a| base_text.can_resolve(a));
|
||||
|
@ -110,7 +110,7 @@ impl Anchor {
|
|||
if let Some(base_text) = snapshot
|
||||
.diffs
|
||||
.get(&excerpt.buffer_id)
|
||||
.and_then(|diff| diff.base_text())
|
||||
.map(|diff| diff.base_text())
|
||||
{
|
||||
if a.buffer_id == Some(base_text.remote_id()) {
|
||||
return a.bias_left(base_text);
|
||||
|
@ -135,7 +135,7 @@ impl Anchor {
|
|||
if let Some(base_text) = snapshot
|
||||
.diffs
|
||||
.get(&excerpt.buffer_id)
|
||||
.and_then(|diff| diff.base_text())
|
||||
.map(|diff| diff.base_text())
|
||||
{
|
||||
if a.buffer_id == Some(base_text.remote_id()) {
|
||||
return a.bias_right(&base_text);
|
||||
|
|
|
@ -69,7 +69,7 @@ pub struct MultiBuffer {
|
|||
// only used by consumers using `set_excerpts_for_buffer`
|
||||
buffers_by_path: BTreeMap<PathKey, Vec<ExcerptId>>,
|
||||
diffs: HashMap<BufferId, DiffState>,
|
||||
all_diff_hunks_expanded: bool,
|
||||
// all_diff_hunks_expanded: bool,
|
||||
subscriptions: Topic,
|
||||
/// If true, the multi-buffer only contains a single [`Buffer`] and a single [`Excerpt`]
|
||||
singleton: bool,
|
||||
|
@ -245,14 +245,9 @@ impl DiffState {
|
|||
DiffState {
|
||||
_subscription: cx.subscribe(&diff, |this, diff, event, cx| match event {
|
||||
BufferDiffEvent::DiffChanged { changed_range } => {
|
||||
let changed_range = if let Some(changed_range) = changed_range {
|
||||
changed_range.clone()
|
||||
} else if diff.read(cx).base_text().is_none() && this.all_diff_hunks_expanded {
|
||||
text::Anchor::MIN..text::Anchor::MAX
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
this.buffer_diff_changed(diff, changed_range, cx)
|
||||
if let Some(changed_range) = changed_range.clone() {
|
||||
this.buffer_diff_changed(diff, changed_range, cx)
|
||||
}
|
||||
}
|
||||
BufferDiffEvent::LanguageChanged => this.buffer_diff_language_changed(diff, cx),
|
||||
}),
|
||||
|
@ -270,6 +265,7 @@ pub struct MultiBufferSnapshot {
|
|||
diffs: TreeMap<BufferId, BufferDiffSnapshot>,
|
||||
diff_transforms: SumTree<DiffTransform>,
|
||||
trailing_excerpt_update_count: usize,
|
||||
all_diff_hunks_expanded: bool,
|
||||
non_text_state_update_count: usize,
|
||||
edit_count: usize,
|
||||
is_dirty: bool,
|
||||
|
@ -559,7 +555,6 @@ impl MultiBuffer {
|
|||
}),
|
||||
buffers: RefCell::default(),
|
||||
diffs: HashMap::default(),
|
||||
all_diff_hunks_expanded: false,
|
||||
subscriptions: Topic::default(),
|
||||
singleton: false,
|
||||
capability,
|
||||
|
@ -581,7 +576,6 @@ impl MultiBuffer {
|
|||
buffers: Default::default(),
|
||||
buffers_by_path: Default::default(),
|
||||
diffs: HashMap::default(),
|
||||
all_diff_hunks_expanded: false,
|
||||
subscriptions: Default::default(),
|
||||
singleton: false,
|
||||
capability,
|
||||
|
@ -622,7 +616,6 @@ impl MultiBuffer {
|
|||
buffers: RefCell::new(buffers),
|
||||
buffers_by_path: Default::default(),
|
||||
diffs: diff_bases,
|
||||
all_diff_hunks_expanded: self.all_diff_hunks_expanded,
|
||||
subscriptions: Default::default(),
|
||||
singleton: self.singleton,
|
||||
capability: self.capability,
|
||||
|
@ -2231,18 +2224,7 @@ impl MultiBuffer {
|
|||
let buffer = buffer_state.buffer.read(cx);
|
||||
let diff_change_range = range.to_offset(buffer);
|
||||
|
||||
let mut new_diff = diff.snapshot(cx);
|
||||
if new_diff.base_text().is_none() && self.all_diff_hunks_expanded {
|
||||
let secondary_diff_insertion = new_diff
|
||||
.secondary_diff()
|
||||
.map_or(true, |secondary_diff| secondary_diff.base_text().is_none());
|
||||
new_diff = BufferDiff::build_with_single_insertion(
|
||||
secondary_diff_insertion,
|
||||
buffer.snapshot(),
|
||||
cx,
|
||||
);
|
||||
}
|
||||
|
||||
let new_diff = diff.snapshot(cx);
|
||||
let mut snapshot = self.snapshot.borrow_mut();
|
||||
let base_text_changed = snapshot
|
||||
.diffs
|
||||
|
@ -2398,12 +2380,12 @@ impl MultiBuffer {
|
|||
}
|
||||
|
||||
pub fn set_all_diff_hunks_expanded(&mut self, cx: &mut Context<Self>) {
|
||||
self.all_diff_hunks_expanded = true;
|
||||
self.snapshot.borrow_mut().all_diff_hunks_expanded = true;
|
||||
self.expand_or_collapse_diff_hunks(vec![Anchor::min()..Anchor::max()], true, cx);
|
||||
}
|
||||
|
||||
pub fn all_diff_hunks_expanded(&self) -> bool {
|
||||
self.all_diff_hunks_expanded
|
||||
self.snapshot.borrow().all_diff_hunks_expanded
|
||||
}
|
||||
|
||||
pub fn has_multiple_hunks(&self, cx: &App) -> bool {
|
||||
|
@ -2459,7 +2441,7 @@ impl MultiBuffer {
|
|||
expand: bool,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
if self.all_diff_hunks_expanded && !expand {
|
||||
if self.snapshot.borrow().all_diff_hunks_expanded && !expand {
|
||||
return;
|
||||
}
|
||||
self.sync(cx);
|
||||
|
@ -2964,9 +2946,10 @@ impl MultiBuffer {
|
|||
}
|
||||
|
||||
// Avoid querying diff hunks if there's no possibility of hunks being expanded.
|
||||
let all_diff_hunks_expanded = snapshot.all_diff_hunks_expanded;
|
||||
if old_expanded_hunks.is_empty()
|
||||
&& change_kind == DiffChangeKind::BufferEdited
|
||||
&& !self.all_diff_hunks_expanded
|
||||
&& !all_diff_hunks_expanded
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -2976,11 +2959,7 @@ impl MultiBuffer {
|
|||
while let Some(excerpt) = excerpts.item() {
|
||||
// Recompute the expanded hunks in the portion of the excerpt that
|
||||
// intersects the edit.
|
||||
if let Some((diff, base_text)) = snapshot
|
||||
.diffs
|
||||
.get(&excerpt.buffer_id)
|
||||
.and_then(|diff| Some((diff, diff.base_text()?)))
|
||||
{
|
||||
if let Some(diff) = snapshot.diffs.get(&excerpt.buffer_id) {
|
||||
let buffer = &excerpt.buffer;
|
||||
let excerpt_start = *excerpts.start();
|
||||
let excerpt_end = excerpt_start + ExcerptOffset::new(excerpt.text_summary.len);
|
||||
|
@ -2995,17 +2974,21 @@ impl MultiBuffer {
|
|||
buffer.anchor_before(edit_buffer_start)..buffer.anchor_after(edit_buffer_end);
|
||||
|
||||
for hunk in diff.hunks_intersecting_range(edit_anchor_range, buffer) {
|
||||
if hunk.is_created_file() && !all_diff_hunks_expanded {
|
||||
continue;
|
||||
}
|
||||
|
||||
let hunk_buffer_range = hunk.buffer_range.to_offset(buffer);
|
||||
if hunk_buffer_range.start < excerpt_buffer_start {
|
||||
log::trace!("skipping hunk that starts before excerpt");
|
||||
continue;
|
||||
}
|
||||
|
||||
let hunk_info = DiffTransformHunkInfo {
|
||||
excerpt_id: excerpt.id,
|
||||
hunk_start_anchor: hunk.buffer_range.start,
|
||||
hunk_secondary_status: hunk.secondary_status,
|
||||
};
|
||||
if hunk_buffer_range.start < excerpt_buffer_start {
|
||||
log::trace!("skipping hunk that starts before excerpt");
|
||||
continue;
|
||||
}
|
||||
|
||||
let hunk_excerpt_start = excerpt_start
|
||||
+ ExcerptOffset::new(
|
||||
|
@ -3028,21 +3011,18 @@ impl MultiBuffer {
|
|||
let was_previously_expanded = old_expanded_hunks.contains(&hunk_info);
|
||||
let should_expand_hunk = match &change_kind {
|
||||
DiffChangeKind::DiffUpdated { base_changed: true } => {
|
||||
self.all_diff_hunks_expanded || was_previously_expanded
|
||||
was_previously_expanded || all_diff_hunks_expanded
|
||||
}
|
||||
DiffChangeKind::ExpandOrCollapseHunks { expand } => {
|
||||
let intersects = hunk_buffer_range.is_empty()
|
||||
|| hunk_buffer_range.end > edit_buffer_start;
|
||||
if *expand {
|
||||
intersects
|
||||
|| was_previously_expanded
|
||||
|| self.all_diff_hunks_expanded
|
||||
intersects || was_previously_expanded || all_diff_hunks_expanded
|
||||
} else {
|
||||
!intersects
|
||||
&& (was_previously_expanded || self.all_diff_hunks_expanded)
|
||||
!intersects && (was_previously_expanded || all_diff_hunks_expanded)
|
||||
}
|
||||
}
|
||||
_ => was_previously_expanded || self.all_diff_hunks_expanded,
|
||||
_ => was_previously_expanded || all_diff_hunks_expanded,
|
||||
};
|
||||
|
||||
if should_expand_hunk {
|
||||
|
@ -3057,6 +3037,7 @@ impl MultiBuffer {
|
|||
&& hunk_buffer_range.start >= edit_buffer_start
|
||||
&& hunk_buffer_range.start <= excerpt_buffer_end
|
||||
{
|
||||
let base_text = diff.base_text();
|
||||
let mut text_cursor =
|
||||
base_text.as_rope().cursor(hunk.diff_base_byte_range.start);
|
||||
let mut base_text_summary =
|
||||
|
@ -3500,11 +3481,14 @@ impl MultiBufferSnapshot {
|
|||
let buffer_end = buffer.anchor_after(buffer_range.end);
|
||||
Some(
|
||||
diff.hunks_intersecting_range(buffer_start..buffer_end, buffer)
|
||||
.map(|hunk| {
|
||||
(
|
||||
.filter_map(|hunk| {
|
||||
if hunk.is_created_file() && !self.all_diff_hunks_expanded {
|
||||
return None;
|
||||
}
|
||||
Some((
|
||||
Point::new(hunk.row_range.start, 0)..Point::new(hunk.row_range.end, 0),
|
||||
hunk,
|
||||
)
|
||||
))
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
@ -4383,8 +4367,7 @@ impl MultiBufferSnapshot {
|
|||
} => {
|
||||
let buffer_start = base_text_byte_range.start + start_overshoot;
|
||||
let mut buffer_end = base_text_byte_range.start + end_overshoot;
|
||||
let Some(base_text) = self.diffs.get(buffer_id).and_then(|diff| diff.base_text())
|
||||
else {
|
||||
let Some(base_text) = self.diffs.get(buffer_id).map(|diff| diff.base_text()) else {
|
||||
panic!("{:?} is in non-existent deleted hunk", range.start)
|
||||
};
|
||||
|
||||
|
@ -4432,8 +4415,7 @@ impl MultiBufferSnapshot {
|
|||
..
|
||||
} => {
|
||||
let buffer_end = base_text_byte_range.start + overshoot;
|
||||
let Some(base_text) = self.diffs.get(buffer_id).and_then(|diff| diff.base_text())
|
||||
else {
|
||||
let Some(base_text) = self.diffs.get(buffer_id).map(|diff| diff.base_text()) else {
|
||||
panic!("{:?} is in non-existent deleted hunk", range.end)
|
||||
};
|
||||
|
||||
|
@ -4537,7 +4519,7 @@ impl MultiBufferSnapshot {
|
|||
}) => {
|
||||
if let Some(diff_base_anchor) = &anchor.diff_base_anchor {
|
||||
if let Some(base_text) =
|
||||
self.diffs.get(buffer_id).and_then(|diff| diff.base_text())
|
||||
self.diffs.get(buffer_id).map(|diff| diff.base_text())
|
||||
{
|
||||
if base_text.can_resolve(&diff_base_anchor) {
|
||||
let base_text_offset = diff_base_anchor.to_offset(&base_text);
|
||||
|
@ -4867,17 +4849,14 @@ impl MultiBufferSnapshot {
|
|||
..
|
||||
}) = diff_transforms.item()
|
||||
{
|
||||
let base_text = self
|
||||
.diffs
|
||||
.get(buffer_id)
|
||||
.and_then(|diff| diff.base_text())
|
||||
.expect("missing diff base");
|
||||
let diff = self.diffs.get(buffer_id).expect("missing diff");
|
||||
if offset_in_transform > base_text_byte_range.len() {
|
||||
debug_assert!(*has_trailing_newline);
|
||||
bias = Bias::Right;
|
||||
} else {
|
||||
diff_base_anchor = Some(
|
||||
base_text.anchor_at(base_text_byte_range.start + offset_in_transform, bias),
|
||||
diff.base_text()
|
||||
.anchor_at(base_text_byte_range.start + offset_in_transform, bias),
|
||||
);
|
||||
bias = Bias::Left;
|
||||
}
|
||||
|
@ -6235,7 +6214,7 @@ where
|
|||
..
|
||||
} => {
|
||||
let diff = self.diffs.get(&buffer_id)?;
|
||||
let buffer = diff.base_text()?;
|
||||
let buffer = diff.base_text();
|
||||
let mut rope_cursor = buffer.as_rope().cursor(0);
|
||||
let buffer_start = rope_cursor.summary::<D>(base_text_byte_range.start);
|
||||
let buffer_range_len = rope_cursor.summary::<D>(base_text_byte_range.end);
|
||||
|
@ -7282,7 +7261,7 @@ impl<'a> Iterator for MultiBufferChunks<'a> {
|
|||
}
|
||||
chunks
|
||||
} else {
|
||||
let base_buffer = &self.diffs.get(&buffer_id)?.base_text()?;
|
||||
let base_buffer = &self.diffs.get(&buffer_id)?.base_text();
|
||||
base_buffer.chunks(base_text_start..base_text_end, self.language_aware)
|
||||
};
|
||||
|
||||
|
|
|
@ -1999,8 +1999,8 @@ fn test_diff_hunks_with_multiple_excerpts(cx: &mut TestAppContext) {
|
|||
|
||||
let id_1 = buffer_1.read_with(cx, |buffer, _| buffer.remote_id());
|
||||
let id_2 = buffer_2.read_with(cx, |buffer, _| buffer.remote_id());
|
||||
let base_id_1 = diff_1.read_with(cx, |diff, _| diff.base_text().as_ref().unwrap().remote_id());
|
||||
let base_id_2 = diff_2.read_with(cx, |diff, _| diff.base_text().as_ref().unwrap().remote_id());
|
||||
let base_id_1 = diff_1.read_with(cx, |diff, _| diff.base_text().remote_id());
|
||||
let base_id_2 = diff_2.read_with(cx, |diff, _| diff.base_text().remote_id());
|
||||
|
||||
let buffer_lines = (0..=snapshot.max_row().0)
|
||||
.map(|row| {
|
||||
|
@ -2221,8 +2221,7 @@ impl ReferenceMultibuffer {
|
|||
let buffer = excerpt.buffer.read(cx);
|
||||
let buffer_range = excerpt.range.to_offset(buffer);
|
||||
let diff = self.diffs.get(&buffer.remote_id()).unwrap().read(cx);
|
||||
// let diff = diff.snapshot.clone();
|
||||
let base_buffer = diff.base_text().unwrap();
|
||||
let base_buffer = diff.base_text();
|
||||
|
||||
let mut offset = buffer_range.start;
|
||||
let mut hunks = diff
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue