aboutsummaryrefslogtreecommitdiff
path: root/crates/completion/src/completions/unqualified_path.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-11-17 17:50:08 +0000
committerGitHub <[email protected]>2020-11-17 17:50:08 +0000
commit156f7d6963fb6b47570bfa457fdf51733a182054 (patch)
tree1070543fd0be92d3ab720bc297962a5dad5e64e8 /crates/completion/src/completions/unqualified_path.rs
parentf4b4f17662a7a1c80743b3108bb0da28b0fe47e5 (diff)
parent16f0b2fdde2fba8f45cc18a8de11b45751c0f923 (diff)
Merge #6553
6553: Auto imports in completion r=matklad a=SomeoneToIgnore ![completion](https://user-images.githubusercontent.com/2690773/99155339-ae4fb380-26bf-11eb-805a-655b1706ce70.gif) Closes https://github.com/rust-analyzer/rust-analyzer/issues/1062 but does not handle the completion order, since it's a separate task for https://github.com/rust-analyzer/rust-analyzer/issues/4922 , https://github.com/rust-analyzer/rust-analyzer/issues/4922 and maybe something else. 2 quirks in the current implementation: * traits are not auto imported during method completion If I understand the current situation right, we cannot search for traits by a **part** of a method name, we need a full name with correct case to get a trait for it. * VSCode (?) autocompletion is not as rigid as in Intellij Rust as you can notice on the animation. Intellij is able to refresh the completions on every new symbol added, yet VS Code does not query the completions on every symbol for me. With a few debug prints placed in RA, I've observed the following behaviour: after the first set of completion suggestions is received, next symbol input does not trigger a server request, if the completions contain this symbol. When more symbols added, the existing completion suggestions are filtered out until none are left and only then, on the next symbol it queries for completions. It seems like the only alternative to get an updated set of results is to manually retrigger it with Esc and Ctrl + Space. Despite the eerie latter bullet, the completion seems to work pretty fine and fast nontheless, but if you have any ideas on how to make it more smooth, I'll gladly try it out. Co-authored-by: Kirill Bulatov <[email protected]>
Diffstat (limited to 'crates/completion/src/completions/unqualified_path.rs')
-rw-r--r--crates/completion/src/completions/unqualified_path.rs130
1 files changed, 129 insertions, 1 deletions
diff --git a/crates/completion/src/completions/unqualified_path.rs b/crates/completion/src/completions/unqualified_path.rs
index 7df58e1da..86c143b63 100644
--- a/crates/completion/src/completions/unqualified_path.rs
+++ b/crates/completion/src/completions/unqualified_path.rs
@@ -1,10 +1,16 @@
1//! Completion of names from the current scope, e.g. locals and imported items. 1//! Completion of names from the current scope, e.g. locals and imported items.
2 2
3use assists::utils::ImportScope;
4use either::Either;
3use hir::{Adt, ModuleDef, ScopeDef, Type}; 5use hir::{Adt, ModuleDef, ScopeDef, Type};
6use ide_db::imports_locator;
4use syntax::AstNode; 7use syntax::AstNode;
5use test_utils::mark; 8use test_utils::mark;
6 9
7use crate::{CompletionContext, Completions}; 10use crate::{
11 render::{render_resolution_with_import, RenderContext},
12 CompletionContext, Completions,
13};
8 14
9pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) { 15pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) {
10 if !(ctx.is_trivial_path || ctx.is_pat_binding_or_const) { 16 if !(ctx.is_trivial_path || ctx.is_pat_binding_or_const) {
@@ -37,6 +43,8 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
37 } 43 }
38 acc.add_resolution(ctx, name.to_string(), &res) 44 acc.add_resolution(ctx, name.to_string(), &res)
39 }); 45 });
46
47 fuzzy_completion(acc, ctx).unwrap_or_default()
40} 48}
41 49
42fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &Type) { 50fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &Type) {
@@ -63,6 +71,45 @@ fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &T
63 } 71 }
64} 72}
65 73
74fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
75 let _p = profile::span("fuzzy_completion");
76 let current_module = ctx.scope.module()?;
77 let anchor = ctx.name_ref_syntax.as_ref()?;
78 let import_scope = ImportScope::find_insert_use_container(anchor.syntax(), &ctx.sema)?;
79
80 let potential_import_name = ctx.token.to_string();
81
82 let possible_imports =
83 imports_locator::find_similar_imports(&ctx.sema, ctx.krate?, &potential_import_name, 400)
84 .filter_map(|import_candidate| match import_candidate {
85 // when completing outside the use declaration, modules are pretty useless
86 // and tend to bloat the completion suggestions a lot
87 Either::Left(ModuleDef::Module(_)) => None,
88 Either::Left(module_def) => Some((
89 current_module.find_use_path(ctx.db, module_def)?,
90 ScopeDef::ModuleDef(module_def),
91 )),
92 Either::Right(macro_def) => Some((
93 current_module.find_use_path(ctx.db, macro_def)?,
94 ScopeDef::MacroDef(macro_def),
95 )),
96 })
97 .filter(|(mod_path, _)| mod_path.len() > 1)
98 .filter_map(|(import_path, definition)| {
99 render_resolution_with_import(
100 RenderContext::new(ctx),
101 import_path.clone(),
102 import_scope.clone(),
103 ctx.config.merge,
104 &definition,
105 )
106 })
107 .take(20);
108
109 acc.add_all(possible_imports);
110 Some(())
111}
112
66#[cfg(test)] 113#[cfg(test)]
67mod tests { 114mod tests {
68 use expect_test::{expect, Expect}; 115 use expect_test::{expect, Expect};
@@ -676,4 +723,85 @@ impl My<|>
676 "#]], 723 "#]],
677 ) 724 )
678 } 725 }
726
727 #[test]
728 fn function_fuzzy_completion() {
729 check_edit(
730 "stdin",
731 r#"
732//- /lib.rs crate:dep
733pub mod io {
734 pub fn stdin() {}
735};
736
737//- /main.rs crate:main deps:dep
738fn main() {
739 stdi<|>
740}
741"#,
742 r#"
743use dep::io::stdin;
744
745fn main() {
746 stdin()$0
747}
748"#,
749 );
750 }
751
752 #[test]
753 fn macro_fuzzy_completion() {
754 check_edit(
755 "macro_with_curlies!",
756 r#"
757//- /lib.rs crate:dep
758/// Please call me as macro_with_curlies! {}
759#[macro_export]
760macro_rules! macro_with_curlies {
761 () => {}
762}
763
764//- /main.rs crate:main deps:dep
765fn main() {
766 curli<|>
767}
768"#,
769 r#"
770use dep::macro_with_curlies;
771
772fn main() {
773 macro_with_curlies! {$0}
774}
775"#,
776 );
777 }
778
779 #[test]
780 fn struct_fuzzy_completion() {
781 check_edit(
782 "ThirdStruct",
783 r#"
784//- /lib.rs crate:dep
785pub struct FirstStruct;
786pub mod some_module {
787 pub struct SecondStruct;
788 pub struct ThirdStruct;
789}
790
791//- /main.rs crate:main deps:dep
792use dep::{FirstStruct, some_module::SecondStruct};
793
794fn main() {
795 this<|>
796}
797"#,
798 r#"
799use dep::{FirstStruct, some_module::{SecondStruct, ThirdStruct}};
800
801fn main() {
802 ThirdStruct
803}
804"#,
805 );
806 }
679} 807}