Add ConstRender

This commit is contained in:
Igor Aleksanov 2020-11-01 12:59:43 +03:00
parent fc8a1cd800
commit 944ccf6075
5 changed files with 65 additions and 28 deletions

View file

@ -0,0 +1,47 @@
use hir::HasSource;
use syntax::{
ast::{Const, NameOwner},
display::const_label,
};
use crate::{
item::{CompletionItem, CompletionItemKind, CompletionKind},
render::RenderContext,
};
#[derive(Debug)]
pub(crate) 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> {
let ast_node = const_.source(ctx.db()).value;
ConstRender { ctx, const_, ast_node }
}
pub(crate) fn render(self) -> Option<CompletionItem> {
let name = self.name()?;
let detail = self.detail();
let item = CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name)
.kind(CompletionItemKind::Const)
.set_documentation(self.ctx.docs(self.const_))
.set_deprecated(self.ctx.is_deprecated(self.const_))
.detail(detail)
.build();
Some(item)
}
fn name(&self) -> Option<String> {
let ast_node = self.const_.source(self.ctx.db()).value;
ast_node.name().map(|name| name.text().to_string())
}
fn detail(&self) -> String {
const_label(&self.ast_node)
}
}

View file

@ -1,4 +1,4 @@
use hir::{Documentation, HasAttrs, HasSource, Type};
use hir::{HasSource, Type};
use syntax::{ast::Fn, display::function_declaration};
use crate::{
@ -30,7 +30,7 @@ impl<'a> FunctionRender<'a> {
let params = self.params();
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone())
.kind(self.kind())
.set_documentation(self.docs())
.set_documentation(self.ctx.docs(self.fn_))
.set_deprecated(self.ctx.is_deprecated(self.fn_))
.detail(self.detail())
.add_call_parens(self.ctx.completion, self.name, params)
@ -45,8 +45,8 @@ impl<'a> FunctionRender<'a> {
if let Some(derefed_ty) = ty.remove_ref() {
for (name, local) in self.ctx.completion.locals.iter() {
if name == arg && local.ty(self.ctx.db()) == derefed_ty {
return (if ty.is_mutable_reference() { "&mut " } else { "&" }).to_string()
+ &arg.to_string();
let mutability = if ty.is_mutable_reference() { "&mut " } else { "&" };
return format!("{}{}", mutability, arg);
}
}
}
@ -80,8 +80,4 @@ impl<'a> FunctionRender<'a> {
CompletionItemKind::Function
}
}
fn docs(&self) -> Option<Documentation> {
self.fn_.docs(self.ctx.db())
}
}

View file

@ -1,4 +1,4 @@
use hir::{Documentation, HasAttrs, HasSource};
use hir::{Documentation, HasSource};
use syntax::display::macro_label;
use test_utils::mark;
@ -23,7 +23,7 @@ impl<'a> MacroRender<'a> {
name: String,
macro_: hir::MacroDef,
) -> MacroRender<'a> {
let docs = macro_.docs(ctx.db());
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);