@@ -23,7 +23,10 @@ use syntax::{
23
23
} ;
24
24
use text_edit:: Indel ;
25
25
26
- use crate :: CompletionConfig ;
26
+ use crate :: {
27
+ context:: analysis:: { expand_and_analyze, AnalysisResult } ,
28
+ CompletionConfig ,
29
+ } ;
27
30
28
31
const COMPLETION_MARKER : & str = "intellijRulezz" ;
29
32
@@ -561,15 +564,27 @@ impl<'a> CompletionContext<'a> {
561
564
let edit = Indel :: insert ( offset, COMPLETION_MARKER . to_string ( ) ) ;
562
565
parse. reparse ( & edit) . tree ( )
563
566
} ;
564
- let fake_ident_token =
565
- file_with_fake_ident. syntax ( ) . token_at_offset ( offset) . right_biased ( ) ?;
566
567
568
+ // always pick the token to the immediate left of the cursor, as that is what we are actually
569
+ // completing on
567
570
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
+ ) ?;
569
585
570
586
// 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) ?;
573
588
574
589
let krate = scope. krate ( ) ;
575
590
let module = scope. module ( ) ;
@@ -583,7 +598,7 @@ impl<'a> CompletionContext<'a> {
583
598
584
599
let depth_from_crate_root = iter:: successors ( module. parent ( db) , |m| m. parent ( db) ) . count ( ) ;
585
600
586
- let mut ctx = CompletionContext {
601
+ let ctx = CompletionContext {
587
602
sema,
588
603
scope,
589
604
db,
@@ -593,19 +608,13 @@ impl<'a> CompletionContext<'a> {
593
608
token,
594
609
krate,
595
610
module,
596
- expected_name : None ,
597
- expected_type : None ,
598
- qualifier_ctx : Default :: default ( ) ,
611
+ expected_name,
612
+ expected_type,
613
+ qualifier_ctx,
599
614
locals,
600
615
depth_from_crate_root,
601
616
} ;
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) )
609
618
}
610
619
}
611
620
0 commit comments