Unnamed repository; edit this file 'description' to name the repository.
Simplify
Lukas Wirth 2022-04-23
parent f8c32df · commit ea45e54
-rw-r--r--crates/ide_completion/src/completions/lifetime.rs4
-rw-r--r--crates/ide_completion/src/completions/use_.rs27
-rw-r--r--crates/ide_completion/src/completions/vis.rs4
-rw-r--r--crates/ide_completion/src/context.rs9
-rw-r--r--crates/syntax/src/ast/node_ext.rs6
5 files changed, 29 insertions, 21 deletions
diff --git a/crates/ide_completion/src/completions/lifetime.rs b/crates/ide_completion/src/completions/lifetime.rs
index 4fffd1aa0c..66f8723912 100644
--- a/crates/ide_completion/src/completions/lifetime.rs
+++ b/crates/ide_completion/src/completions/lifetime.rs
@@ -22,8 +22,8 @@ pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext)
Some(LifetimeContext::LifetimeParam { is_decl: false, param }) => Some(param),
_ => return,
};
- let param_lifetime = match (&ctx.name_syntax, lp.and_then(|lp| lp.lifetime())) {
- (Some(ast::NameLike::Lifetime(lt)), Some(lp)) if lp == lt.clone() => return,
+ let param_lifetime = match (ctx.lifetime(), lp.and_then(|lp| lp.lifetime())) {
+ (Some(lt), Some(lp)) if lp == lt.clone() => return,
(Some(_), Some(lp)) => Some(lp),
_ => None,
};
diff --git a/crates/ide_completion/src/completions/use_.rs b/crates/ide_completion/src/completions/use_.rs
index 94df46efb0..07f14c8735 100644
--- a/crates/ide_completion/src/completions/use_.rs
+++ b/crates/ide_completion/src/completions/use_.rs
@@ -11,12 +11,9 @@ use crate::{
};
pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext) {
- let (is_absolute_path, qualifier) = match ctx.path_context {
+ let (&is_absolute_path, qualifier) = match &ctx.path_context {
Some(PathCompletionCtx {
- kind: Some(PathKind::Use),
- is_absolute_path,
- ref qualifier,
- ..
+ kind: Some(PathKind::Use), is_absolute_path, qualifier, ..
}) => (is_absolute_path, qualifier),
_ => return,
};
@@ -45,13 +42,9 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext)
if let Some(list) = ctx.token.ancestors().find_map(ast::UseTreeList::cast) {
let use_tree = list.parent_use_tree();
if use_tree.path().as_ref() == Some(path) {
- for tree in list.use_trees() {
- if tree.is_simple_path() {
- if let Some(name) =
- tree.path().and_then(|path| path.as_single_name_ref())
- {
- already_imported_names.insert(name.to_string());
- }
+ for tree in list.use_trees().filter(|tree| tree.is_simple_path()) {
+ if let Some(name) = tree.path().and_then(|path| path.as_single_name_ref()) {
+ already_imported_names.insert(name.to_string());
}
}
}
@@ -62,14 +55,14 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext)
let module_scope = module.scope(ctx.db, Some(ctx.module));
let unknown_is_current = |name: &hir::Name| {
matches!(
- ctx.name_syntax.as_ref(),
- Some(ast::NameLike::NameRef(name_ref))
- if name_ref.syntax().text() == name.to_smol_str().as_str()
+ ctx.name_ref(),
+ Some(name_ref) if name_ref.syntax().text() == name.to_smol_str().as_str()
)
};
for (name, def) in module_scope {
- let is_name_already_imported =
- already_imported_names.contains(name.as_text().unwrap().as_str());
+ let is_name_already_imported = name
+ .as_text()
+ .map_or(false, |text| already_imported_names.contains(text.as_str()));
let add_resolution = match def {
ScopeDef::Unknown if unknown_is_current(&name) => {
diff --git a/crates/ide_completion/src/completions/vis.rs b/crates/ide_completion/src/completions/vis.rs
index 338e003437..7315a488b8 100644
--- a/crates/ide_completion/src/completions/vis.rs
+++ b/crates/ide_completion/src/completions/vis.rs
@@ -8,11 +8,11 @@ use crate::{
};
pub(crate) fn complete_vis(acc: &mut Completions, ctx: &CompletionContext) {
- let (is_absolute_path, qualifier, has_in_token) = match ctx.path_context {
+ let (&is_absolute_path, qualifier, &has_in_token) = match &ctx.path_context {
Some(PathCompletionCtx {
kind: Some(PathKind::Vis { has_in_token }),
is_absolute_path,
- ref qualifier,
+ qualifier,
..
}) => (is_absolute_path, qualifier, has_in_token),
_ => return,
diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs
index 3ba02d78b5..a98e10a226 100644
--- a/crates/ide_completion/src/context.rs
+++ b/crates/ide_completion/src/context.rs
@@ -50,6 +50,7 @@ pub(super) enum PathKind {
Type,
Attr { kind: AttrKind, annotated_item_kind: Option<SyntaxKind> },
Derive,
+ // This should be removed in favor of `has_macro_bang` in PathCompletionContext
Mac,
Pat,
Vis { has_in_token: bool },
@@ -196,6 +197,14 @@ impl<'a> CompletionContext<'a> {
}
}
+ pub(crate) fn name_ref(&self) -> Option<&ast::NameRef> {
+ self.name_syntax.as_ref().and_then(ast::NameLike::as_name_ref)
+ }
+
+ pub(crate) fn lifetime(&self) -> Option<&ast::Lifetime> {
+ self.name_syntax.as_ref().and_then(ast::NameLike::as_lifetime)
+ }
+
pub(crate) fn previous_token_is(&self, kind: SyntaxKind) -> bool {
self.previous_token.as_ref().map_or(false, |tok| tok.kind() == kind)
}
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs
index e1d4addb52..f2153ca921 100644
--- a/crates/syntax/src/ast/node_ext.rs
+++ b/crates/syntax/src/ast/node_ext.rs
@@ -426,6 +426,12 @@ impl NameLike {
_ => None,
}
}
+ pub fn as_lifetime(&self) -> Option<&ast::Lifetime> {
+ match self {
+ NameLike::Lifetime(lifetime) => Some(lifetime),
+ _ => None,
+ }
+ }
}
impl ast::AstNode for NameLike {