diff options
author | Igor Aleksanov <[email protected]> | 2020-11-01 09:59:43 +0000 |
---|---|---|
committer | Igor Aleksanov <[email protected]> | 2020-11-03 07:16:35 +0000 |
commit | 944ccf60758305a1b15224defe622cfca6939aaa (patch) | |
tree | d300280524b06d66d96d04df6bc882293c788707 /crates/completion/src/render | |
parent | fc8a1cd8006b021541ff673ec7f37a0f4b7bef57 (diff) |
Add ConstRender
Diffstat (limited to 'crates/completion/src/render')
-rw-r--r-- | crates/completion/src/render/const_.rs | 47 | ||||
-rw-r--r-- | crates/completion/src/render/function.rs | 12 | ||||
-rw-r--r-- | crates/completion/src/render/macro_.rs | 4 |
3 files changed, 53 insertions, 10 deletions
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 @@ | |||
1 | use hir::HasSource; | ||
2 | use syntax::{ | ||
3 | ast::{Const, NameOwner}, | ||
4 | display::const_label, | ||
5 | }; | ||
6 | |||
7 | use crate::{ | ||
8 | item::{CompletionItem, CompletionItemKind, CompletionKind}, | ||
9 | render::RenderContext, | ||
10 | }; | ||
11 | |||
12 | #[derive(Debug)] | ||
13 | pub(crate) struct ConstRender<'a> { | ||
14 | ctx: RenderContext<'a>, | ||
15 | const_: hir::Const, | ||
16 | ast_node: Const, | ||
17 | } | ||
18 | |||
19 | impl<'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 @@ | |||
1 | use hir::{Documentation, HasAttrs, HasSource, Type}; | 1 | use hir::{HasSource, Type}; |
2 | use syntax::{ast::Fn, display::function_declaration}; | 2 | use syntax::{ast::Fn, display::function_declaration}; |
3 | 3 | ||
4 | use crate::{ | 4 | use 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 @@ | |||
1 | use hir::{Documentation, HasAttrs, HasSource}; | 1 | use hir::{Documentation, HasSource}; |
2 | use syntax::display::macro_label; | 2 | use syntax::display::macro_label; |
3 | use test_utils::mark; | 3 | use 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 | ||