@@ -4,7 +4,7 @@ use hir::ScopeDef;
44use test_utils:: tested_by;
55
66use crate :: completion:: { CompletionContext , Completions } ;
7- use hir:: { Adt , ModuleDef } ;
7+ use hir:: { Adt , ModuleDef , Type } ;
88use ra_syntax:: AstNode ;
99
1010pub ( super ) fn complete_unqualified_path ( acc : & mut Completions , ctx : & CompletionContext ) {
@@ -15,7 +15,9 @@ pub(super) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
1515 return ;
1616 }
1717
18- complete_enum_variants ( acc, ctx) ;
18+ if let Some ( ty) = & ctx. expected_type {
19+ complete_enum_variants ( acc, ctx, ty) ;
20+ }
1921
2022 if ctx. is_pat_binding_or_const {
2123 return ;
@@ -34,26 +36,24 @@ pub(super) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
3436 } ) ;
3537}
3638
37- fn complete_enum_variants ( acc : & mut Completions , ctx : & CompletionContext ) {
38- if let Some ( ty) = ctx. expected_type_of ( & ctx. token . parent ( ) ) {
39- if let Some ( Adt :: Enum ( enum_data) ) = ty. as_adt ( ) {
40- let variants = enum_data. variants ( ctx. db ) ;
41-
42- let module = if let Some ( module) = ctx. scope ( ) . module ( ) {
43- // Compute path from the completion site if available.
44- module
45- } else {
46- // Otherwise fall back to the enum's definition site.
47- enum_data. module ( ctx. db )
48- } ;
49-
50- for variant in variants {
51- if let Some ( path) = module. find_use_path ( ctx. db , ModuleDef :: from ( variant) ) {
52- // Variants with trivial paths are already added by the existing completion logic,
53- // so we should avoid adding these twice
54- if path. segments . len ( ) > 1 {
55- acc. add_enum_variant ( ctx, variant, Some ( path. to_string ( ) ) ) ;
56- }
39+ fn complete_enum_variants ( acc : & mut Completions , ctx : & CompletionContext , ty : & Type ) {
40+ if let Some ( Adt :: Enum ( enum_data) ) = ty. as_adt ( ) {
41+ let variants = enum_data. variants ( ctx. db ) ;
42+
43+ let module = if let Some ( module) = ctx. scope ( ) . module ( ) {
44+ // Compute path from the completion site if available.
45+ module
46+ } else {
47+ // Otherwise fall back to the enum's definition site.
48+ enum_data. module ( ctx. db )
49+ } ;
50+
51+ for variant in variants {
52+ if let Some ( path) = module. find_use_path ( ctx. db , ModuleDef :: from ( variant) ) {
53+ // Variants with trivial paths are already added by the existing completion logic,
54+ // so we should avoid adding these twice
55+ if path. segments . len ( ) > 1 {
56+ acc. add_enum_variant ( ctx, variant, Some ( path. to_string ( ) ) ) ;
5757 }
5858 }
5959 }
0 commit comments