From 4d333ebb6372c135e5a723da899528cc22d07faa Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Tue, 3 Nov 2020 10:33:13 +0300 Subject: Get rid of do-er antipattern --- crates/completion/src/completions.rs | 22 +++++------- crates/completion/src/render.rs | 50 ++++++++++++++++++++-------- crates/completion/src/render/const_.rs | 13 ++++++-- crates/completion/src/render/enum_variant.rs | 15 +++++++-- crates/completion/src/render/function.rs | 14 ++++++-- crates/completion/src/render/macro_.rs | 18 ++++++---- crates/completion/src/render/type_alias.rs | 13 ++++++-- 7 files changed, 100 insertions(+), 45 deletions(-) (limited to 'crates/completion/src') 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 { } pub(crate) fn add_field(&mut self, ctx: &CompletionContext, field: hir::Field, ty: &Type) { - let item = Render::new(RenderContext::new(ctx)).add_field(field, ty); + let item = render_field(RenderContext::new(ctx), field, ty); self.add(item); } pub(crate) fn add_tuple_field(&mut self, ctx: &CompletionContext, field: usize, ty: &Type) { - let item = Render::new(RenderContext::new(ctx)).add_tuple_field(field, ty); + let item = render_tuple_field(RenderContext::new(ctx), field, ty); self.add(item); } @@ -67,9 +67,7 @@ impl Completions { local_name: String, resolution: &ScopeDef, ) { - if let Some(item) = - Render::new(RenderContext::new(ctx)).render_resolution(local_name, resolution) - { + if let Some(item) = render_resolution(RenderContext::new(ctx), local_name, resolution) { self.add(item); } } @@ -84,7 +82,7 @@ impl Completions { Some(it) => it, None => return, }; - if let Some(item) = MacroRender::new(RenderContext::new(ctx), name, macro_).render() { + if let Some(item) = render_macro(RenderContext::new(ctx), name, macro_) { self.add(item); } } @@ -95,18 +93,18 @@ impl Completions { func: hir::Function, local_name: Option, ) { - let item = FunctionRender::new(RenderContext::new(ctx), local_name, func).render(); + let item = render_fn(RenderContext::new(ctx), local_name, func); self.add(item) } pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) { - if let Some(item) = ConstRender::new(RenderContext::new(ctx), constant).render() { + if let Some(item) = render_const(RenderContext::new(ctx), constant) { self.add(item); } } pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) { - if let Some(item) = TypeAliasRender::new(RenderContext::new(ctx), type_alias).render() { + if let Some(item) = render_type_alias(RenderContext::new(ctx), type_alias) { self.add(item) } } @@ -117,8 +115,7 @@ impl Completions { variant: hir::EnumVariant, path: ModPath, ) { - let item = - EnumVariantRender::new(RenderContext::new(ctx), None, variant, Some(path)).render(); + let item = render_enum_variant(RenderContext::new(ctx), None, variant, Some(path)); self.add(item); } @@ -128,8 +125,7 @@ impl Completions { variant: hir::EnumVariant, local_name: Option, ) { - let item = - EnumVariantRender::new(RenderContext::new(ctx), local_name, variant, None).render(); + let item = render_enum_variant(RenderContext::new(ctx), local_name, variant, None); self.add(item); } } 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::{ }; pub(crate) use crate::render::{ - const_::ConstRender, enum_variant::EnumVariantRender, function::FunctionRender, - macro_::MacroRender, type_alias::TypeAliasRender, + const_::render_const, enum_variant::render_enum_variant, function::render_fn, + macro_::render_macro, type_alias::render_type_alias, }; -/// Generic renderer for completion items. -#[derive(Debug)] -pub(crate) struct Render<'a> { +pub(crate) fn render_field<'a>( + ctx: RenderContext<'a>, + field: hir::Field, + ty: &Type, +) -> CompletionItem { + Render::new(ctx).add_field(field, ty) +} + +pub(crate) fn render_tuple_field<'a>( ctx: RenderContext<'a>, + field: usize, + ty: &Type, +) -> CompletionItem { + Render::new(ctx).add_tuple_field(field, ty) +} + +pub(crate) fn render_resolution<'a>( + ctx: RenderContext<'a>, + local_name: String, + resolution: &ScopeDef, +) -> Option { + Render::new(ctx).render_resolution(local_name, resolution) } /// Interface for data and methods required for items rendering. @@ -74,12 +92,18 @@ impl<'a> RenderContext<'a> { } } +/// Generic renderer for completion items. +#[derive(Debug)] +struct Render<'a> { + ctx: RenderContext<'a>, +} + impl<'a> Render<'a> { - pub(crate) fn new(ctx: RenderContext<'a>) -> Render<'a> { + fn new(ctx: RenderContext<'a>) -> Render<'a> { Render { ctx } } - pub(crate) fn add_field(&mut self, field: hir::Field, ty: &Type) -> CompletionItem { + fn add_field(&mut self, field: hir::Field, ty: &Type) -> CompletionItem { let is_deprecated = self.ctx.is_deprecated(field); let name = field.name(self.ctx.db()); let mut item = CompletionItem::new( @@ -96,17 +120,17 @@ impl<'a> Render<'a> { item = item.set_score(score); } - return item.build(); + item.build() } - pub(crate) fn add_tuple_field(&mut self, field: usize, ty: &Type) -> CompletionItem { + fn add_tuple_field(&mut self, field: usize, ty: &Type) -> CompletionItem { CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), field.to_string()) .kind(CompletionItemKind::Field) .detail(ty.display(self.ctx.db()).to_string()) .build() } - pub(crate) fn render_resolution( + fn render_resolution( self, local_name: String, resolution: &ScopeDef, @@ -120,15 +144,15 @@ impl<'a> Render<'a> { let kind = match resolution { ScopeDef::ModuleDef(Function(func)) => { - let item = FunctionRender::new(self.ctx, Some(local_name), *func).render(); + let item = render_fn(self.ctx, Some(local_name), *func); return Some(item); } ScopeDef::ModuleDef(EnumVariant(var)) => { - let item = EnumVariantRender::new(self.ctx, Some(local_name), *var, None).render(); + let item = render_enum_variant(self.ctx, Some(local_name), *var, None); return Some(item); } ScopeDef::MacroDef(mac) => { - let item = MacroRender::new(self.ctx, local_name, *mac).render(); + let item = render_macro(self.ctx, local_name, *mac); return item; } 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::{ render::RenderContext, }; +pub(crate) fn render_const<'a>( + ctx: RenderContext<'a>, + const_: hir::Const, +) -> Option { + ConstRender::new(ctx, const_).render() +} + #[derive(Debug)] -pub(crate) struct ConstRender<'a> { +struct ConstRender<'a> { ctx: RenderContext<'a>, const_: hir::Const, ast_node: Const, } impl<'a> ConstRender<'a> { - pub(crate) fn new(ctx: RenderContext<'a>, const_: hir::Const) -> ConstRender<'a> { + fn new(ctx: RenderContext<'a>, const_: hir::Const) -> ConstRender<'a> { let ast_node = const_.source(ctx.db()).value; ConstRender { ctx, const_, ast_node } } - pub(crate) fn render(self) -> Option { + fn render(self) -> Option { let name = self.name()?; let detail = self.detail(); 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::{ render::{builder_ext::Params, RenderContext}, }; +pub(crate) fn render_enum_variant<'a>( + ctx: RenderContext<'a>, + local_name: Option, + variant: hir::EnumVariant, + path: Option, +) -> CompletionItem { + EnumVariantRender::new(ctx, local_name, variant, path).render() +} + #[derive(Debug)] -pub(crate) struct EnumVariantRender<'a> { +struct EnumVariantRender<'a> { ctx: RenderContext<'a>, name: String, variant: hir::EnumVariant, @@ -21,7 +30,7 @@ pub(crate) struct EnumVariantRender<'a> { } impl<'a> EnumVariantRender<'a> { - pub(crate) fn new( + fn new( ctx: RenderContext<'a>, local_name: Option, variant: hir::EnumVariant, @@ -51,7 +60,7 @@ impl<'a> EnumVariantRender<'a> { } } - pub(crate) fn render(self) -> CompletionItem { + fn render(self) -> CompletionItem { let mut builder = CompletionItem::new( CompletionKind::Reference, 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::{ render::{builder_ext::Params, RenderContext}, }; +pub(crate) fn render_fn<'a>( + ctx: RenderContext<'a>, + local_name: Option, + fn_: hir::Function, +) -> CompletionItem { + FunctionRender::new(ctx, local_name, fn_).render() +} + #[derive(Debug)] -pub(crate) struct FunctionRender<'a> { +struct FunctionRender<'a> { ctx: RenderContext<'a>, name: String, fn_: hir::Function, @@ -17,7 +25,7 @@ pub(crate) struct FunctionRender<'a> { } impl<'a> FunctionRender<'a> { - pub(crate) fn new( + fn new( ctx: RenderContext<'a>, local_name: Option, fn_: hir::Function, @@ -28,7 +36,7 @@ impl<'a> FunctionRender<'a> { FunctionRender { ctx, name, fn_, ast_node } } - pub(crate) fn render(self) -> CompletionItem { + fn render(self) -> CompletionItem { let params = self.params(); CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone()) .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::{ render::RenderContext, }; +pub(crate) fn render_macro<'a>( + ctx: RenderContext<'a>, + name: String, + macro_: hir::MacroDef, +) -> Option { + MacroRender::new(ctx, name, macro_).render() +} + #[derive(Debug)] -pub(crate) struct MacroRender<'a> { +struct MacroRender<'a> { ctx: RenderContext<'a>, name: String, macro_: hir::MacroDef, @@ -20,11 +28,7 @@ pub(crate) struct MacroRender<'a> { } impl<'a> MacroRender<'a> { - pub(crate) fn new( - ctx: RenderContext<'a>, - name: String, - macro_: hir::MacroDef, - ) -> MacroRender<'a> { + fn new(ctx: RenderContext<'a>, name: String, macro_: hir::MacroDef) -> MacroRender<'a> { let docs = ctx.docs(macro_); let docs_str = docs.as_ref().map_or("", |s| s.as_str()); let (bra, ket) = guess_macro_braces(&name, docs_str); @@ -32,7 +36,7 @@ impl<'a> MacroRender<'a> { MacroRender { ctx, name, macro_, docs, bra, ket } } - pub(crate) fn render(&self) -> Option { + fn render(&self) -> Option { // FIXME: Currently proc-macro do not have ast-node, // such that it does not have source 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::{ render::RenderContext, }; +pub(crate) fn render_type_alias<'a>( + ctx: RenderContext<'a>, + type_alias: hir::TypeAlias, +) -> Option { + TypeAliasRender::new(ctx, type_alias).render() +} + #[derive(Debug)] -pub(crate) struct TypeAliasRender<'a> { +struct TypeAliasRender<'a> { ctx: RenderContext<'a>, type_alias: hir::TypeAlias, ast_node: TypeAlias, } impl<'a> TypeAliasRender<'a> { - pub(crate) fn new(ctx: RenderContext<'a>, type_alias: hir::TypeAlias) -> TypeAliasRender<'a> { + fn new(ctx: RenderContext<'a>, type_alias: hir::TypeAlias) -> TypeAliasRender<'a> { let ast_node = type_alias.source(ctx.db()).value; TypeAliasRender { ctx, type_alias, ast_node } } - pub(crate) fn render(self) -> Option { + fn render(self) -> Option { let name = self.name()?; let detail = self.detail(); -- cgit v1.2.3