diff options
author | Igor Aleksanov <[email protected]> | 2020-11-03 07:33:13 +0000 |
---|---|---|
committer | Igor Aleksanov <[email protected]> | 2020-11-03 07:33:13 +0000 |
commit | 4d333ebb6372c135e5a723da899528cc22d07faa (patch) | |
tree | 7ab4c7f113a1686d7378502e2685d3529cb6f5ba /crates/completion/src/render | |
parent | caf0fa20a7d895612ceee1948d6a9895e53bee4a (diff) |
Get rid of do-er antipattern
Diffstat (limited to 'crates/completion/src/render')
-rw-r--r-- | crates/completion/src/render/const_.rs | 13 | ||||
-rw-r--r-- | crates/completion/src/render/enum_variant.rs | 15 | ||||
-rw-r--r-- | crates/completion/src/render/function.rs | 14 | ||||
-rw-r--r-- | crates/completion/src/render/macro_.rs | 18 | ||||
-rw-r--r-- | crates/completion/src/render/type_alias.rs | 13 |
5 files changed, 54 insertions, 19 deletions
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 | ||
14 | pub(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)] |
15 | pub(crate) struct ConstRender<'a> { | 22 | struct 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 | ||
21 | impl<'a> ConstRender<'a> { | 28 | impl<'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 | ||
12 | pub(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)] |
13 | pub(crate) struct EnumVariantRender<'a> { | 22 | struct 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 | ||
23 | impl<'a> EnumVariantRender<'a> { | 32 | impl<'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 | ||
11 | pub(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)] |
12 | pub(crate) struct FunctionRender<'a> { | 20 | struct 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 | ||
19 | impl<'a> FunctionRender<'a> { | 27 | impl<'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 | ||
12 | pub(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)] |
13 | pub(crate) struct MacroRender<'a> { | 21 | struct 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 | ||
22 | impl<'a> MacroRender<'a> { | 30 | impl<'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 | ||
14 | pub(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)] |
15 | pub(crate) struct TypeAliasRender<'a> { | 22 | struct 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 | ||
21 | impl<'a> TypeAliasRender<'a> { | 28 | impl<'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 | ||