Resolve completion items

This commit is contained in:
Kirill Bulatov 2024-09-23 05:29:30 +03:00
parent cc2686c1a9
commit ecae5a8b33
3 changed files with 42 additions and 11 deletions

View file

@ -10,9 +10,9 @@ use std::{
use anyhow::Context; use anyhow::Context;
use ide::{ use ide::{
AnnotationConfig, AssistKind, AssistResolveStrategy, Cancellable, FilePosition, FileRange, AnnotationConfig, AssistKind, AssistResolveStrategy, Cancellable, CompletionFieldsToResolve,
HoverAction, HoverGotoTypeData, InlayFieldsToResolve, Query, RangeInfo, ReferenceCategory, FilePosition, FileRange, HoverAction, HoverGotoTypeData, InlayFieldsToResolve, Query,
Runnable, RunnableKind, SingleResolve, SourceChange, TextEdit, RangeInfo, ReferenceCategory, Runnable, RunnableKind, SingleResolve, SourceChange, TextEdit,
}; };
use ide_db::SymbolKind; use ide_db::SymbolKind;
use itertools::Itertools; use itertools::Itertools;
@ -1056,12 +1056,43 @@ pub(crate) fn handle_completion_resolve(
}; };
let source_root = snap.analysis.source_root_id(file_id)?; 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() { if !resolve_data.imports.is_empty() {
let additional_edits = snap let additional_edits = snap
.analysis .analysis
.resolve_completion_edits( .resolve_completion_edits(
&snap.config.completion(Some(source_root)), &forced_resolve_completions_config,
FilePosition { file_id, offset }, position,
resolve_data resolve_data
.imports .imports
.into_iter() .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))) .flat_map(|edit| edit.into_iter().map(|indel| to_proto::text_edit(&line_index, indel)))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
if !all_edits_are_disjoint(&original_completion, &additional_edits) { if !all_edits_are_disjoint(&resolved_completion, &additional_edits) {
return Err(LspError::new( return Err(LspError::new(
ErrorCode::InternalError as i32, ErrorCode::InternalError as i32,
"Import edit overlaps with the original completion edits, this is not LSP-compliant" "Import edit overlaps with the original completion edits, this is not LSP-compliant"
@ -1080,15 +1111,15 @@ pub(crate) fn handle_completion_resolve(
.into()); .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) original_additional_edits.extend(additional_edits)
} else { } 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( pub(crate) fn handle_folding_range(

View file

@ -825,7 +825,7 @@ pub struct CompletionResolveData {
pub position: lsp_types::TextDocumentPositionParams, pub position: lsp_types::TextDocumentPositionParams,
pub imports: Vec<CompletionImport>, pub imports: Vec<CompletionImport>,
pub version: Option<i32>, pub version: Option<i32>,
pub completion_trigger_character: Option<char>, pub trigger_character: Option<char>,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]

View file

@ -396,7 +396,7 @@ fn completion_item(
position: tdpp.clone(), position: tdpp.clone(),
imports, imports,
version, version,
completion_trigger_character, trigger_character: completion_trigger_character,
}; };
lsp_item.data = Some(to_value(data).unwrap()); lsp_item.data = Some(to_value(data).unwrap());
} }