Skip to content

Commit 6207ac9

Browse files
bors[bot]matklad
andauthored
Merge #3840
3840: Add parens for enums r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents cde92d0 + a5e8dfd commit 6207ac9

File tree

7 files changed

+236
-127
lines changed

7 files changed

+236
-127
lines changed

crates/ra_ide/src/completion/complete_dot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: &T
6161
&& ctx.scope().module().map_or(true, |m| func.is_visible_from(ctx.db, m))
6262
&& seen_methods.insert(func.name(ctx.db))
6363
{
64-
acc.add_function(ctx, func);
64+
acc.add_function(ctx, func, None);
6565
}
6666
None::<()>
6767
});

crates/ra_ide/src/completion/complete_path.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
3838
hir::ModuleDef::Adt(_) | hir::ModuleDef::TypeAlias(_) => {
3939
if let hir::ModuleDef::Adt(Adt::Enum(e)) = def {
4040
for variant in e.variants(ctx.db) {
41-
acc.add_enum_variant(ctx, variant);
41+
acc.add_enum_variant(ctx, variant, None);
4242
}
4343
}
4444
let ty = match def {
@@ -58,7 +58,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
5858
match item {
5959
hir::AssocItem::Function(func) => {
6060
if !func.has_self_param(ctx.db) {
61-
acc.add_function(ctx, func);
61+
acc.add_function(ctx, func, None);
6262
}
6363
}
6464
hir::AssocItem::Const(ct) => acc.add_const(ctx, ct),
@@ -87,7 +87,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
8787
match item {
8888
hir::AssocItem::Function(func) => {
8989
if !func.has_self_param(ctx.db) {
90-
acc.add_function(ctx, func);
90+
acc.add_function(ctx, func, None);
9191
}
9292
}
9393
hir::AssocItem::Const(ct) => acc.add_const(ctx, ct),
@@ -355,15 +355,17 @@ mod tests {
355355
@r###"
356356
[
357357
CompletionItem {
358-
label: "Bar",
358+
label: "Bar(…)",
359359
source_range: [116; 116),
360360
delete: [116; 116),
361-
insert: "Bar",
361+
insert: "Bar($0)",
362362
kind: EnumVariant,
363+
lookup: "Bar",
363364
detail: "(i32)",
364365
documentation: Documentation(
365366
"Bar Variant with i32",
366367
),
368+
trigger_call_info: true,
367369
},
368370
CompletionItem {
369371
label: "Foo",
@@ -403,15 +405,17 @@ mod tests {
403405
@r###"
404406
[
405407
CompletionItem {
406-
label: "Bar",
408+
label: "Bar(…)",
407409
source_range: [180; 180),
408410
delete: [180; 180),
409-
insert: "Bar",
411+
insert: "Bar($0)",
410412
kind: EnumVariant,
413+
lookup: "Bar",
411414
detail: "(i32, u32)",
412415
documentation: Documentation(
413416
"Bar Variant with i32 and u32",
414417
),
418+
trigger_call_info: true,
415419
},
416420
CompletionItem {
417421
label: "Foo",
@@ -425,15 +429,17 @@ mod tests {
425429
),
426430
},
427431
CompletionItem {
428-
label: "S",
432+
label: "S(…)",
429433
source_range: [180; 180),
430434
delete: [180; 180),
431-
insert: "S",
435+
insert: "S($0)",
432436
kind: EnumVariant,
437+
lookup: "S",
433438
detail: "(S)",
434439
documentation: Documentation(
435440
"",
436441
),
442+
trigger_call_info: true,
437443
},
438444
]
439445
"###

crates/ra_ide/src/completion/complete_pattern.rs

Lines changed: 13 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,33 +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,
85-
},
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,
80+
detail: "()",
9981
},
10082
CompletionItem {
10183
label: "Z",
@@ -111,13 +93,6 @@ mod tests {
11193
insert: "m",
11294
kind: Module,
11395
},
114-
CompletionItem {
115-
label: "m",
116-
source_range: [246; 246),
117-
delete: [246; 246),
118-
insert: "m",
119-
kind: Module,
120-
},
12196
]
12297
"###);
12398
}
@@ -138,13 +113,6 @@ mod tests {
138113
);
139114
assert_debug_snapshot!(completions, @r###"
140115
[
141-
CompletionItem {
142-
label: "E",
143-
source_range: [151; 151),
144-
delete: [151; 151),
145-
insert: "E",
146-
kind: Enum,
147-
},
148116
CompletionItem {
149117
label: "E",
150118
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()) {

0 commit comments

Comments
 (0)