Skip to content

Commit 19cfa58

Browse files
Simplify
1 parent 6badf70 commit 19cfa58

File tree

4 files changed

+35
-36
lines changed

4 files changed

+35
-36
lines changed

crates/completion/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub fn resolve_completion_edits(
141141
position: FilePosition,
142142
full_import_path: &str,
143143
imported_name: &str,
144-
) -> Option<TextEdit> {
144+
) -> Option<Vec<TextEdit>> {
145145
let ctx = CompletionContext::new(db, position, config)?;
146146
let anchor = ctx.name_ref_syntax.as_ref()?;
147147
let import_scope = ImportScope::find_insert_use_container(anchor.syntax(), &ctx.sema)?;
@@ -156,7 +156,9 @@ pub fn resolve_completion_edits(
156156
})
157157
.find(|mod_path| mod_path.to_string() == full_import_path)?;
158158

159-
ImportEdit { import_path, import_scope, merge_behaviour: config.merge }.to_text_edit()
159+
ImportEdit { import_path, import_scope, merge_behaviour: config.merge }
160+
.to_text_edit()
161+
.map(|edit| vec![edit])
160162
}
161163

162164
#[cfg(test)]

crates/ide/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,6 @@ impl Analysis {
487487
imported_name,
488488
)
489489
})?
490-
.map(|edit| vec![edit])
491490
.unwrap_or_default())
492491
}
493492

crates/rust-analyzer/src/handlers.rs

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ use std::{
88
};
99

1010
use ide::{
11-
CompletionConfig, CompletionResolveCapability, FileId, FilePosition, FileRange, HoverAction,
12-
HoverGotoTypeData, NavigationTarget, Query, RangeInfo, Runnable, RunnableKind, SearchScope,
13-
TextEdit,
11+
CompletionResolveCapability, FileId, FilePosition, FileRange, HoverAction, HoverGotoTypeData,
12+
NavigationTarget, Query, RangeInfo, Runnable, RunnableKind, SearchScope, TextEdit,
1413
};
1514
use itertools::Itertools;
1615
use lsp_server::ErrorCode;
@@ -578,14 +577,11 @@ pub(crate) fn handle_completion(
578577
let mut new_completion_items =
579578
to_proto::completion_item(&line_index, line_endings, item.clone());
580579

581-
for new_item in &mut new_completion_items {
582-
let _ = fill_resolve_data(
583-
&mut new_item.data,
584-
&item,
585-
&snap.config.completion,
586-
&text_document_position,
587-
)
588-
.take();
580+
if snap.config.completion.resolve_additional_edits_lazily() {
581+
for new_item in &mut new_completion_items {
582+
let _ = fill_resolve_data(&mut new_item.data, &item, &text_document_position)
583+
.take();
584+
}
589585
}
590586

591587
new_completion_items
@@ -600,12 +596,12 @@ pub(crate) fn handle_completion_resolve(
600596
snap: GlobalStateSnapshot,
601597
mut original_completion: CompletionItem,
602598
) -> Result<CompletionItem> {
603-
let _p = profile::span("handle_resolve_completion");
599+
let _p = profile::span("handle_completion_resolve");
604600

605601
if !all_edits_are_disjoint(&original_completion, &[]) {
606602
return Err(LspError::new(
607603
ErrorCode::InvalidParams as i32,
608-
"Received a completion with disjoint edits".into(),
604+
"Received a completion with overlapping edits, this is not LSP-compliant".into(),
609605
)
610606
.into());
611607
}
@@ -635,7 +631,7 @@ pub(crate) fn handle_completion_resolve(
635631
let line_endings = snap.file_line_endings(file_id);
636632
let offset = from_proto::offset(&line_index, resolve_data.position.position);
637633

638-
let mut additional_edits = snap
634+
let additional_edits = snap
639635
.analysis
640636
.resolve_completion_edits(
641637
&snap.config.completion,
@@ -652,13 +648,14 @@ pub(crate) fn handle_completion_resolve(
652648
if !all_edits_are_disjoint(&original_completion, &additional_edits) {
653649
return Err(LspError::new(
654650
ErrorCode::InternalError as i32,
655-
"Import edit is not disjoint with the original completion edits".into(),
651+
"Import edit overlaps with the original completion edits, this is not LSP-compliant"
652+
.into(),
656653
)
657654
.into());
658655
}
659656

660657
if let Some(original_additional_edits) = original_completion.additional_text_edits.as_mut() {
661-
original_additional_edits.extend(additional_edits.drain(..))
658+
original_additional_edits.extend(additional_edits.into_iter())
662659
} else {
663660
original_completion.additional_text_edits = Some(additional_edits);
664661
}
@@ -1634,22 +1631,19 @@ struct CompletionResolveData {
16341631
fn fill_resolve_data(
16351632
resolve_data: &mut Option<serde_json::Value>,
16361633
item: &ide::CompletionItem,
1637-
completion_config: &CompletionConfig,
16381634
position: &TextDocumentPositionParams,
16391635
) -> Option<()> {
1640-
if completion_config.resolve_additional_edits_lazily() {
1641-
let import_edit = item.import_to_add()?;
1642-
let full_import_path = import_edit.import_path.to_string();
1643-
let imported_name = import_edit.import_path.segments.clone().pop()?.to_string();
1644-
1645-
*resolve_data = Some(
1646-
to_value(CompletionResolveData {
1647-
position: position.to_owned(),
1648-
full_import_path,
1649-
imported_name,
1650-
})
1651-
.unwrap(),
1652-
)
1653-
}
1636+
let import_edit = item.import_to_add()?;
1637+
let full_import_path = import_edit.import_path.to_string();
1638+
let imported_name = import_edit.import_path.segments.clone().pop()?.to_string();
1639+
1640+
*resolve_data = Some(
1641+
to_value(CompletionResolveData {
1642+
position: position.to_owned(),
1643+
full_import_path,
1644+
imported_name,
1645+
})
1646+
.unwrap(),
1647+
);
16541648
Some(())
16551649
}

crates/rust-analyzer/src/lsp_utils.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ pub(crate) fn apply_document_changes(
129129
}
130130
}
131131

132-
/// Checks that the edits inside the completion and the additional edits are disjoint.
132+
/// Checks that the edits inside the completion and the additional edits do not overlap.
133+
/// LSP explicitly forbits the additional edits to overlap both with the main edit and themselves.
133134
pub(crate) fn all_edits_are_disjoint(
134135
completion: &lsp_types::CompletionItem,
135136
additional_edits: &[lsp_types::TextEdit],
@@ -150,7 +151,10 @@ pub(crate) fn all_edits_are_disjoint(
150151
};
151152
edit_ranges.extend(additional_edits.iter().map(|edit| edit.range));
152153
edit_ranges.sort_by_key(|range| (range.start, range.end));
153-
edit_ranges.iter().zip(edit_ranges.iter().skip(1)).all(|(l, r)| l.end <= r.start)
154+
edit_ranges
155+
.iter()
156+
.zip(edit_ranges.iter().skip(1))
157+
.all(|(previous, next)| previous.end <= next.start)
154158
}
155159

156160
#[cfg(test)]

0 commit comments

Comments
 (0)