aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorIgor Aleksanov <[email protected]>2020-11-03 07:33:13 +0000
committerIgor Aleksanov <[email protected]>2020-11-03 07:33:13 +0000
commit4d333ebb6372c135e5a723da899528cc22d07faa (patch)
tree7ab4c7f113a1686d7378502e2685d3529cb6f5ba /crates
parentcaf0fa20a7d895612ceee1948d6a9895e53bee4a (diff)
Get rid of do-er antipattern
Diffstat (limited to 'crates')
-rw-r--r--crates/completion/src/completions.rs22
-rw-r--r--crates/completion/src/render.rs50
-rw-r--r--crates/completion/src/render/const_.rs13
-rw-r--r--crates/completion/src/render/enum_variant.rs15
-rw-r--r--crates/completion/src/render/function.rs14
-rw-r--r--crates/completion/src/render/macro_.rs18
-rw-r--r--crates/completion/src/render/type_alias.rs13
7 files changed, 100 insertions, 45 deletions
diff --git a/crates/completion/src/completions.rs b/crates/completion/src/completions.rs
index d59f5ca05..162d567d9 100644
--- a/crates/completion/src/completions.rs
+++ b/crates/completion/src/completions.rs
@@ -52,12 +52,12 @@ impl Completions {
52 } 52 }
53 53
54 pub(crate) fn add_field(&mut self, ctx: &CompletionContext, field: hir::Field, ty: &Type) { 54 pub(crate) fn add_field(&mut self, ctx: &CompletionContext, field: hir::Field, ty: &Type) {
55 let item = Render::new(RenderContext::new(ctx)).add_field(field, ty); 55 let item = render_field(RenderContext::new(ctx), field, ty);
56 self.add(item); 56 self.add(item);
57 } 57 }
58 58
59 pub(crate) fn add_tuple_field(&mut self, ctx: &CompletionContext, field: usize, ty: &Type) { 59 pub(crate) fn add_tuple_field(&mut self, ctx: &CompletionContext, field: usize, ty: &Type) {
60 let item = Render::new(RenderContext::new(ctx)).add_tuple_field(field, ty); 60 let item = render_tuple_field(RenderContext::new(ctx), field, ty);
61 self.add(item); 61 self.add(item);
62 } 62 }
63 63
@@ -67,9 +67,7 @@ impl Completions {
67 local_name: String, 67 local_name: String,
68 resolution: &ScopeDef, 68 resolution: &ScopeDef,
69 ) { 69 ) {
70 if let Some(item) = 70 if let Some(item) = render_resolution(RenderContext::new(ctx), local_name, resolution) {
71 Render::new(RenderContext::new(ctx)).render_resolution(local_name, resolution)
72 {
73 self.add(item); 71 self.add(item);
74 } 72 }
75 } 73 }
@@ -84,7 +82,7 @@ impl Completions {
84 Some(it) => it, 82 Some(it) => it,
85 None => return, 83 None => return,
86 }; 84 };
87 if let Some(item) = MacroRender::new(RenderContext::new(ctx), name, macro_).render() { 85 if let Some(item) = render_macro(RenderContext::new(ctx), name, macro_) {
88 self.add(item); 86 self.add(item);
89 } 87 }
90 } 88 }
@@ -95,18 +93,18 @@ impl Completions {
95 func: hir::Function, 93 func: hir::Function,
96 local_name: Option<String>, 94 local_name: Option<String>,
97 ) { 95 ) {
98 let item = FunctionRender::new(RenderContext::new(ctx), local_name, func).render(); 96 let item = render_fn(RenderContext::new(ctx), local_name, func);
99 self.add(item) 97 self.add(item)
100 } 98 }
101 99
102 pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) { 100 pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {
103 if let Some(item) = ConstRender::new(RenderContext::new(ctx), constant).render() { 101 if let Some(item) = render_const(RenderContext::new(ctx), constant) {
104 self.add(item); 102 self.add(item);
105 } 103 }
106 } 104 }
107 105
108 pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) { 106 pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {
109 if let Some(item) = TypeAliasRender::new(RenderContext::new(ctx), type_alias).render() { 107 if let Some(item) = render_type_alias(RenderContext::new(ctx), type_alias) {
110 self.add(item) 108 self.add(item)
111 } 109 }
112 } 110 }
@@ -117,8 +115,7 @@ impl Completions {
117 variant: hir::EnumVariant, 115 variant: hir::EnumVariant,
118 path: ModPath, 116 path: ModPath,
119 ) { 117 ) {
120 let item = 118 let item = render_enum_variant(RenderContext::new(ctx), None, variant, Some(path));
121 EnumVariantRender::new(RenderContext::new(ctx), None, variant, Some(path)).render();
122 self.add(item); 119 self.add(item);
123 } 120 }
124 121
@@ -128,8 +125,7 @@ impl Completions {
128 variant: hir::EnumVariant, 125 variant: hir::EnumVariant,
129 local_name: Option<String>, 126 local_name: Option<String>,
130 ) { 127 ) {
131 let item = 128 let item = render_enum_variant(RenderContext::new(ctx), local_name, variant, None);
132 EnumVariantRender::new(RenderContext::new(ctx), local_name, variant, None).render();
133 self.add(item); 129 self.add(item);
134 } 130 }
135} 131}
diff --git a/crates/completion/src/render.rs b/crates/completion/src/render.rs
index dfba81734..b54241d51 100644
--- a/crates/completion/src/render.rs
+++ b/crates/completion/src/render.rs
@@ -19,14 +19,32 @@ use crate::{
19}; 19};
20 20
21pub(crate) use crate::render::{ 21pub(crate) use crate::render::{
22 const_::ConstRender, enum_variant::EnumVariantRender, function::FunctionRender, 22 const_::render_const, enum_variant::render_enum_variant, function::render_fn,
23 macro_::MacroRender, type_alias::TypeAliasRender, 23 macro_::render_macro, type_alias::render_type_alias,
24}; 24};
25 25
26/// Generic renderer for completion items. 26pub(crate) fn render_field<'a>(
27#[derive(Debug)] 27 ctx: RenderContext<'a>,
28pub(crate) struct Render<'a> { 28 field: hir::Field,
29 ty: &Type,
30) -> CompletionItem {
31 Render::new(ctx).add_field(field, ty)
32}
33
34pub(crate) fn render_tuple_field<'a>(
29 ctx: RenderContext<'a>, 35 ctx: RenderContext<'a>,
36 field: usize,
37 ty: &Type,
38) -> CompletionItem {
39 Render::new(ctx).add_tuple_field(field, ty)
40}
41
42pub(crate) fn render_resolution<'a>(
43 ctx: RenderContext<'a>,
44 local_name: String,
45 resolution: &ScopeDef,
46) -> Option<CompletionItem> {
47 Render::new(ctx).render_resolution(local_name, resolution)
30} 48}
31 49
32/// Interface for data and methods required for items rendering. 50/// Interface for data and methods required for items rendering.
@@ -74,12 +92,18 @@ impl<'a> RenderContext<'a> {
74 } 92 }
75} 93}
76 94
95/// Generic renderer for completion items.
96#[derive(Debug)]
97struct Render<'a> {
98 ctx: RenderContext<'a>,
99}
100
77impl<'a> Render<'a> { 101impl<'a> Render<'a> {
78 pub(crate) fn new(ctx: RenderContext<'a>) -> Render<'a> { 102 fn new(ctx: RenderContext<'a>) -> Render<'a> {
79 Render { ctx } 103 Render { ctx }
80 } 104 }
81 105
82 pub(crate) fn add_field(&mut self, field: hir::Field, ty: &Type) -> CompletionItem { 106 fn add_field(&mut self, field: hir::Field, ty: &Type) -> CompletionItem {
83 let is_deprecated = self.ctx.is_deprecated(field); 107 let is_deprecated = self.ctx.is_deprecated(field);
84 let name = field.name(self.ctx.db()); 108 let name = field.name(self.ctx.db());
85 let mut item = CompletionItem::new( 109 let mut item = CompletionItem::new(
@@ -96,17 +120,17 @@ impl<'a> Render<'a> {
96 item = item.set_score(score); 120 item = item.set_score(score);
97 } 121 }
98 122
99 return item.build(); 123 item.build()
100 } 124 }
101 125
102 pub(crate) fn add_tuple_field(&mut self, field: usize, ty: &Type) -> CompletionItem { 126 fn add_tuple_field(&mut self, field: usize, ty: &Type) -> CompletionItem {
103 CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), field.to_string()) 127 CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), field.to_string())
104 .kind(CompletionItemKind::Field) 128 .kind(CompletionItemKind::Field)
105 .detail(ty.display(self.ctx.db()).to_string()) 129 .detail(ty.display(self.ctx.db()).to_string())
106 .build() 130 .build()
107 } 131 }
108 132
109 pub(crate) fn render_resolution( 133 fn render_resolution(
110 self, 134 self,
111 local_name: String, 135 local_name: String,
112 resolution: &ScopeDef, 136 resolution: &ScopeDef,
@@ -120,15 +144,15 @@ impl<'a> Render<'a> {
120 144
121 let kind = match resolution { 145 let kind = match resolution {
122 ScopeDef::ModuleDef(Function(func)) => { 146 ScopeDef::ModuleDef(Function(func)) => {
123 let item = FunctionRender::new(self.ctx, Some(local_name), *func).render(); 147 let item = render_fn(self.ctx, Some(local_name), *func);
124 return Some(item); 148 return Some(item);
125 } 149 }
126 ScopeDef::ModuleDef(EnumVariant(var)) => { 150 ScopeDef::ModuleDef(EnumVariant(var)) => {
127 let item = EnumVariantRender::new(self.ctx, Some(local_name), *var, None).render(); 151 let item = render_enum_variant(self.ctx, Some(local_name), *var, None);
128 return Some(item); 152 return Some(item);
129 } 153 }
130 ScopeDef::MacroDef(mac) => { 154 ScopeDef::MacroDef(mac) => {
131 let item = MacroRender::new(self.ctx, local_name, *mac).render(); 155 let item = render_macro(self.ctx, local_name, *mac);
132 return item; 156 return item;
133 } 157 }
134 158
diff --git a/crates/completion/src/render/const_.rs b/crates/completion/src/render/const_.rs
index 3a1954211..039bdabc0 100644
--- a/crates/completion/src/render/const_.rs
+++ b/crates/completion/src/render/const_.rs
@@ -11,20 +11,27 @@ use crate::{
11 render::RenderContext, 11 render::RenderContext,
12}; 12};
13 13
14pub(crate) fn render_const<'a>(
15 ctx: RenderContext<'a>,
16 const_: hir::Const,
17) -> Option<CompletionItem> {
18 ConstRender::new(ctx, const_).render()
19}
20
14#[derive(Debug)] 21#[derive(Debug)]
15pub(crate) struct ConstRender<'a> { 22struct ConstRender<'a> {
16 ctx: RenderContext<'a>, 23 ctx: RenderContext<'a>,
17 const_: hir::Const, 24 const_: hir::Const,
18 ast_node: Const, 25 ast_node: Const,
19} 26}
20 27
21impl<'a> ConstRender<'a> { 28impl<'a> ConstRender<'a> {
22 pub(crate) fn new(ctx: RenderContext<'a>, const_: hir::Const) -> ConstRender<'a> { 29 fn new(ctx: RenderContext<'a>, const_: hir::Const) -> ConstRender<'a> {
23 let ast_node = const_.source(ctx.db()).value; 30 let ast_node = const_.source(ctx.db()).value;
24 ConstRender { ctx, const_, ast_node } 31 ConstRender { ctx, const_, ast_node }
25 } 32 }
26 33
27 pub(crate) fn render(self) -> Option<CompletionItem> { 34 fn render(self) -> Option<CompletionItem> {
28 let name = self.name()?; 35 let name = self.name()?;
29 let detail = self.detail(); 36 let detail = self.detail();
30 37
diff --git a/crates/completion/src/render/enum_variant.rs b/crates/completion/src/render/enum_variant.rs
index f8b151318..fd412ed0e 100644
--- a/crates/completion/src/render/enum_variant.rs
+++ b/crates/completion/src/render/enum_variant.rs
@@ -9,8 +9,17 @@ use crate::{
9 render::{builder_ext::Params, RenderContext}, 9 render::{builder_ext::Params, RenderContext},
10}; 10};
11 11
12pub(crate) fn render_enum_variant<'a>(
13 ctx: RenderContext<'a>,
14 local_name: Option<String>,
15 variant: hir::EnumVariant,
16 path: Option<ModPath>,
17) -> CompletionItem {
18 EnumVariantRender::new(ctx, local_name, variant, path).render()
19}
20
12#[derive(Debug)] 21#[derive(Debug)]
13pub(crate) struct EnumVariantRender<'a> { 22struct EnumVariantRender<'a> {
14 ctx: RenderContext<'a>, 23 ctx: RenderContext<'a>,
15 name: String, 24 name: String,
16 variant: hir::EnumVariant, 25 variant: hir::EnumVariant,
@@ -21,7 +30,7 @@ pub(crate) struct EnumVariantRender<'a> {
21} 30}
22 31
23impl<'a> EnumVariantRender<'a> { 32impl<'a> EnumVariantRender<'a> {
24 pub(crate) fn new( 33 fn new(
25 ctx: RenderContext<'a>, 34 ctx: RenderContext<'a>,
26 local_name: Option<String>, 35 local_name: Option<String>,
27 variant: hir::EnumVariant, 36 variant: hir::EnumVariant,
@@ -51,7 +60,7 @@ impl<'a> EnumVariantRender<'a> {
51 } 60 }
52 } 61 }
53 62
54 pub(crate) fn render(self) -> CompletionItem { 63 fn render(self) -> CompletionItem {
55 let mut builder = CompletionItem::new( 64 let mut builder = CompletionItem::new(
56 CompletionKind::Reference, 65 CompletionKind::Reference,
57 self.ctx.source_range(), 66 self.ctx.source_range(),
diff --git a/crates/completion/src/render/function.rs b/crates/completion/src/render/function.rs
index 9d9bbe309..4fa6eafd7 100644
--- a/crates/completion/src/render/function.rs
+++ b/crates/completion/src/render/function.rs
@@ -8,8 +8,16 @@ use crate::{
8 render::{builder_ext::Params, RenderContext}, 8 render::{builder_ext::Params, RenderContext},
9}; 9};
10 10
11pub(crate) fn render_fn<'a>(
12 ctx: RenderContext<'a>,
13 local_name: Option<String>,
14 fn_: hir::Function,
15) -> CompletionItem {
16 FunctionRender::new(ctx, local_name, fn_).render()
17}
18
11#[derive(Debug)] 19#[derive(Debug)]
12pub(crate) struct FunctionRender<'a> { 20struct FunctionRender<'a> {
13 ctx: RenderContext<'a>, 21 ctx: RenderContext<'a>,
14 name: String, 22 name: String,
15 fn_: hir::Function, 23 fn_: hir::Function,
@@ -17,7 +25,7 @@ pub(crate) struct FunctionRender<'a> {
17} 25}
18 26
19impl<'a> FunctionRender<'a> { 27impl<'a> FunctionRender<'a> {
20 pub(crate) fn new( 28 fn new(
21 ctx: RenderContext<'a>, 29 ctx: RenderContext<'a>,
22 local_name: Option<String>, 30 local_name: Option<String>,
23 fn_: hir::Function, 31 fn_: hir::Function,
@@ -28,7 +36,7 @@ impl<'a> FunctionRender<'a> {
28 FunctionRender { ctx, name, fn_, ast_node } 36 FunctionRender { ctx, name, fn_, ast_node }
29 } 37 }
30 38
31 pub(crate) fn render(self) -> CompletionItem { 39 fn render(self) -> CompletionItem {
32 let params = self.params(); 40 let params = self.params();
33 CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone()) 41 CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone())
34 .kind(self.kind()) 42 .kind(self.kind())
diff --git a/crates/completion/src/render/macro_.rs b/crates/completion/src/render/macro_.rs
index 066185559..96be59cc3 100644
--- a/crates/completion/src/render/macro_.rs
+++ b/crates/completion/src/render/macro_.rs
@@ -9,8 +9,16 @@ use crate::{
9 render::RenderContext, 9 render::RenderContext,
10}; 10};
11 11
12pub(crate) fn render_macro<'a>(
13 ctx: RenderContext<'a>,
14 name: String,
15 macro_: hir::MacroDef,
16) -> Option<CompletionItem> {
17 MacroRender::new(ctx, name, macro_).render()
18}
19
12#[derive(Debug)] 20#[derive(Debug)]
13pub(crate) struct MacroRender<'a> { 21struct MacroRender<'a> {
14 ctx: RenderContext<'a>, 22 ctx: RenderContext<'a>,
15 name: String, 23 name: String,
16 macro_: hir::MacroDef, 24 macro_: hir::MacroDef,
@@ -20,11 +28,7 @@ pub(crate) struct MacroRender<'a> {
20} 28}
21 29
22impl<'a> MacroRender<'a> { 30impl<'a> MacroRender<'a> {
23 pub(crate) fn new( 31 fn new(ctx: RenderContext<'a>, name: String, macro_: hir::MacroDef) -> MacroRender<'a> {
24 ctx: RenderContext<'a>,
25 name: String,
26 macro_: hir::MacroDef,
27 ) -> MacroRender<'a> {
28 let docs = ctx.docs(macro_); 32 let docs = ctx.docs(macro_);
29 let docs_str = docs.as_ref().map_or("", |s| s.as_str()); 33 let docs_str = docs.as_ref().map_or("", |s| s.as_str());
30 let (bra, ket) = guess_macro_braces(&name, docs_str); 34 let (bra, ket) = guess_macro_braces(&name, docs_str);
@@ -32,7 +36,7 @@ impl<'a> MacroRender<'a> {
32 MacroRender { ctx, name, macro_, docs, bra, ket } 36 MacroRender { ctx, name, macro_, docs, bra, ket }
33 } 37 }
34 38
35 pub(crate) fn render(&self) -> Option<CompletionItem> { 39 fn render(&self) -> Option<CompletionItem> {
36 // FIXME: Currently proc-macro do not have ast-node, 40 // FIXME: Currently proc-macro do not have ast-node,
37 // such that it does not have source 41 // such that it does not have source
38 if self.macro_.is_proc_macro() { 42 if self.macro_.is_proc_macro() {
diff --git a/crates/completion/src/render/type_alias.rs b/crates/completion/src/render/type_alias.rs
index 50a6d06c2..9605c7fa9 100644
--- a/crates/completion/src/render/type_alias.rs
+++ b/crates/completion/src/render/type_alias.rs
@@ -11,20 +11,27 @@ use crate::{
11 render::RenderContext, 11 render::RenderContext,
12}; 12};
13 13
14pub(crate) fn render_type_alias<'a>(
15 ctx: RenderContext<'a>,
16 type_alias: hir::TypeAlias,
17) -> Option<CompletionItem> {
18 TypeAliasRender::new(ctx, type_alias).render()
19}
20
14#[derive(Debug)] 21#[derive(Debug)]
15pub(crate) struct TypeAliasRender<'a> { 22struct TypeAliasRender<'a> {
16 ctx: RenderContext<'a>, 23 ctx: RenderContext<'a>,
17 type_alias: hir::TypeAlias, 24 type_alias: hir::TypeAlias,
18 ast_node: TypeAlias, 25 ast_node: TypeAlias,
19} 26}
20 27
21impl<'a> TypeAliasRender<'a> { 28impl<'a> TypeAliasRender<'a> {
22 pub(crate) fn new(ctx: RenderContext<'a>, type_alias: hir::TypeAlias) -> TypeAliasRender<'a> { 29 fn new(ctx: RenderContext<'a>, type_alias: hir::TypeAlias) -> TypeAliasRender<'a> {
23 let ast_node = type_alias.source(ctx.db()).value; 30 let ast_node = type_alias.source(ctx.db()).value;
24 TypeAliasRender { ctx, type_alias, ast_node } 31 TypeAliasRender { ctx, type_alias, ast_node }
25 } 32 }
26 33
27 pub(crate) fn render(self) -> Option<CompletionItem> { 34 fn render(self) -> Option<CompletionItem> {
28 let name = self.name()?; 35 let name = self.name()?;
29 let detail = self.detail(); 36 let detail = self.detail();
30 37