aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/completion/src/completions.rs20
-rw-r--r--crates/completion/src/render.rs10
-rw-r--r--crates/completion/src/render/const_.rs47
-rw-r--r--crates/completion/src/render/function.rs12
-rw-r--r--crates/completion/src/render/macro_.rs4
5 files changed, 65 insertions, 28 deletions
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;
20 20
21use crate::{ 21use crate::{
22 item::Builder, 22 item::Builder,
23 render::{EnumVariantRender, FunctionRender, MacroRender}, 23 render::{ConstRender, EnumVariantRender, FunctionRender, MacroRender},
24 CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, CompletionScore, 24 CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, CompletionScore,
25 RootDatabase, 25 RootDatabase,
26}; 26};
@@ -194,7 +194,6 @@ impl Completions {
194 Some(it) => it, 194 Some(it) => it,
195 None => return, 195 None => return,
196 }; 196 };
197
198 if let Some(item) = MacroRender::new(ctx.into(), name, macro_).render() { 197 if let Some(item) = MacroRender::new(ctx.into(), name, macro_).render() {
199 self.add(item); 198 self.add(item);
200 } 199 }
@@ -207,24 +206,13 @@ impl Completions {
207 local_name: Option<String>, 206 local_name: Option<String>,
208 ) { 207 ) {
209 let item = FunctionRender::new(ctx.into(), local_name, func).render(); 208 let item = FunctionRender::new(ctx.into(), local_name, func).render();
210
211 self.add(item) 209 self.add(item)
212 } 210 }
213 211
214 pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) { 212 pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {
215 let ast_node = constant.source(ctx.db).value; 213 if let Some(item) = ConstRender::new(ctx.into(), constant).render() {
216 let name = match ast_node.name() { 214 self.add(item);
217 Some(name) => name, 215 }
218 _ => return,
219 };
220 let detail = const_label(&ast_node);
221
222 CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string())
223 .kind(CompletionItemKind::Const)
224 .set_documentation(constant.docs(ctx.db))
225 .set_deprecated(is_deprecated(constant, ctx.db))
226 .detail(detail)
227 .add_to(self);
228 } 216 }
229 217
230 pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) { 218 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_;
5mod function; 5mod function;
6mod builder_ext; 6mod builder_ext;
7mod enum_variant; 7mod enum_variant;
8mod const_;
8 9
9use hir::HasAttrs; 10use hir::{Documentation, HasAttrs};
10use ide_db::RootDatabase; 11use ide_db::RootDatabase;
11use syntax::TextRange; 12use syntax::TextRange;
12 13
13use crate::{config::SnippetCap, CompletionContext}; 14use crate::{config::SnippetCap, CompletionContext};
14 15
15pub(crate) use crate::render::{ 16pub(crate) use crate::render::{
16 enum_variant::EnumVariantRender, function::FunctionRender, macro_::MacroRender, 17 const_::ConstRender, enum_variant::EnumVariantRender, function::FunctionRender,
18 macro_::MacroRender,
17}; 19};
18 20
19#[derive(Debug)] 21#[derive(Debug)]
@@ -41,6 +43,10 @@ impl<'a> RenderContext<'a> {
41 pub fn is_deprecated(&self, node: impl HasAttrs) -> bool { 43 pub fn is_deprecated(&self, node: impl HasAttrs) -> bool {
42 node.attrs(self.db()).by_key("deprecated").exists() 44 node.attrs(self.db()).by_key("deprecated").exists()
43 } 45 }
46
47 pub fn docs(&self, node: impl HasAttrs) -> Option<Documentation> {
48 node.docs(self.db())
49 }
44} 50}
45 51
46impl<'a> From<&'a CompletionContext<'a>> for RenderContext<'a> { 52impl<'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 @@
1use hir::HasSource;
2use syntax::{
3 ast::{Const, NameOwner},
4 display::const_label,
5};
6
7use crate::{
8 item::{CompletionItem, CompletionItemKind, CompletionKind},
9 render::RenderContext,
10};
11
12#[derive(Debug)]
13pub(crate) struct ConstRender<'a> {
14 ctx: RenderContext<'a>,
15 const_: hir::Const,
16 ast_node: Const,
17}
18
19impl<'a> ConstRender<'a> {
20 pub(crate) fn new(ctx: RenderContext<'a>, const_: hir::Const) -> ConstRender<'a> {
21 let ast_node = const_.source(ctx.db()).value;
22 ConstRender { ctx, const_, ast_node }
23 }
24
25 pub(crate) fn render(self) -> Option<CompletionItem> {
26 let name = self.name()?;
27 let detail = self.detail();
28
29 let item = CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name)
30 .kind(CompletionItemKind::Const)
31 .set_documentation(self.ctx.docs(self.const_))
32 .set_deprecated(self.ctx.is_deprecated(self.const_))
33 .detail(detail)
34 .build();
35
36 Some(item)
37 }
38
39 fn name(&self) -> Option<String> {
40 let ast_node = self.const_.source(self.ctx.db()).value;
41 ast_node.name().map(|name| name.text().to_string())
42 }
43
44 fn detail(&self) -> String {
45 const_label(&self.ast_node)
46 }
47}
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 @@
1use hir::{Documentation, HasAttrs, HasSource, Type}; 1use hir::{HasSource, Type};
2use syntax::{ast::Fn, display::function_declaration}; 2use syntax::{ast::Fn, display::function_declaration};
3 3
4use crate::{ 4use crate::{
@@ -30,7 +30,7 @@ impl<'a> FunctionRender<'a> {
30 let params = self.params(); 30 let params = self.params();
31 CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone()) 31 CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone())
32 .kind(self.kind()) 32 .kind(self.kind())
33 .set_documentation(self.docs()) 33 .set_documentation(self.ctx.docs(self.fn_))
34 .set_deprecated(self.ctx.is_deprecated(self.fn_)) 34 .set_deprecated(self.ctx.is_deprecated(self.fn_))
35 .detail(self.detail()) 35 .detail(self.detail())
36 .add_call_parens(self.ctx.completion, self.name, params) 36 .add_call_parens(self.ctx.completion, self.name, params)
@@ -45,8 +45,8 @@ impl<'a> FunctionRender<'a> {
45 if let Some(derefed_ty) = ty.remove_ref() { 45 if let Some(derefed_ty) = ty.remove_ref() {
46 for (name, local) in self.ctx.completion.locals.iter() { 46 for (name, local) in self.ctx.completion.locals.iter() {
47 if name == arg && local.ty(self.ctx.db()) == derefed_ty { 47 if name == arg && local.ty(self.ctx.db()) == derefed_ty {
48 return (if ty.is_mutable_reference() { "&mut " } else { "&" }).to_string() 48 let mutability = if ty.is_mutable_reference() { "&mut " } else { "&" };
49 + &arg.to_string(); 49 return format!("{}{}", mutability, arg);
50 } 50 }
51 } 51 }
52 } 52 }
@@ -80,8 +80,4 @@ impl<'a> FunctionRender<'a> {
80 CompletionItemKind::Function 80 CompletionItemKind::Function
81 } 81 }
82 } 82 }
83
84 fn docs(&self) -> Option<Documentation> {
85 self.fn_.docs(self.ctx.db())
86 }
87} 83}
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 @@
1use hir::{Documentation, HasAttrs, HasSource}; 1use hir::{Documentation, HasSource};
2use syntax::display::macro_label; 2use syntax::display::macro_label;
3use test_utils::mark; 3use test_utils::mark;
4 4
@@ -23,7 +23,7 @@ impl<'a> MacroRender<'a> {
23 name: String, 23 name: String,
24 macro_: hir::MacroDef, 24 macro_: hir::MacroDef,
25 ) -> MacroRender<'a> { 25 ) -> MacroRender<'a> {
26 let docs = macro_.docs(ctx.db()); 26 let docs = ctx.docs(macro_);
27 let docs_str = docs.as_ref().map_or("", |s| s.as_str()); 27 let docs_str = docs.as_ref().map_or("", |s| s.as_str());
28 let (bra, ket) = guess_macro_braces(&name, docs_str); 28 let (bra, ket) = guess_macro_braces(&name, docs_str);
29 29