From 944ccf60758305a1b15224defe622cfca6939aaa Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Sun, 1 Nov 2020 12:59:43 +0300 Subject: Add ConstRender --- crates/completion/src/completions.rs | 20 +++----------- crates/completion/src/render.rs | 10 +++++-- crates/completion/src/render/const_.rs | 47 ++++++++++++++++++++++++++++++++ crates/completion/src/render/function.rs | 12 +++----- crates/completion/src/render/macro_.rs | 4 +-- 5 files changed, 65 insertions(+), 28 deletions(-) create mode 100644 crates/completion/src/render/const_.rs (limited to 'crates') diff --git a/crates/completion/src/completions.rs b/crates/completion/src/completions.rs index 1ca5cf33d..d8dc1b1c3 100644 --- a/crates/completion/src/completions.rs +++ b/crates/completion/src/completions.rs @@ -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, ) { 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) { diff --git a/crates/completion/src/render.rs b/crates/completion/src/render.rs index 66eb753b1..c614a7172 100644 --- a/crates/completion/src/render.rs +++ b/crates/completion/src/render.rs @@ -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 { + node.docs(self.db()) + } } impl<'a> From<&'a CompletionContext<'a>> for RenderContext<'a> { diff --git a/crates/completion/src/render/const_.rs b/crates/completion/src/render/const_.rs new file mode 100644 index 000000000..d88bfa07f --- /dev/null +++ b/crates/completion/src/render/const_.rs @@ -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 { + 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 { + 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) + } +} diff --git a/crates/completion/src/render/function.rs b/crates/completion/src/render/function.rs index 16f15e22c..d22081236 100644 --- a/crates/completion/src/render/function.rs +++ b/crates/completion/src/render/function.rs @@ -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 { - self.fn_.docs(self.ctx.db()) - } } diff --git a/crates/completion/src/render/macro_.rs b/crates/completion/src/render/macro_.rs index bcf94f47e..0ad8c03b3 100644 --- a/crates/completion/src/render/macro_.rs +++ b/crates/completion/src/render/macro_.rs @@ -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); -- cgit v1.2.3