diff --git a/crates/diagnostics/src/diagnostics.rs b/crates/diagnostics/src/diagnostics.rs index c78d54e424beed88c7a20b4ff5e031901a5989ed..de17cdc210c33cf76c46ad99b8fb9d8110bcfde0 100644 --- a/crates/diagnostics/src/diagnostics.rs +++ b/crates/diagnostics/src/diagnostics.rs @@ -191,7 +191,7 @@ impl ProjectDiagnosticsEditor { for selection in editor.local_selections::(cx) { for (buffer, mut range) in - excerpts.excerpted_buffers(selection.start..selection.end, cx) + excerpts.range_to_buffer_ranges(selection.start..selection.end, cx) { if selection.reversed { mem::swap(&mut range.start, &mut range.end); diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 0a9baa1f139414a02f4607df53e5da747f2c5e5b..8aa922eb5b3f3ebae074db65eb5e9662dded58f8 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -2114,6 +2114,35 @@ impl Editor { Some(cx.spawn(|workspace, mut cx| async move { let project_transaction = apply_code_actions.await?; + // If the code action's edits are all contained within this editor, then + // avoid opening a new editor to display them. + let mut entries = project_transaction.0.iter(); + if let Some((buffer, transaction)) = entries.next() { + if entries.next().is_none() { + let excerpt = editor.read_with(&cx, |editor, cx| { + editor + .buffer() + .read(cx) + .excerpt_containing(editor.newest_anchor_selection().head(), cx) + }); + if let Some((excerpted_buffer, excerpt_range)) = excerpt { + if excerpted_buffer == *buffer { + let snapshot = buffer.read_with(&cx, |buffer, _| buffer.snapshot()); + let excerpt_range = excerpt_range.to_offset(&snapshot); + if snapshot + .edited_ranges_for_transaction(transaction) + .all(|range| { + excerpt_range.start <= range.start + && excerpt_range.end >= range.end + }) + { + return Ok(()); + } + } + } + } + } + let mut ranges_to_highlight = Vec::new(); let excerpt_buffer = cx.add_model(|cx| { let mut multibuffer = MultiBuffer::new(replica_id).with_title(title); diff --git a/crates/editor/src/multi_buffer.rs b/crates/editor/src/multi_buffer.rs index acc7b22183a0d0501bf7ef2ffc4caee85b88c6cb..cd6e3dc1409957fdf1232b91cd10c6ec05333e17 100644 --- a/crates/editor/src/multi_buffer.rs +++ b/crates/editor/src/multi_buffer.rs @@ -779,12 +779,35 @@ impl MultiBuffer { .map_or(Vec::new(), |state| state.excerpts.clone()) } - pub fn excerpted_buffers<'a, T: ToOffset>( + pub fn excerpt_containing( + &self, + position: impl ToOffset, + cx: &AppContext, + ) -> Option<(ModelHandle, Range)> { + let snapshot = self.read(cx); + let position = position.to_offset(&snapshot); + + let mut cursor = snapshot.excerpts.cursor::(); + cursor.seek(&position, Bias::Right, &()); + cursor.item().map(|excerpt| { + ( + self.buffers + .borrow() + .get(&excerpt.buffer_id) + .unwrap() + .buffer + .clone(), + excerpt.range.clone(), + ) + }) + } + + pub fn range_to_buffer_ranges<'a, T: ToOffset>( &'a self, range: Range, cx: &AppContext, ) -> Vec<(ModelHandle, Range)> { - let snapshot = self.snapshot(cx); + let snapshot = self.read(cx); let start = range.start.to_offset(&snapshot); let end = range.end.to_offset(&snapshot); @@ -3538,8 +3561,9 @@ mod tests { start_ix..end_ix ); - let excerpted_buffer_ranges = - multibuffer.read(cx).excerpted_buffers(start_ix..end_ix, cx); + let excerpted_buffer_ranges = multibuffer + .read(cx) + .range_to_buffer_ranges(start_ix..end_ix, cx); let excerpted_buffers_text = excerpted_buffer_ranges .into_iter() .map(|(buffer, buffer_range)| {