Skip to content

Commit

Permalink
Resolve completion items
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeoneToIgnore committed Sep 23, 2024
1 parent ba1c914 commit 950bb83
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
49 changes: 40 additions & 9 deletions crates/rust-analyzer/src/handlers/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -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"
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion crates/rust-analyzer/src/lsp/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion crates/rust-analyzer/src/lsp/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit 950bb83

Please sign in to comment.