Repository: rust-lang/rust-analyzer
Pull request #12490 (2022-06-08 12:04:42)

fix: complete non-std enum at the start of `use` completions
close: #12421


Patch:
diff --git a/crates/ide-completion/src/completions/use_.rs b/crates/ide-completion/src/completions/use_.rs
index f1beeb454c..28e4e47751 100644
--- a/crates/ide-completion/src/completions/use_.rs
+++ b/crates/ide-completion/src/completions/use_.rs
@@ -1,13 +1,13 @@
 //! Completion for use trees
 
 use hir::ScopeDef;
-use ide_db::FxHashSet;
+use ide_db::{FxHashSet, SymbolKind};
 use syntax::{ast, AstNode};
 
 use crate::{
     context::{CompletionContext, NameRefContext, PathCompletionCtx, PathKind, PathQualifierCtx},
     item::Builder,
-    CompletionRelevance, Completions,
+    CompletionItem, CompletionItemKind, CompletionRelevance, Completions,
 };
 
 pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext) {
@@ -101,13 +101,30 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext)
             cov_mark::hit!(use_tree_crate_roots_only);
             acc.add_crate_roots(ctx);
         }
-        // only show modules in a fresh UseTree
+        // only show modules and non-std enum in a fresh UseTree
         None => {
-            cov_mark::hit!(unqualified_path_only_modules_in_import);
+            cov_mark::hit!(unqualified_path_selected_only);
             ctx.process_all_names(&mut |name, res| {
-                if let ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res {
-                    acc.add_resolution(ctx, name, res);
-                }
+                match res {
+                    ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => {
+                        acc.add_resolution(ctx, name, res);
+                    }
+                    ScopeDef::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(e))) => {
+                        // exclude prelude enum
+                        let is_builtin =
+                            res.krate(ctx.db).map_or(false, |krate| krate.is_builtin(ctx.db));
+
+                        if !is_builtin {
+                            let item = CompletionItem::new(
+                                CompletionItemKind::SymbolKind(SymbolKind::Enum),
+                                ctx.source_range(),
+                                format!("{}::", e.name(ctx.db)),
+                            );
+                            acc.add(item.build());
+                        }
+                    }
+                    _ => {}
+                };
             });
             acc.add_nameref_keywords_with_colon(ctx);
         }


Test patch:
diff --git a/crates/ide-completion/src/tests/use_tree.rs b/crates/ide-completion/src/tests/use_tree.rs
index 70aa5e5468..711822d731 100644
--- a/crates/ide-completion/src/tests/use_tree.rs
+++ b/crates/ide-completion/src/tests/use_tree.rs
@@ -10,18 +10,23 @@ fn check(ra_fixture: &str, expect: Expect) {
 
 #[test]
 fn use_tree_start() {
-    cov_mark::check!(unqualified_path_only_modules_in_import);
+    cov_mark::check!(unqualified_path_selected_only);
     check(
         r#"
 //- /lib.rs crate:main deps:other_crate
 use f$0
 
 struct Foo;
+enum FooBar {
+    Foo,
+    Bar
+}
 mod foo {}
 //- /other_crate/lib.rs crate:other_crate
 // nothing here
 "#,
         expect![[r#"
+            en FooBar::
             md foo
             md other_crate
             kw crate::