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

@ -20,7 +20,7 @@ use test_utils::mark;
use crate::{
item::Builder,
render::{EnumVariantRender, FunctionRender, MacroRender},
render::{ConstRender, EnumVariantRender, FunctionRender, MacroRender},
CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, CompletionScore,
RootDatabase,
};
@ -194,7 +194,6 @@ impl Completions {
Some(it) => it,
None => return,
};
if let Some(item) = MacroRender::new(ctx.into(), name, macro_).render() {
self.add(item);
}
@ -207,24 +206,13 @@ impl Completions {
local_name: Option<String>,
) {
let item = FunctionRender::new(ctx.into(), local_name, func).render();
self.add(item)
}
pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {
let ast_node = constant.source(ctx.db).value;
let name = match ast_node.name() {
Some(name) => name,
_ => return,
};
let detail = const_label(&ast_node);
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string())
.kind(CompletionItemKind::Const)
.set_documentation(constant.docs(ctx.db))
.set_deprecated(is_deprecated(constant, ctx.db))
.detail(detail)
.add_to(self);
if let Some(item) = ConstRender::new(ctx.into(), constant).render() {
self.add(item);
}
}
pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {

View file

@ -5,15 +5,17 @@ mod macro_;
mod function;
mod builder_ext;
mod enum_variant;
mod const_;
use hir::HasAttrs;
use hir::{Documentation, HasAttrs};
use ide_db::RootDatabase;
use syntax::TextRange;
use crate::{config::SnippetCap, CompletionContext};
pub(crate) use crate::render::{
enum_variant::EnumVariantRender, function::FunctionRender, macro_::MacroRender,
const_::ConstRender, enum_variant::EnumVariantRender, function::FunctionRender,
macro_::MacroRender,
};
#[derive(Debug)]
@ -41,6 +43,10 @@ impl<'a> RenderContext<'a> {
pub fn is_deprecated(&self, node: impl HasAttrs) -> bool {
node.attrs(self.db()).by_key("deprecated").exists()
}
pub fn docs(&self, node: impl HasAttrs) -> Option<Documentation> {
node.docs(self.db())
}
}
impl<'a> From<&'a CompletionContext<'a>> for RenderContext<'a> {

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);