diff options
author | Kirill Bulatov <[email protected]> | 2021-01-17 00:52:36 +0000 |
---|---|---|
committer | Kirill Bulatov <[email protected]> | 2021-01-17 00:57:38 +0000 |
commit | d1ac3293f4aa0f0807cfb9a9d5337c6a4a2632d7 (patch) | |
tree | 649b41f2e08e74e67b35475d4a6a431e36161061 /crates/completion/src/completions | |
parent | 09c11054a1b4886fdfd8f0bbb119aae0f264af1a (diff) |
Properly resolve completion edits for empty input
Diffstat (limited to 'crates/completion/src/completions')
-rw-r--r-- | crates/completion/src/completions/flyimport.rs | 73 |
1 files changed, 42 insertions, 31 deletions
diff --git a/crates/completion/src/completions/flyimport.rs b/crates/completion/src/completions/flyimport.rs index a40843669..5c04d5c41 100644 --- a/crates/completion/src/completions/flyimport.rs +++ b/crates/completion/src/completions/flyimport.rs | |||
@@ -85,7 +85,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext) | |||
85 | let user_input_lowercased = potential_import_name.to_lowercase(); | 85 | let user_input_lowercased = potential_import_name.to_lowercase(); |
86 | let import_assets = import_assets(ctx, potential_import_name)?; | 86 | let import_assets = import_assets(ctx, potential_import_name)?; |
87 | let import_scope = ImportScope::find_insert_use_container( | 87 | let import_scope = ImportScope::find_insert_use_container( |
88 | position_for_import(ctx, import_assets.import_candidate())?, | 88 | position_for_import(ctx, Some(import_assets.import_candidate()))?, |
89 | &ctx.sema, | 89 | &ctx.sema, |
90 | )?; | 90 | )?; |
91 | let mut all_mod_paths = import_assets | 91 | let mut all_mod_paths = import_assets |
@@ -122,14 +122,20 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext) | |||
122 | Some(()) | 122 | Some(()) |
123 | } | 123 | } |
124 | 124 | ||
125 | fn position_for_import<'a>( | 125 | pub(crate) fn position_for_import<'a>( |
126 | ctx: &'a CompletionContext, | 126 | ctx: &'a CompletionContext, |
127 | import_candidate: &ImportCandidate, | 127 | import_candidate: Option<&ImportCandidate>, |
128 | ) -> Option<&'a SyntaxNode> { | 128 | ) -> Option<&'a SyntaxNode> { |
129 | Some(match import_candidate { | 129 | Some(match import_candidate { |
130 | ImportCandidate::Path(_) => ctx.name_ref_syntax.as_ref()?.syntax(), | 130 | Some(ImportCandidate::Path(_)) => ctx.name_ref_syntax.as_ref()?.syntax(), |
131 | ImportCandidate::TraitAssocItem(_) => ctx.path_qual.as_ref()?.syntax(), | 131 | Some(ImportCandidate::TraitAssocItem(_)) => ctx.path_qual.as_ref()?.syntax(), |
132 | ImportCandidate::TraitMethod(_) => ctx.dot_receiver.as_ref()?.syntax(), | 132 | Some(ImportCandidate::TraitMethod(_)) => ctx.dot_receiver.as_ref()?.syntax(), |
133 | None => ctx | ||
134 | .name_ref_syntax | ||
135 | .as_ref() | ||
136 | .map(|name_ref| name_ref.syntax()) | ||
137 | .or_else(|| ctx.path_qual.as_ref().map(|path| path.syntax())) | ||
138 | .or_else(|| ctx.dot_receiver.as_ref().map(|expr| expr.syntax()))?, | ||
133 | }) | 139 | }) |
134 | } | 140 | } |
135 | 141 | ||
@@ -565,7 +571,8 @@ fn main() { | |||
565 | 571 | ||
566 | #[test] | 572 | #[test] |
567 | fn blanket_trait_impl_import() { | 573 | fn blanket_trait_impl_import() { |
568 | check( | 574 | check_edit( |
575 | "another_function", | ||
569 | r#" | 576 | r#" |
570 | //- /lib.rs crate:dep | 577 | //- /lib.rs crate:dep |
571 | pub mod test_mod { | 578 | pub mod test_mod { |
@@ -583,9 +590,13 @@ fn main() { | |||
583 | dep::test_mod::TestStruct::ano$0 | 590 | dep::test_mod::TestStruct::ano$0 |
584 | } | 591 | } |
585 | "#, | 592 | "#, |
586 | expect![[r#" | 593 | r#" |
587 | fn another_function() (dep::test_mod::TestTrait) fn another_function() | 594 | use dep::test_mod::TestTrait; |
588 | "#]], | 595 | |
596 | fn main() { | ||
597 | dep::test_mod::TestStruct::another_function()$0 | ||
598 | } | ||
599 | "#, | ||
589 | ); | 600 | ); |
590 | } | 601 | } |
591 | 602 | ||
@@ -593,28 +604,28 @@ fn main() { | |||
593 | fn zero_input_assoc_item_completion() { | 604 | fn zero_input_assoc_item_completion() { |
594 | check( | 605 | check( |
595 | r#" | 606 | r#" |
596 | //- /lib.rs crate:dep | 607 | //- /lib.rs crate:dep |
597 | pub mod test_mod { | 608 | pub mod test_mod { |
598 | pub trait TestTrait { | 609 | pub trait TestTrait { |
599 | const SPECIAL_CONST: u8; | 610 | const SPECIAL_CONST: u8; |
600 | type HumbleType; | 611 | type HumbleType; |
601 | fn weird_function(); | 612 | fn weird_function(); |
602 | fn random_method(&self); | 613 | fn random_method(&self); |
603 | } | 614 | } |
604 | pub struct TestStruct {} | 615 | pub struct TestStruct {} |
605 | impl TestTrait for TestStruct { | 616 | impl TestTrait for TestStruct { |
606 | const SPECIAL_CONST: u8 = 42; | 617 | const SPECIAL_CONST: u8 = 42; |
607 | type HumbleType = (); | 618 | type HumbleType = (); |
608 | fn weird_function() {} | 619 | fn weird_function() {} |
609 | fn random_method(&self) {} | 620 | fn random_method(&self) {} |
610 | } | 621 | } |
611 | } | 622 | } |
612 | 623 | ||
613 | //- /main.rs crate:main deps:dep | 624 | //- /main.rs crate:main deps:dep |
614 | fn main() { | 625 | fn main() { |
615 | let test_struct = dep::test_mod::TestStruct {}; | 626 | let test_struct = dep::test_mod::TestStruct {}; |
616 | test_struct.$0 | 627 | test_struct.$0 |
617 | } | 628 | } |
618 | "#, | 629 | "#, |
619 | expect![[r#" | 630 | expect![[r#" |
620 | me random_method() (dep::test_mod::TestTrait) fn random_method(&self) | 631 | me random_method() (dep::test_mod::TestTrait) fn random_method(&self) |