Skip to content

Commit

Permalink
Get unstaged changes when excerpts of new buffers are added (#21619)
Browse files Browse the repository at this point in the history
This fixes an error on nightly, introduced in
#21258, where diffs were not
shown for buffers that were added to multi-buffers after construction.

Release Notes:

- N/A
  • Loading branch information
maxbrunsfeld authored Dec 6, 2024
1 parent cf4e847 commit f6b5e17
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2023,28 +2023,7 @@ impl Editor {

let mut code_action_providers = Vec::new();
if let Some(project) = project.clone() {
let mut tasks = Vec::new();
buffer.update(cx, |multibuffer, cx| {
project.update(cx, |project, cx| {
multibuffer.for_each_buffer(|buffer| {
tasks.push(project.open_unstaged_changes(buffer.clone(), cx))
});
});
});

cx.spawn(|this, mut cx| async move {
let change_sets = futures::future::join_all(tasks).await;
this.update(&mut cx, |this, cx| {
for change_set in change_sets {
if let Some(change_set) = change_set.log_err() {
this.diff_map.add_change_set(change_set, cx);
}
}
})
.ok();
})
.detach();

get_unstaged_changes_for_buffers(&project, buffer.read(cx).all_buffers(), cx);
code_action_providers.push(Arc::new(project) as Arc<_>);
}

Expand Down Expand Up @@ -12646,6 +12625,12 @@ impl Editor {
excerpts,
} => {
self.tasks_update_task = Some(self.refresh_runnables(cx));
let buffer_id = buffer.read(cx).remote_id();
if !self.diff_map.diff_bases.contains_key(&buffer_id) {
if let Some(project) = &self.project {
get_unstaged_changes_for_buffers(project, [buffer.clone()], cx);
}
}
cx.emit(EditorEvent::ExcerptsAdded {
buffer: buffer.clone(),
predecessor: *predecessor,
Expand Down Expand Up @@ -13342,6 +13327,31 @@ impl Editor {
}
}

fn get_unstaged_changes_for_buffers(
project: &Model<Project>,
buffers: impl IntoIterator<Item = Model<Buffer>>,
cx: &mut ViewContext<Editor>,
) {
let mut tasks = Vec::new();
project.update(cx, |project, cx| {
for buffer in buffers {
tasks.push(project.open_unstaged_changes(buffer.clone(), cx))
}
});
cx.spawn(|this, mut cx| async move {
let change_sets = futures::future::join_all(tasks).await;
this.update(&mut cx, |this, cx| {
for change_set in change_sets {
if let Some(change_set) = change_set.log_err() {
this.diff_map.add_change_set(change_set, cx);
}
}
})
.ok();
})
.detach();
}

fn char_len_with_expanded_tabs(offset: usize, text: &str, tab_size: NonZeroU32) -> usize {
let tab_size = tab_size.get() as usize;
let mut width = offset;
Expand Down

0 comments on commit f6b5e17

Please sign in to comment.