diff options
Diffstat (limited to 'crates/completion/src/render/const_.rs')
-rw-r--r-- | crates/completion/src/render/const_.rs | 47 |
1 files changed, 47 insertions, 0 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 | } | ||