Skip to content

Commit c606229

Browse files
committed
Auto merge of #12060 - Veykril:completion-ctx, r=Veykril
minor: Simplify bors r+
2 parents 1894473 + ea45e54 commit c606229

File tree

5 files changed

+29
-21
lines changed

5 files changed

+29
-21
lines changed

crates/ide_completion/src/completions/lifetime.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext)
2222
Some(LifetimeContext::LifetimeParam { is_decl: false, param }) => Some(param),
2323
_ => return,
2424
};
25-
let param_lifetime = match (&ctx.name_syntax, lp.and_then(|lp| lp.lifetime())) {
26-
(Some(ast::NameLike::Lifetime(lt)), Some(lp)) if lp == lt.clone() => return,
25+
let param_lifetime = match (ctx.lifetime(), lp.and_then(|lp| lp.lifetime())) {
26+
(Some(lt), Some(lp)) if lp == lt.clone() => return,
2727
(Some(_), Some(lp)) => Some(lp),
2828
_ => None,
2929
};

crates/ide_completion/src/completions/use_.rs

+10-17
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@ use crate::{
1111
};
1212

1313
pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext) {
14-
let (is_absolute_path, qualifier) = match ctx.path_context {
14+
let (&is_absolute_path, qualifier) = match &ctx.path_context {
1515
Some(PathCompletionCtx {
16-
kind: Some(PathKind::Use),
17-
is_absolute_path,
18-
ref qualifier,
19-
..
16+
kind: Some(PathKind::Use), is_absolute_path, qualifier, ..
2017
}) => (is_absolute_path, qualifier),
2118
_ => return,
2219
};
@@ -45,13 +42,9 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext)
4542
if let Some(list) = ctx.token.ancestors().find_map(ast::UseTreeList::cast) {
4643
let use_tree = list.parent_use_tree();
4744
if use_tree.path().as_ref() == Some(path) {
48-
for tree in list.use_trees() {
49-
if tree.is_simple_path() {
50-
if let Some(name) =
51-
tree.path().and_then(|path| path.as_single_name_ref())
52-
{
53-
already_imported_names.insert(name.to_string());
54-
}
45+
for tree in list.use_trees().filter(|tree| tree.is_simple_path()) {
46+
if let Some(name) = tree.path().and_then(|path| path.as_single_name_ref()) {
47+
already_imported_names.insert(name.to_string());
5548
}
5649
}
5750
}
@@ -62,14 +55,14 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext)
6255
let module_scope = module.scope(ctx.db, Some(ctx.module));
6356
let unknown_is_current = |name: &hir::Name| {
6457
matches!(
65-
ctx.name_syntax.as_ref(),
66-
Some(ast::NameLike::NameRef(name_ref))
67-
if name_ref.syntax().text() == name.to_smol_str().as_str()
58+
ctx.name_ref(),
59+
Some(name_ref) if name_ref.syntax().text() == name.to_smol_str().as_str()
6860
)
6961
};
7062
for (name, def) in module_scope {
71-
let is_name_already_imported =
72-
already_imported_names.contains(name.as_text().unwrap().as_str());
63+
let is_name_already_imported = name
64+
.as_text()
65+
.map_or(false, |text| already_imported_names.contains(text.as_str()));
7366

7467
let add_resolution = match def {
7568
ScopeDef::Unknown if unknown_is_current(&name) => {

crates/ide_completion/src/completions/vis.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ use crate::{
88
};
99

1010
pub(crate) fn complete_vis(acc: &mut Completions, ctx: &CompletionContext) {
11-
let (is_absolute_path, qualifier, has_in_token) = match ctx.path_context {
11+
let (&is_absolute_path, qualifier, &has_in_token) = match &ctx.path_context {
1212
Some(PathCompletionCtx {
1313
kind: Some(PathKind::Vis { has_in_token }),
1414
is_absolute_path,
15-
ref qualifier,
15+
qualifier,
1616
..
1717
}) => (is_absolute_path, qualifier, has_in_token),
1818
_ => return,

crates/ide_completion/src/context.rs

+9
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub(super) enum PathKind {
5050
Type,
5151
Attr { kind: AttrKind, annotated_item_kind: Option<SyntaxKind> },
5252
Derive,
53+
// This should be removed in favor of `has_macro_bang` in PathCompletionContext
5354
Mac,
5455
Pat,
5556
Vis { has_in_token: bool },
@@ -196,6 +197,14 @@ impl<'a> CompletionContext<'a> {
196197
}
197198
}
198199

200+
pub(crate) fn name_ref(&self) -> Option<&ast::NameRef> {
201+
self.name_syntax.as_ref().and_then(ast::NameLike::as_name_ref)
202+
}
203+
204+
pub(crate) fn lifetime(&self) -> Option<&ast::Lifetime> {
205+
self.name_syntax.as_ref().and_then(ast::NameLike::as_lifetime)
206+
}
207+
199208
pub(crate) fn previous_token_is(&self, kind: SyntaxKind) -> bool {
200209
self.previous_token.as_ref().map_or(false, |tok| tok.kind() == kind)
201210
}

crates/syntax/src/ast/node_ext.rs

+6
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,12 @@ impl NameLike {
426426
_ => None,
427427
}
428428
}
429+
pub fn as_lifetime(&self) -> Option<&ast::Lifetime> {
430+
match self {
431+
NameLike::Lifetime(lifetime) => Some(lifetime),
432+
_ => None,
433+
}
434+
}
429435
}
430436

431437
impl ast::AstNode for NameLike {

0 commit comments

Comments
 (0)