Skip to content

Commit 6941a7f

Browse files
committed
- Exclude Local Scope for BindPats
- Exclude BindPats with @ or ref - Remove outdated test and add one testing for ref
1 parent b6d6277 commit 6941a7f

File tree

2 files changed

+16
-58
lines changed

2 files changed

+16
-58
lines changed

crates/ra_ide/src/completion/complete_scope.rs

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

56
pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
67
if !ctx.is_trivial_path && !ctx.is_pat_binding_and_path {
78
return;
89
}
910

10-
ctx.scope().process_all_names(&mut |name, res| acc.add_resolution(ctx, name.to_string(), &res));
11+
ctx.scope().process_all_names(&mut |name, res| match (ctx.is_pat_binding_and_path, &res) {
12+
(true, ScopeDef::Local(..)) => {}
13+
_ => acc.add_resolution(ctx, name.to_string(), &res),
14+
});
1115
}
1216

1317
#[cfg(test)]
@@ -21,53 +25,23 @@ mod tests {
2125
}
2226

2327
#[test]
24-
fn nested_bind_pat_and_path() {
28+
fn bind_pat_and_path_ignore_ref() {
2529
assert_debug_snapshot!(
2630
do_reference_completion(
2731
r"
28-
enum First {
32+
enum Enum {
2933
A,
3034
B,
3135
}
32-
enum Second {
33-
A(First),
34-
B(First),
35-
}
36-
fn quux(x: Option<Option<Second>>>) {
36+
fn quux(x: Option<Enum>) {
3737
match x {
3838
None => (),
39-
Some(Some(Second(Fi<|>))) => (),
39+
Some(ref en<|>) => (),
4040
}
4141
}
4242
"
4343
),
44-
@r###"
45-
[
46-
CompletionItem {
47-
label: "First",
48-
source_range: [363; 365),
49-
delete: [363; 365),
50-
insert: "First",
51-
kind: Enum,
52-
},
53-
CompletionItem {
54-
label: "Second",
55-
source_range: [363; 365),
56-
delete: [363; 365),
57-
insert: "Second",
58-
kind: Enum,
59-
},
60-
CompletionItem {
61-
label: "quux(…)",
62-
source_range: [363; 365),
63-
delete: [363; 365),
64-
insert: "quux(${1:x})$0",
65-
kind: Function,
66-
lookup: "quux",
67-
detail: "fn quux(x: Option<Option<Second>>)",
68-
},
69-
]
70-
"###
44+
@r###"[]"###
7145
);
7246
}
7347

@@ -83,7 +57,7 @@ mod tests {
8357
fn quux(x: Option<Enum>) {
8458
match x {
8559
None => (),
86-
Some(en<|>) => (),
60+
Some(En<|>) => (),
8761
}
8862
}
8963
"
@@ -97,13 +71,6 @@ mod tests {
9771
insert: "Enum",
9872
kind: Enum,
9973
},
100-
CompletionItem {
101-
label: "None",
102-
source_range: [231; 233),
103-
delete: [231; 233),
104-
insert: "None",
105-
kind: Binding,
106-
},
10774
CompletionItem {
10875
label: "quux(…)",
10976
source_range: [231; 233),
@@ -112,13 +79,7 @@ mod tests {
11279
kind: Function,
11380
lookup: "quux",
11481
detail: "fn quux(x: Option<Enum>)",
115-
},
116-
CompletionItem {
117-
label: "x",
118-
source_range: [231; 233),
119-
delete: [231; 233),
120-
insert: "x",
121-
kind: Binding,
82+
trigger_call_info: true,
12283
},
12384
]
12485
"###

crates/ra_ide/src/completion/completion_context.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,19 +190,16 @@ impl<'a> CompletionContext<'a> {
190190
// suggest declaration names, see `CompletionKind::Magic`.
191191
if let Some(name) = find_node_at_offset::<ast::Name>(&file_with_fake_ident, offset) {
192192
if let Some(bind_pat) = name.syntax().ancestors().find_map(ast::BindPat::cast) {
193-
let mut parent = bind_pat.syntax().parent();
193+
let parent = bind_pat.syntax().parent();
194194
if parent.clone().and_then(ast::MatchArm::cast).is_some()
195195
|| parent.clone().and_then(ast::Condition::cast).is_some()
196196
{
197197
self.is_pat_binding = true;
198198
}
199199

200-
while let Some(_) = parent.clone().and_then(ast::TupleStructPat::cast) {
201-
parent = parent.and_then(|p| p.parent());
202-
if parent.clone().and_then(ast::MatchArm::cast).is_some() {
203-
self.is_pat_binding_and_path = true;
204-
break;
205-
}
200+
let bind_pat_string = bind_pat.syntax().to_string();
201+
if !bind_pat_string.contains("ref ") && !bind_pat_string.contains(" @ ") {
202+
self.is_pat_binding_and_path = true;
206203
}
207204
}
208205
if is_node::<ast::Param>(name.syntax()) {

0 commit comments

Comments
 (0)