mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 21:54:42 +00:00
Resolve completion items
This commit is contained in:
parent
ba1c9146fc
commit
950bb83f81
3 changed files with 42 additions and 11 deletions
|
@ -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::<Vec<_>>();
|
||||
|
||||
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(
|
||||
|
|
|
@ -825,7 +825,7 @@ pub struct CompletionResolveData {
|
|||
pub position: lsp_types::TextDocumentPositionParams,
|
||||
pub imports: Vec<CompletionImport>,
|
||||
pub version: Option<i32>,
|
||||
pub completion_trigger_character: Option<char>,
|
||||
pub trigger_character: Option<char>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue