diff --git a/crates/rust-analyzer/src/handlers/request.rs b/crates/rust-analyzer/src/handlers/request.rs index 7d5a3946db16..80d8c53801f1 100644 --- a/crates/rust-analyzer/src/handlers/request.rs +++ b/crates/rust-analyzer/src/handlers/request.rs @@ -10,9 +10,9 @@ use std::{ use anyhow::Context; use ide::{ - AnnotationConfig, AssistKind, AssistResolveStrategy, Cancellable, FilePosition, FileRange, - HoverAction, HoverGotoTypeData, InlayFieldsToResolve, Query, RangeInfo, ReferenceCategory, - Runnable, RunnableKind, SingleResolve, SourceChange, TextEdit, + AnnotationConfig, AssistKind, AssistResolveStrategy, Cancellable, CompletionFieldsToResolve, + FilePosition, FileRange, HoverAction, HoverGotoTypeData, InlayFieldsToResolve, Query, + RangeInfo, ReferenceCategory, Runnable, RunnableKind, SingleResolve, SourceChange, TextEdit, }; use ide_db::SymbolKind; use itertools::Itertools; @@ -1056,12 +1056,43 @@ pub(crate) fn handle_completion_resolve( }; let source_root = snap.analysis.source_root_id(file_id)?; + let mut forced_resolve_completions_config = snap.config.completion(Some(source_root)); + forced_resolve_completions_config.fields_to_resolve = CompletionFieldsToResolve::empty(); + + let position = FilePosition { file_id, offset }; + let Some(unresolved_completions) = snap.analysis.completions( + &&forced_resolve_completions_config, + position, + resolve_data.trigger_character, + )? + else { + return Ok(original_completion); + }; + let resolved_completions = to_proto::completion_items( + &snap.config, + &forced_resolve_completions_config.fields_to_resolve, + &line_index, + snap.file_version(position.file_id), + resolve_data.position, + resolve_data.trigger_character, + unresolved_completions, + ); + let Some(mut resolved_completion) = resolved_completions.into_iter().find(|completion| { + completion.label == original_completion.label + && completion.kind == original_completion.kind + && completion.deprecated == original_completion.deprecated + && completion.preselect == original_completion.preselect + && completion.sort_text == original_completion.sort_text + }) else { + return Ok(original_completion); + }; + if !resolve_data.imports.is_empty() { let additional_edits = snap .analysis .resolve_completion_edits( - &snap.config.completion(Some(source_root)), - FilePosition { file_id, offset }, + &forced_resolve_completions_config, + position, resolve_data .imports .into_iter() @@ -1071,7 +1102,7 @@ pub(crate) fn handle_completion_resolve( .flat_map(|edit| edit.into_iter().map(|indel| to_proto::text_edit(&line_index, indel))) .collect::>(); - if !all_edits_are_disjoint(&original_completion, &additional_edits) { + if !all_edits_are_disjoint(&resolved_completion, &additional_edits) { return Err(LspError::new( ErrorCode::InternalError as i32, "Import edit overlaps with the original completion edits, this is not LSP-compliant" @@ -1080,15 +1111,15 @@ pub(crate) fn handle_completion_resolve( .into()); } - if let Some(original_additional_edits) = original_completion.additional_text_edits.as_mut() + if let Some(original_additional_edits) = resolved_completion.additional_text_edits.as_mut() { original_additional_edits.extend(additional_edits) } else { - original_completion.additional_text_edits = Some(additional_edits); + resolved_completion.additional_text_edits = Some(additional_edits); } } - Ok(original_completion) + Ok(resolved_completion) } pub(crate) fn handle_folding_range( diff --git a/crates/rust-analyzer/src/lsp/ext.rs b/crates/rust-analyzer/src/lsp/ext.rs index 990fadc6719c..8039f0644eec 100644 --- a/crates/rust-analyzer/src/lsp/ext.rs +++ b/crates/rust-analyzer/src/lsp/ext.rs @@ -825,7 +825,7 @@ pub struct CompletionResolveData { pub position: lsp_types::TextDocumentPositionParams, pub imports: Vec, pub version: Option, - pub completion_trigger_character: Option, + pub trigger_character: Option, } #[derive(Debug, Serialize, Deserialize)] diff --git a/crates/rust-analyzer/src/lsp/to_proto.rs b/crates/rust-analyzer/src/lsp/to_proto.rs index c7f4b22a7719..c6a57c6114ca 100644 --- a/crates/rust-analyzer/src/lsp/to_proto.rs +++ b/crates/rust-analyzer/src/lsp/to_proto.rs @@ -396,7 +396,7 @@ fn completion_item( position: tdpp.clone(), imports, version, - completion_trigger_character, + trigger_character: completion_trigger_character, }; lsp_item.data = Some(to_value(data).unwrap()); }