diff options
Diffstat (limited to 'crates/completion/src/render/const_.rs')
-rw-r--r-- | crates/completion/src/render/const_.rs | 55 |
1 files changed, 55 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..039bdabc0 --- /dev/null +++ b/crates/completion/src/render/const_.rs | |||
@@ -0,0 +1,55 @@ | |||
1 | //! Renderer for `const` fields. | ||
2 | |||
3 | use hir::HasSource; | ||
4 | use syntax::{ | ||
5 | ast::{Const, NameOwner}, | ||
6 | display::const_label, | ||
7 | }; | ||
8 | |||
9 | use crate::{ | ||
10 | item::{CompletionItem, CompletionItemKind, CompletionKind}, | ||
11 | render::RenderContext, | ||
12 | }; | ||
13 | |||
14 | pub(crate) fn render_const<'a>( | ||
15 | ctx: RenderContext<'a>, | ||
16 | const_: hir::Const, | ||
17 | ) -> Option<CompletionItem> { | ||
18 | ConstRender::new(ctx, const_).render() | ||
19 | } | ||
20 | |||
21 | #[derive(Debug)] | ||
22 | struct ConstRender<'a> { | ||
23 | ctx: RenderContext<'a>, | ||
24 | const_: hir::Const, | ||
25 | ast_node: Const, | ||
26 | } | ||
27 | |||
28 | impl<'a> ConstRender<'a> { | ||
29 | fn new(ctx: RenderContext<'a>, const_: hir::Const) -> ConstRender<'a> { | ||
30 | let ast_node = const_.source(ctx.db()).value; | ||
31 | ConstRender { ctx, const_, ast_node } | ||
32 | } | ||
33 | |||
34 | fn render(self) -> Option<CompletionItem> { | ||
35 | let name = self.name()?; | ||
36 | let detail = self.detail(); | ||
37 | |||
38 | let item = CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name) | ||
39 | .kind(CompletionItemKind::Const) | ||
40 | .set_documentation(self.ctx.docs(self.const_)) | ||
41 | .set_deprecated(self.ctx.is_deprecated(self.const_)) | ||
42 | .detail(detail) | ||
43 | .build(); | ||
44 | |||
45 | Some(item) | ||
46 | } | ||
47 | |||
48 | fn name(&self) -> Option<String> { | ||
49 | self.ast_node.name().map(|name| name.text().to_string()) | ||
50 | } | ||
51 | |||
52 | fn detail(&self) -> String { | ||
53 | const_label(&self.ast_node) | ||
54 | } | ||
55 | } | ||