Skip to content

Commit 83121ef

Browse files
committed
Reduce some more code duplication
1 parent 2cd2947 commit 83121ef

File tree

5 files changed

+76
-94
lines changed

5 files changed

+76
-94
lines changed

crates/completion/src/completions/pattern.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! Completes constats and paths in patterns.
22
3-
use hir::StructKind;
4-
53
use crate::{CompletionContext, Completions};
64

75
/// Completes constants and paths in patterns.
@@ -22,11 +20,7 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
2220
acc.add_struct_pat(ctx, strukt.clone(), Some(name.clone()));
2321
true
2422
}
25-
hir::ModuleDef::Variant(variant)
26-
if !ctx.is_irrefutable_pat_binding
27-
// render_resolution already does some pattern completion tricks for tuple variants
28-
&& variant.kind(ctx.db) == StructKind::Record =>
29-
{
23+
hir::ModuleDef::Variant(variant) if !ctx.is_irrefutable_pat_binding => {
3024
acc.add_variant_pat(ctx, variant.clone(), Some(name.clone()));
3125
true
3226
}
@@ -49,7 +43,10 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
4943
mod tests {
5044
use expect_test::{expect, Expect};
5145

52-
use crate::{test_utils::completion_list, CompletionKind};
46+
use crate::{
47+
test_utils::{check_edit, completion_list},
48+
CompletionKind,
49+
};
5350

5451
fn check(ra_fixture: &str, expect: Expect) {
5552
let actual = completion_list(ra_fixture, CompletionKind::Reference);
@@ -81,7 +78,7 @@ fn foo() {
8178
en E
8279
ct Z
8380
st Bar
84-
ev X ()
81+
ev X
8582
md m
8683
"#]],
8784
);
@@ -238,4 +235,27 @@ fn outer() {
238235
"#]],
239236
)
240237
}
238+
239+
#[test]
240+
fn only_shows_ident_completion() {
241+
check_edit(
242+
"Foo",
243+
r#"
244+
struct Foo(i32);
245+
fn main() {
246+
match Foo(92) {
247+
<|>(92) => (),
248+
}
249+
}
250+
"#,
251+
r#"
252+
struct Foo(i32);
253+
fn main() {
254+
match Foo(92) {
255+
Foo(92) => (),
256+
}
257+
}
258+
"#,
259+
);
260+
}
241261
}

crates/completion/src/render.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ impl<'a> Render<'a> {
160160
let item = render_fn(self.ctx, import_to_add, Some(local_name), *func);
161161
return Some(item);
162162
}
163+
ScopeDef::ModuleDef(Variant(_))
164+
if self.ctx.completion.is_pat_binding_or_const
165+
| self.ctx.completion.is_irrefutable_pat_binding =>
166+
{
167+
CompletionItemKind::EnumVariant
168+
}
163169
ScopeDef::ModuleDef(Variant(var)) => {
164170
let item = render_variant(self.ctx, import_to_add, Some(local_name), *var, None);
165171
return Some(item);

crates/completion/src/render/builder_ext.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ impl Builder {
3434
return false;
3535
}
3636
if ctx.is_pattern_call {
37-
mark::hit!(dont_duplicate_pattern_parens);
3837
return false;
3938
}
4039
if ctx.is_call {

crates/completion/src/render/enum_variant.rs

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -124,51 +124,6 @@ use Option::*;
124124
fn main() -> Option<i32> {
125125
Some($0)
126126
}
127-
"#,
128-
);
129-
check_edit(
130-
"Some",
131-
r#"
132-
enum Option<T> { Some(T), None }
133-
use Option::*;
134-
fn main(value: Option<i32>) {
135-
match value {
136-
Som<|>
137-
}
138-
}
139-
"#,
140-
r#"
141-
enum Option<T> { Some(T), None }
142-
use Option::*;
143-
fn main(value: Option<i32>) {
144-
match value {
145-
Some($0)
146-
}
147-
}
148-
"#,
149-
);
150-
}
151-
152-
#[test]
153-
fn dont_duplicate_pattern_parens() {
154-
mark::check!(dont_duplicate_pattern_parens);
155-
check_edit(
156-
"Var",
157-
r#"
158-
enum E { Var(i32) }
159-
fn main() {
160-
match E::Var(92) {
161-
E::<|>(92) => (),
162-
}
163-
}
164-
"#,
165-
r#"
166-
enum E { Var(i32) }
167-
fn main() {
168-
match E::Var(92) {
169-
E::Var(92) => (),
170-
}
171-
}
172127
"#,
173128
);
174129
}

crates/completion/src/render/pattern.rs

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,43 @@ use crate::{
88
CompletionItemKind,
99
};
1010

11+
fn visible_fields(
12+
ctx: &RenderContext<'_>,
13+
fields: &[hir::Field],
14+
item: impl HasAttrs,
15+
) -> Option<(Vec<hir::Field>, bool)> {
16+
let module = ctx.completion.scope.module()?;
17+
let n_fields = fields.len();
18+
let fields = fields
19+
.into_iter()
20+
.filter(|field| field.is_visible_from(ctx.db(), module))
21+
.copied()
22+
.collect::<Vec<_>>();
23+
24+
let fields_omitted =
25+
n_fields - fields.len() > 0 || item.attrs(ctx.db()).by_key("non_exhaustive").exists();
26+
Some((fields, fields_omitted))
27+
}
28+
1129
pub(crate) fn render_struct_pat(
1230
ctx: RenderContext<'_>,
1331
strukt: hir::Struct,
1432
local_name: Option<Name>,
1533
) -> Option<CompletionItem> {
1634
let _p = profile::span("render_struct_pat");
1735

18-
let module = ctx.completion.scope.module()?;
1936
let fields = strukt.fields(ctx.db());
20-
let n_fields = fields.len();
21-
let fields = fields
22-
.into_iter()
23-
.filter(|field| field.is_visible_from(ctx.db(), module))
24-
.collect::<Vec<_>>();
37+
let (visible_fields, fields_omitted) = visible_fields(&ctx, &fields, strukt)?;
2538

26-
if fields.is_empty() {
39+
if visible_fields.is_empty() {
2740
// Matching a struct without matching its fields is pointless, unlike matching a Variant without its fields
2841
return None;
2942
}
30-
let fields_omitted =
31-
n_fields - fields.len() > 0 || strukt.attrs(ctx.db()).by_key("non_exhaustive").exists();
3243

3344
let name = local_name.unwrap_or_else(|| strukt.name(ctx.db())).to_string();
34-
let pat = render_pat(&ctx, &name, strukt.kind(ctx.db()), &fields, fields_omitted)?;
45+
let pat = render_pat(&ctx, &name, strukt.kind(ctx.db()), &visible_fields, fields_omitted)?;
3546

36-
let mut completion = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), name)
37-
.kind(CompletionItemKind::Binding)
38-
.set_documentation(ctx.docs(strukt))
39-
.set_deprecated(ctx.is_deprecated(strukt))
40-
.detail(&pat);
41-
if let Some(snippet_cap) = ctx.snippet_cap() {
42-
completion = completion.insert_snippet(snippet_cap, pat);
43-
} else {
44-
completion = completion.insert_text(pat);
45-
}
46-
Some(completion.build())
47+
Some(build_completion(ctx, name, pat, strukt))
4748
}
4849

