Skip to content

Commit d235373

Browse files
committed
Auto merge of rust-lang#13386 - Veykril:completions-ref, r=Veykril
Refactor completions expansion Depends on rust-lang/rust-analyzer#13384 Diff is unfortunately massive as I changed the functions in the analysis module from associated ones to standalone (unfortunately without an extra commit)
2 parents deddad3 + f3ae5e5 commit d235373

File tree

2 files changed

+1011
-971
lines changed

2 files changed

+1011
-971
lines changed

crates/ide-completion/src/context.rs

+26-17
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ use syntax::{
2323
};
2424
use text_edit::Indel;
2525

26-
use crate::CompletionConfig;
26+
use crate::{
27+
context::analysis::{expand_and_analyze, AnalysisResult},
28+
CompletionConfig,
29+
};
2730

2831
const COMPLETION_MARKER: &str = "intellijRulezz";
2932

@@ -561,15 +564,27 @@ impl<'a> CompletionContext<'a> {
561564
let edit = Indel::insert(offset, COMPLETION_MARKER.to_string());
562565
parse.reparse(&edit).tree()
563566
};
564-
let fake_ident_token =
565-
file_with_fake_ident.syntax().token_at_offset(offset).right_biased()?;
566567

568+
// always pick the token to the immediate left of the cursor, as that is what we are actually
569+
// completing on
567570
let original_token = original_file.syntax().token_at_offset(offset).left_biased()?;
568-
let token = sema.descend_into_macros_single(original_token.clone());
571+
572+
let AnalysisResult {
573+
analysis,
574+
expected: (expected_type, expected_name),
575+
qualifier_ctx,
576+
token,
577+
offset,
578+
} = expand_and_analyze(
579+
&sema,
580+
original_file.syntax().clone(),
581+
file_with_fake_ident.syntax().clone(),
582+
offset,
583+
&original_token,
584+
)?;
569585

570586
// adjust for macro input, this still fails if there is no token written yet
571-
let scope_offset = if original_token == token { offset } else { token.text_range().end() };
572-
let scope = sema.scope_at_offset(&token.parent()?, scope_offset)?;
587+
let scope = sema.scope_at_offset(&token.parent()?, offset)?;
573588

574589
let krate = scope.krate();
575590
let module = scope.module();
@@ -583,7 +598,7 @@ impl<'a> CompletionContext<'a> {
583598

584599
let depth_from_crate_root = iter::successors(module.parent(db), |m| m.parent(db)).count();
585600

586-
let mut ctx = CompletionContext {
601+
let ctx = CompletionContext {
587602
sema,
588603
scope,
589604
db,
@@ -593,19 +608,13 @@ impl<'a> CompletionContext<'a> {
593608
token,
594609
krate,
595610
module,
596-
expected_name: None,
597-
expected_type: None,
598-
qualifier_ctx: Default::default(),
611+
expected_name,
612+
expected_type,
613+
qualifier_ctx,
599614
locals,
600615
depth_from_crate_root,
601616
};
602-
let ident_ctx = ctx.expand_and_analyze(
603-
original_file.syntax().clone(),
604-
file_with_fake_ident.syntax().clone(),
605-
offset,
606-
fake_ident_token,
607-
)?;
608-
Some((ctx, ident_ctx))
617+
Some((ctx, analysis))
609618
}
610619
}
611620

0 commit comments

Comments
 (0)