Fix handling of selection ranges for format selections in multibuffer (#22929)
Before this change it was using the same multibuffer point ranges in every buffer, which only worked correctly for singleton buffers. Release Notes: - Fixed handling of selection ranges when formatting selections within a multibuffer. --------- Co-authored-by: Thorsten Ball <mrnugget@gmail.com>
This commit is contained in:
parent
29aa291d28
commit
685dd77d97
7 changed files with 170 additions and 119 deletions
|
@ -129,7 +129,7 @@ use multi_buffer::{
|
|||
};
|
||||
use project::{
|
||||
buffer_store::BufferChangeSet,
|
||||
lsp_store::{FormatTarget, FormatTrigger, OpenLspBufferHandle},
|
||||
lsp_store::{FormatTrigger, LspFormatTarget, OpenLspBufferHandle},
|
||||
project_settings::{GitGutterSetting, ProjectSettings},
|
||||
CodeAction, Completion, CompletionIntent, DocumentHighlight, InlayHint, Location, LocationLink,
|
||||
LspStore, Project, ProjectItem, ProjectTransaction, TaskSourceKind,
|
||||
|
@ -986,6 +986,11 @@ impl InlayHintRefreshReason {
|
|||
}
|
||||
}
|
||||
|
||||
pub enum FormatTarget {
|
||||
Buffers,
|
||||
Ranges(Vec<Range<MultiBufferPoint>>),
|
||||
}
|
||||
|
||||
pub(crate) struct FocusedBlock {
|
||||
id: BlockId,
|
||||
focus_handle: WeakFocusHandle,
|
||||
|
@ -10237,7 +10242,7 @@ impl Editor {
|
|||
None => return None,
|
||||
};
|
||||
|
||||
Some(self.perform_format(project, FormatTrigger::Manual, FormatTarget::Buffer, cx))
|
||||
Some(self.perform_format(project, FormatTrigger::Manual, FormatTarget::Buffers, cx))
|
||||
}
|
||||
|
||||
fn format_selections(
|
||||
|
@ -10250,17 +10255,18 @@ impl Editor {
|
|||
None => return None,
|
||||
};
|
||||
|
||||
let selections = self
|
||||
let ranges = self
|
||||
.selections
|
||||
.all_adjusted(cx)
|
||||
.into_iter()
|
||||
.map(|selection| selection.range())
|
||||
.filter(|s| !s.is_empty())
|
||||
.collect_vec();
|
||||
|
||||
Some(self.perform_format(
|
||||
project,
|
||||
FormatTrigger::Manual,
|
||||
FormatTarget::Ranges(selections),
|
||||
FormatTarget::Ranges(ranges),
|
||||
cx,
|
||||
))
|
||||
}
|
||||
|
@ -10272,15 +10278,41 @@ impl Editor {
|
|||
target: FormatTarget,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> Task<Result<()>> {
|
||||
let buffer = self.buffer().clone();
|
||||
let mut buffers = buffer.read(cx).all_buffers();
|
||||
if trigger == FormatTrigger::Save {
|
||||
buffers.retain(|buffer| buffer.read(cx).is_dirty());
|
||||
}
|
||||
let buffer = self.buffer.clone();
|
||||
let (buffers, target) = match target {
|
||||
FormatTarget::Buffers => {
|
||||
let mut buffers = buffer.read(cx).all_buffers();
|
||||
if trigger == FormatTrigger::Save {
|
||||
buffers.retain(|buffer| buffer.read(cx).is_dirty());
|
||||
}
|
||||
(buffers, LspFormatTarget::Buffers)
|
||||
}
|
||||
FormatTarget::Ranges(selection_ranges) => {
|
||||
let multi_buffer = buffer.read(cx);
|
||||
let snapshot = multi_buffer.read(cx);
|
||||
let mut buffers = HashSet::default();
|
||||
let mut buffer_id_to_ranges: BTreeMap<BufferId, Vec<Range<text::Anchor>>> =
|
||||
BTreeMap::new();
|
||||
for selection_range in selection_ranges {
|
||||
for (excerpt, buffer_range) in snapshot.range_to_buffer_ranges(selection_range)
|
||||
{
|
||||
let buffer_id = excerpt.buffer_id();
|
||||
let start = excerpt.buffer().anchor_before(buffer_range.start);
|
||||
let end = excerpt.buffer().anchor_after(buffer_range.end);
|
||||
buffers.insert(multi_buffer.buffer(buffer_id).unwrap());
|
||||
buffer_id_to_ranges
|
||||
.entry(buffer_id)
|
||||
.and_modify(|buffer_ranges| buffer_ranges.push(start..end))
|
||||
.or_insert_with(|| vec![start..end]);
|
||||
}
|
||||
}
|
||||
(buffers, LspFormatTarget::Ranges(buffer_id_to_ranges))
|
||||
}
|
||||
};
|
||||
|
||||
let mut timeout = cx.background_executor().timer(FORMAT_TIMEOUT).fuse();
|
||||
let format = project.update(cx, |project, cx| {
|
||||
project.format(buffers, true, trigger, target, cx)
|
||||
project.format(buffers, target, true, trigger, cx)
|
||||
});
|
||||
|
||||
cx.spawn(|_, mut cx| async move {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue