Auto merge of #12490 - yue4u:fix/show-enum-in-fresh-use-tree, r=Veykril

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

close: #12421
This commit is contained in:
bors 2022-06-10 20:33:17 +00:00
commit 9ef01d0895
2 changed files with 30 additions and 8 deletions

View file

@ -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);
}

View file

@ -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::