From 7bfec89cf969aa630f184f7d4a66e8e12a423d2f Mon Sep 17 00:00:00 2001 From: Nick Spain Date: Fri, 1 Jan 2021 17:26:39 +1100 Subject: Make the result of Const, FunctionRender and TypeAliasRender constructors optional They use source() which now returns an Option so they need to too. --- crates/completion/src/completions.rs | 5 +++-- crates/completion/src/render.rs | 3 +-- crates/completion/src/render/const_.rs | 9 ++++----- crates/completion/src/render/function.rs | 11 +++++------ crates/completion/src/render/type_alias.rs | 9 ++++----- 5 files changed, 17 insertions(+), 20 deletions(-) diff --git a/crates/completion/src/completions.rs b/crates/completion/src/completions.rs index d9fe13485..00c9e76f0 100644 --- a/crates/completion/src/completions.rs +++ b/crates/completion/src/completions.rs @@ -106,8 +106,9 @@ impl Completions { func: hir::Function, local_name: Option, ) { - let item = render_fn(RenderContext::new(ctx), None, local_name, func); - self.add(item) + if let Some(item) = render_fn(RenderContext::new(ctx), None, local_name, func) { + self.add(item) + } } pub(crate) fn add_variant_pat( diff --git a/crates/completion/src/render.rs b/crates/completion/src/render.rs index 1ba7201a1..ac0b2a513 100644 --- a/crates/completion/src/render.rs +++ b/crates/completion/src/render.rs @@ -157,8 +157,7 @@ impl<'a> Render<'a> { let kind = match resolution { ScopeDef::ModuleDef(Function(func)) => { - let item = render_fn(self.ctx, import_to_add, Some(local_name), *func); - return Some(item); + return render_fn(self.ctx, import_to_add, Some(local_name), *func); } ScopeDef::ModuleDef(Variant(_)) if self.ctx.completion.is_pat_binding_or_const diff --git a/crates/completion/src/render/const_.rs b/crates/completion/src/render/const_.rs index 648a1afc5..ce924f309 100644 --- a/crates/completion/src/render/const_.rs +++ b/crates/completion/src/render/const_.rs @@ -15,7 +15,7 @@ pub(crate) fn render_const<'a>( ctx: RenderContext<'a>, const_: hir::Const, ) -> Option { - ConstRender::new(ctx, const_).render() + ConstRender::new(ctx, const_)?.render() } #[derive(Debug)] @@ -26,10 +26,9 @@ struct ConstRender<'a> { } impl<'a> ConstRender<'a> { - fn new(ctx: RenderContext<'a>, const_: hir::Const) -> ConstRender<'a> { - #[allow(deprecated)] - let ast_node = const_.source_old(ctx.db()).value; - ConstRender { ctx, const_, ast_node } + fn new(ctx: RenderContext<'a>, const_: hir::Const) -> Option> { + let ast_node = const_.source(ctx.db())?.value; + Some(ConstRender { ctx, const_, ast_node }) } fn render(self) -> Option { diff --git a/crates/completion/src/render/function.rs b/crates/completion/src/render/function.rs index 4c8996204..081be14f4 100644 --- a/crates/completion/src/render/function.rs +++ b/crates/completion/src/render/function.rs @@ -14,9 +14,9 @@ pub(crate) fn render_fn<'a>( import_to_add: Option, local_name: Option, fn_: hir::Function, -) -> CompletionItem { +) -> Option { let _p = profile::span("render_fn"); - FunctionRender::new(ctx, local_name, fn_).render(import_to_add) + Some(FunctionRender::new(ctx, local_name, fn_)?.render(import_to_add)) } #[derive(Debug)] @@ -32,12 +32,11 @@ impl<'a> FunctionRender<'a> { ctx: RenderContext<'a>, local_name: Option, fn_: hir::Function, - ) -> FunctionRender<'a> { + ) -> Option> { let name = local_name.unwrap_or_else(|| fn_.name(ctx.db()).to_string()); - #[allow(deprecated)] - let ast_node = fn_.source_old(ctx.db()).value; + let ast_node = fn_.source(ctx.db())?.value; - FunctionRender { ctx, name, func: fn_, ast_node } + Some(FunctionRender { ctx, name, func: fn_, ast_node }) } fn render(self, import_to_add: Option) -> CompletionItem { diff --git a/crates/completion/src/render/type_alias.rs b/crates/completion/src/render/type_alias.rs index 276090015..69b445b9c 100644 --- a/crates/completion/src/render/type_alias.rs +++ b/crates/completion/src/render/type_alias.rs @@ -15,7 +15,7 @@ pub(crate) fn render_type_alias<'a>( ctx: RenderContext<'a>, type_alias: hir::TypeAlias, ) -> Option { - TypeAliasRender::new(ctx, type_alias).render() + TypeAliasRender::new(ctx, type_alias)?.render() } #[derive(Debug)] @@ -26,10 +26,9 @@ struct TypeAliasRender<'a> { } impl<'a> TypeAliasRender<'a> { - fn new(ctx: RenderContext<'a>, type_alias: hir::TypeAlias) -> TypeAliasRender<'a> { - #[allow(deprecated)] - let ast_node = type_alias.source_old(ctx.db()).value; - TypeAliasRender { ctx, type_alias, ast_node } + fn new(ctx: RenderContext<'a>, type_alias: hir::TypeAlias) -> Option> { + let ast_node = type_alias.source(ctx.db())?.value; + Some(TypeAliasRender { ctx, type_alias, ast_node }) } fn render(self) -> Option { -- cgit v1.2.3