4950
pub(crate) fn render_variant_pat(
@@ -53,31 +54,32 @@ pub(crate) fn render_variant_pat(
5354
) -> Option<CompletionItem> {
5455
let _p = profile::span("render_variant_pat");
5556

56-
let module = ctx.completion.scope.module()?;
5757
let fields = variant.fields(ctx.db());
58-
let n_fields = fields.len();
59-
let fields = fields
60-
.into_iter()
61-
.filter(|field| field.is_visible_from(ctx.db(), module))
62-
.collect::<Vec<_>>();
63-
64-
let fields_omitted =
65-
n_fields - fields.len() > 0 || variant.attrs(ctx.db()).by_key("non_exhaustive").exists();
58+
let (visible_fields, fields_omitted) = visible_fields(&ctx, &fields, variant)?;
6659

6760
let name = local_name.unwrap_or_else(|| variant.name(ctx.db())).to_string();
68-
let pat = render_pat(&ctx, &name, variant.kind(ctx.db()), &fields, fields_omitted)?;
61+
let pat = render_pat(&ctx, &name, variant.kind(ctx.db()), &visible_fields, fields_omitted)?;
6962

70-
let mut completion = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), name)
63+
Some(build_completion(ctx, name, pat, variant))
64+
}
65+
66+
fn build_completion(
67+
ctx: RenderContext<'_>,
68+
name: String,
69+
pat: String,
70+
item: impl HasAttrs + Copy,
71+
) -> CompletionItem {
72+
let completion = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), name)
7173
.kind(CompletionItemKind::Binding)
72-
.set_documentation(ctx.docs(variant))
73-
.set_deprecated(ctx.is_deprecated(variant))
74+
.set_documentation(ctx.docs(item))
75+
.set_deprecated(ctx.is_deprecated(item))
7476
.detail(&pat);
75-
if let Some(snippet_cap) = ctx.snippet_cap() {
76-
completion = completion.insert_snippet(snippet_cap, pat);
77+
let completion = if let Some(snippet_cap) = ctx.snippet_cap() {
78+
completion.insert_snippet(snippet_cap, pat)
7779
} else {
78-
completion = completion.insert_text(pat);
79-
}
80-
Some(completion.build())
80+
completion.insert_text(pat)
81+
};
82+
completion.build()
8183
}
8284

8385
fn render_pat(

0 commit comments

Comments
 (0)