Make the result of Const, FunctionRender and TypeAliasRender constructors optional

They use source() which now returns an Option so they need to too.
This commit is contained in:
Nick Spain 2021-01-01 17:26:39 +11:00
parent 3f1b3df65b
commit 7bfec89cf9
5 changed files with 17 additions and 20 deletions

View file

@ -106,8 +106,9 @@ impl Completions {
func: hir::Function,
local_name: Option<String>,
) {
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(

View file

@ -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

View file

@ -15,7 +15,7 @@ pub(crate) fn render_const<'a>(
ctx: RenderContext<'a>,
const_: hir::Const,
) -> Option<CompletionItem> {
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<ConstRender<'a>> {
let ast_node = const_.source(ctx.db())?.value;
Some(ConstRender { ctx, const_, ast_node })
}
fn render(self) -> Option<CompletionItem> {

View file

@ -14,9 +14,9 @@ pub(crate) fn render_fn<'a>(
import_to_add: Option<ImportEdit>,
local_name: Option<String>,
fn_: hir::Function,
) -> CompletionItem {
) -> Option<CompletionItem> {
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<String>,
fn_: hir::Function,
) -> FunctionRender<'a> {
) -> Option<FunctionRender<'a>> {
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<ImportEdit>) -> CompletionItem {

View file

@ -15,7 +15,7 @@ pub(crate) fn render_type_alias<'a>(
ctx: RenderContext<'a>,
type_alias: hir::TypeAlias,
) -> Option<CompletionItem> {
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<TypeAliasRender<'a>> {
let ast_node = type_alias.source(ctx.db())?.value;
Some(TypeAliasRender { ctx, type_alias, ast_node })
}
fn render(self) -> Option<CompletionItem> {