This commit is contained in:
Lukas Wirth 2022-02-02 18:18:08 +01:00
parent 9f5ee155c1
commit 7619c2afea
5 changed files with 25 additions and 37 deletions

View File

@ -99,6 +99,19 @@ pub(crate) fn add_keyword(&mut self, ctx: &CompletionContext, keyword: &'static
item.add_to(self);
}
pub(crate) fn add_nameref_keywords(&mut self, ctx: &CompletionContext) {
["self::", "super::", "crate::"].into_iter().for_each(|kw| self.add_keyword(ctx, kw));
}
pub(crate) fn add_crate_roots(&mut self, ctx: &CompletionContext) {
ctx.process_all_names(&mut |name, res| match res {
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => {
self.add_resolution(ctx, name, res);
}
_ => (),
});
}
pub(crate) fn add_resolution(
&mut self,
ctx: &CompletionContext,

View File

@ -2,7 +2,6 @@
//!
//! This module uses a bit of static metadata to provide completions for builtin-in attributes and lints.
use hir::ScopeDef;
use ide_db::{
helpers::{
generated_lints::{
@ -103,14 +102,7 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext)
return;
}
// fresh use tree with leading colon2, only show crate roots
None if is_absolute_path => {
ctx.process_all_names(&mut |name, res| match res {
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => {
acc.add_resolution(ctx, name, res);
}
_ => (),
});
}
None if is_absolute_path => acc.add_crate_roots(ctx),
// only show modules in a fresh UseTree
None => {
ctx.process_all_names(&mut |name, def| {
@ -118,7 +110,7 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext)
acc.add_resolution(ctx, name, def);
}
});
["self::", "super::", "crate::"].into_iter().for_each(|kw| acc.add_keyword(ctx, kw));
acc.add_nameref_keywords(ctx);
}
}

View File

@ -141,7 +141,6 @@ fn pattern_path_completion(
_ => return,
};
// Note associated consts cannot be referenced in patterns
if let Some(hir::Adt::Enum(e)) = ty.as_adt() {
e.variants(ctx.db)
.into_iter()
@ -157,9 +156,9 @@ fn pattern_path_completion(
ctx.module,
None,
|_ty, item| {
// We might iterate candidates of a trait multiple times here, so deduplicate
// them.
// Note associated consts cannot be referenced in patterns
if let AssocItem::TypeAlias(ta) = item {
// We might iterate candidates of a trait multiple times here, so deduplicate them.
if seen.insert(item) {
acc.add_type_alias(ctx, ta);
}
@ -173,18 +172,7 @@ fn pattern_path_completion(
}
}
// qualifier can only be none here if we are in a TuplePat or RecordPat in which case special characters have to follow the path
// so executing the rest of this completion doesn't make sense
// fresh use tree with leading colon2, only show crate roots
None if *is_absolute_path => {
cov_mark::hit!(use_tree_crate_roots_only);
ctx.process_all_names(&mut |name, res| match res {
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => {
acc.add_resolution(ctx, name, res);
}
_ => (),
});
}
// only show modules in a fresh UseTree
None if *is_absolute_path => acc.add_crate_roots(ctx),
None => {
cov_mark::hit!(unqualified_path_only_modules_in_import);
ctx.process_all_names(&mut |name, res| {
@ -192,7 +180,7 @@ fn pattern_path_completion(
acc.add_resolution(ctx, name, res);
}
});
["self::", "super::", "crate::"].into_iter().for_each(|kw| acc.add_keyword(ctx, kw));
acc.add_nameref_keywords(ctx);
}
}
}

View File

@ -79,12 +79,7 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext)
// fresh use tree with leading colon2, only show crate roots
None if is_absolute_path => {
cov_mark::hit!(use_tree_crate_roots_only);
ctx.process_all_names(&mut |name, res| match res {
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => {
acc.add_resolution(ctx, name, res);
}
_ => (),
});
acc.add_crate_roots(ctx);
}
// only show modules in a fresh UseTree
None => {
@ -94,7 +89,7 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext)
acc.add_resolution(ctx, name, res);
}
});
["self::", "super::", "crate::"].into_iter().for_each(|kw| acc.add_keyword(ctx, kw));
acc.add_nameref_keywords(ctx);
}
}
}

View File

@ -72,12 +72,12 @@ pub(crate) struct PathCompletionCtx {
#[derive(Debug)]
pub(crate) struct PathQualifierCtx {
pub path: ast::Path,
pub resolution: Option<PathResolution>,
pub(crate) path: ast::Path,
pub(crate) resolution: Option<PathResolution>,
/// Whether this path consists solely of `super` segments
pub is_super_chain: bool,
pub(crate) is_super_chain: bool,
/// Whether the qualifier comes from a use tree parent or not
pub use_tree_parent: bool,
pub(crate) use_tree_parent: bool,
}
#[derive(Debug)]