aboutsummaryrefslogtreecommitdiff
path: root/crates/completion
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2020-12-20 18:20:55 +0000
committerLukas Wirth <[email protected]>2020-12-20 18:20:55 +0000
commita5874a38cb1bbaee685ff92a40b66f9d5bcf34f6 (patch)
tree90997c417f7249cb0f6073a4a37159a401ae5eba /crates/completion
parent2f6a24950a555bdfecbda7a50735d643f9d0e7f9 (diff)
Reduce code duplication in pattern completion
Diffstat (limited to 'crates/completion')
-rw-r--r--crates/completion/src/render/pattern.rs63
1 files changed, 29 insertions, 34 deletions
diff --git a/crates/completion/src/render/pattern.rs b/crates/completion/src/render/pattern.rs
index 2327bf363..cfe2116bd 100644
--- a/crates/completion/src/render/pattern.rs
+++ b/crates/completion/src/render/pattern.rs
@@ -8,8 +8,8 @@ use crate::{
8 CompletionItemKind, 8 CompletionItemKind,
9}; 9};
10 10
11pub(crate) fn render_struct_pat<'a>( 11pub(crate) fn render_struct_pat(
12 ctx: RenderContext<'a>, 12 ctx: RenderContext<'_>,
13 strukt: hir::Struct, 13 strukt: hir::Struct,
14 local_name: Option<Name>, 14 local_name: Option<Name>,
15) -> Option<CompletionItem> { 15) -> Option<CompletionItem> {
@@ -30,24 +30,7 @@ pub(crate) fn render_struct_pat<'a>(
30 let fields_omitted = n_fields - fields.len() > 0; 30 let fields_omitted = n_fields - fields.len() > 0;
31 31
32 let name = local_name.unwrap_or_else(|| strukt.name(ctx.db())).to_string(); 32 let name = local_name.unwrap_or_else(|| strukt.name(ctx.db())).to_string();
33 let mut pat = match strukt.kind(ctx.db()) { 33 let pat = render_pat(&ctx, &name, strukt.kind(ctx.db()), &fields, fields_omitted)?;
34 StructKind::Tuple if ctx.snippet_cap().is_some() => {
35 render_tuple_as_pat(&fields, &name, fields_omitted)
36 }
37 StructKind::Record => {
38 render_record_as_pat(ctx.db(), ctx.snippet_cap(), &fields, &name, fields_omitted)
39 }
40 _ => return None,
41 };
42
43 if ctx.completion.is_param {
44 pat.push(':');
45 pat.push(' ');
46 pat.push_str(&name);
47 }
48 if ctx.snippet_cap().is_some() {
49 pat.push_str("$0");
50 }
51 34
52 let mut completion = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), name) 35 let mut completion = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), name)
53 .kind(CompletionItemKind::Binding) 36 .kind(CompletionItemKind::Binding)
@@ -62,8 +45,8 @@ pub(crate) fn render_struct_pat<'a>(
62 Some(completion.build()) 45 Some(completion.build())
63} 46}
64 47
65pub(crate) fn render_variant_pat<'a>( 48pub(crate) fn render_variant_pat(
66 ctx: RenderContext<'a>, 49 ctx: RenderContext<'_>,
67 variant: hir::Variant, 50 variant: hir::Variant,
68 local_name: Option<Name>, 51 local_name: Option<Name>,
69) -> Option<CompletionItem> { 52) -> Option<CompletionItem> {
@@ -80,7 +63,29 @@ pub(crate) fn render_variant_pat<'a>(
80 let fields_omitted = n_fields - fields.len() > 0; 63 let fields_omitted = n_fields - fields.len() > 0;
81 64
82 let name = local_name.unwrap_or_else(|| variant.name(ctx.db())).to_string(); 65 let name = local_name.unwrap_or_else(|| variant.name(ctx.db())).to_string();
83 let mut pat = match variant.kind(ctx.db()) { 66 let pat = render_pat(&ctx, &name, variant.kind(ctx.db()), &fields, fields_omitted)?;
67
68 let mut completion = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), name)
69 .kind(CompletionItemKind::Binding)
70 .set_documentation(ctx.docs(variant))
71 .set_deprecated(ctx.is_deprecated(variant))
72 .detail(&pat);
73 if let Some(snippet_cap) = ctx.snippet_cap() {
74 completion = completion.insert_snippet(snippet_cap, pat);
75 } else {
76 completion = completion.insert_text(pat);
77 }
78 Some(completion.build())
79}
80
81fn render_pat(
82 ctx: &RenderContext<'_>,
83 name: &str,
84 kind: StructKind,
85 fields: &[hir::Field],
86 fields_omitted: bool,
87) -> Option<String> {
88 let mut pat = match kind {
84 StructKind::Tuple if ctx.snippet_cap().is_some() => { 89 StructKind::Tuple if ctx.snippet_cap().is_some() => {
85 render_tuple_as_pat(&fields, &name, fields_omitted) 90 render_tuple_as_pat(&fields, &name, fields_omitted)
86 } 91 }
@@ -98,17 +103,7 @@ pub(crate) fn render_variant_pat<'a>(
98 if ctx.snippet_cap().is_some() { 103 if ctx.snippet_cap().is_some() {
99 pat.push_str("$0"); 104 pat.push_str("$0");
100 } 105 }
101 let mut completion = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), name) 106 Some(pat)
102 .kind(CompletionItemKind::Binding)
103 .set_documentation(ctx.docs(variant))
104 .set_deprecated(ctx.is_deprecated(variant))
105 .detail(&pat);
106 if let Some(snippet_cap) = ctx.snippet_cap() {
107 completion = completion.insert_snippet(snippet_cap, pat);
108 } else {
109 completion = completion.insert_text(pat);
110 }
111 Some(completion.build())
112} 107}
113 108
114fn render_record_as_pat( 109fn render_record_as_pat(