Skip to content

Commit adbcedd

Browse files
committed
Remove the second code-path for completing names in patterns
1 parent 6a2dd7b commit adbcedd

File tree

4 files changed

+31
-70
lines changed

4 files changed

+31
-70
lines changed

crates/ra_ide/src/completion/complete_pattern.rs

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,25 @@ use crate::completion::{CompletionContext, Completions};
44

55
/// Completes constats and paths in patterns.
66
pub(super) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
7-
if !ctx.is_pat_binding {
7+
if !ctx.is_pat_binding_or_const {
88
return;
99
}
1010
// FIXME: ideally, we should look at the type we are matching against and
1111
// suggest variants + auto-imports
1212
ctx.scope().process_all_names(&mut |name, res| {
13-
let def = match &res {
14-
hir::ScopeDef::ModuleDef(def) => def,
13+
match &res {
14+
hir::ScopeDef::ModuleDef(def) => match def {
15+
hir::ModuleDef::Adt(hir::Adt::Enum(..))
16+
| hir::ModuleDef::Adt(hir::Adt::Struct(..))
17+
| hir::ModuleDef::EnumVariant(..)
18+
| hir::ModuleDef::Const(..)
19+
| hir::ModuleDef::Module(..) => (),
20+
_ => return,
21+
},
22+
hir::ScopeDef::MacroDef(_) => (),
1523
_ => return,
1624
};
17-
match def {
18-
hir::ModuleDef::Adt(hir::Adt::Enum(..))
19-
| hir::ModuleDef::EnumVariant(..)
20-
| hir::ModuleDef::Const(..)
21-
| hir::ModuleDef::Module(..) => (),
22-
_ => return,
23-
}
25+
2426
acc.add_resolution(ctx, name.to_string(), &res)
2527
});
2628
}
@@ -69,34 +71,13 @@ mod tests {
6971
insert: "E",
7072
kind: Enum,
7173
},
72-
CompletionItem {
73-
label: "E",
74-
source_range: [246; 246),
75-
delete: [246; 246),
76-
insert: "E",
77-
kind: Enum,
78-
},
7974
CompletionItem {
8075
label: "X",
8176
source_range: [246; 246),
8277
delete: [246; 246),
8378
insert: "X",
8479
kind: EnumVariant,
8580
},
86-
CompletionItem {
87-
label: "X",
88-
source_range: [246; 246),
89-
delete: [246; 246),
90-
insert: "X",
91-
kind: EnumVariant,
92-
},
93-
CompletionItem {
94-
label: "Z",
95-
source_range: [246; 246),
96-
delete: [246; 246),
97-
insert: "Z",
98-
kind: Const,
99-
},
10081
CompletionItem {
10182
label: "Z",
10283
source_range: [246; 246),
@@ -111,13 +92,6 @@ mod tests {
11192
insert: "m",
11293
kind: Module,
11394
},
114-
CompletionItem {
115-
label: "m",
116-
source_range: [246; 246),
117-
delete: [246; 246),
118-
insert: "m",
119-
kind: Module,
120-
},
12195
]
12296
"###);
12397
}
@@ -138,13 +112,6 @@ mod tests {
138112
);
139113
assert_debug_snapshot!(completions, @r###"
140114
[
141-
CompletionItem {
142-
label: "E",
143-
source_range: [151; 151),
144-
delete: [151; 151),
145-
insert: "E",
146-
kind: Enum,
147-
},
148115
CompletionItem {
149116
label: "E",
150117
source_range: [151; 151),

crates/ra_ide/src/completion/complete_scope.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
//! Completion of names from the current scope, e.g. locals and imported items.
22
33
use crate::completion::{CompletionContext, Completions};
4-
use hir::{ModuleDef, ScopeDef};
54

65
pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
7-
if !ctx.is_trivial_path && !ctx.is_pat_binding_and_path {
6+
if !(ctx.is_trivial_path && !ctx.is_pat_binding_or_const) {
87
return;
98
}
109

11-
ctx.scope().process_all_names(&mut |name, res| match (ctx.is_pat_binding_and_path, &res) {
12-
(true, ScopeDef::ModuleDef(ModuleDef::Function(..))) => (),
13-
(true, ScopeDef::ModuleDef(ModuleDef::Static(..))) => (),
14-
(true, ScopeDef::Local(..)) => (),
15-
_ => acc.add_resolution(ctx, name.to_string(), &res),
16-
});
10+
ctx.scope().process_all_names(&mut |name, res| acc.add_resolution(ctx, name.to_string(), &res));
1711
}
1812

1913
#[cfg(test)]

crates/ra_ide/src/completion/completion_context.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ pub(crate) struct CompletionContext<'a> {
3535
pub(super) is_param: bool,
3636
/// If a name-binding or reference to a const in a pattern.
3737
/// Irrefutable patterns (like let) are excluded.
38-
pub(super) is_pat_binding: bool,
39-
// A bind battern which may also be part of a path.
40-
// if let Some(En<|>) = Some(Enum::A)
41-
pub(super) is_pat_binding_and_path: bool,
38+
pub(super) is_pat_binding_or_const: bool,
4239
/// A single-indent path, like `foo`. `::foo` should not be considered a trivial path.
4340
pub(super) is_trivial_path: bool,
4441
/// If not a trivial path, the prefix (qualifier).
@@ -97,8 +94,7 @@ impl<'a> CompletionContext<'a> {
9794
record_lit_pat: None,
9895
impl_def: None,
9996
is_param: false,
100-
is_pat_binding: false,
101-
is_pat_binding_and_path: false,
97+
is_pat_binding_or_const: false,
10298
is_trivial_path: false,
10399
path_prefix: None,
104100
after_if: false,
@@ -190,18 +186,19 @@ impl<'a> CompletionContext<'a> {
190186
// suggest declaration names, see `CompletionKind::Magic`.
191187
if let Some(name) = find_node_at_offset::<ast::Name>(&file_with_fake_ident, offset) {
192188
if let Some(bind_pat) = name.syntax().ancestors().find_map(ast::BindPat::cast) {
193-
let parent = bind_pat.syntax().parent();
194-
if parent.clone().and_then(ast::MatchArm::cast).is_some()
195-
|| parent.clone().and_then(ast::Condition::cast).is_some()
196-
{
197-
self.is_pat_binding = true;
189+
self.is_pat_binding_or_const = true;
190+
if bind_pat.has_at() || bind_pat.is_ref() || bind_pat.is_mutable() {
191+
self.is_pat_binding_or_const = false;
198192
}
199-
200-
if parent.and_then(ast::RecordFieldPatList::cast).is_none()
201-
&& bind_pat.pat().is_none()
202-
&& !bind_pat.is_ref()
203-
{
204-
self.is_pat_binding_and_path = true;
193+
if bind_pat.syntax().parent().and_then(ast::RecordFieldPatList::cast).is_some() {
194+
self.is_pat_binding_or_const = false;
195+
}
196+
if let Some(let_stmt) = bind_pat.syntax().ancestors().find_map(ast::LetStmt::cast) {
197+
if let Some(pat) = let_stmt.pat() {
198+
if bind_pat.syntax().text_range().is_subrange(&pat.syntax().text_range()) {
199+
self.is_pat_binding_or_const = false;
200+
}
201+
}
205202
}
206203
}
207204
if is_node::<ast::Param>(name.syntax()) {

crates/ra_syntax/src/ast/extensions.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ impl ast::BindPat {
325325
pub fn is_ref(&self) -> bool {
326326
self.syntax().children_with_tokens().any(|n| n.kind() == T![ref])
327327
}
328+
pub fn has_at(&self) -> bool {
329+
self.syntax().children_with_tokens().any(|it| it.kind() == T![@])
330+
}
328331
}
329332

330333
pub struct SlicePatComponents {

0 commit comments

Comments
 (0)