diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-11-03 07:54:45 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-11-03 07:54:45 +0000 |
commit | 07c7f35effe1a4602ba02baf9ff67a4eb214818f (patch) | |
tree | 1cf4253dc23366dfd20f36285036d73b168a526e /crates/completion/src/render/const_.rs | |
parent | 658e97a39e77bcb978697a66ddccd7e4b58990cf (diff) | |
parent | 8efe43245bc64ed27f88c333541b2cb7af2ce44c (diff) |
Merge #6430
6430: Move completions rendering into a separate module r=popzxc a=popzxc
This PR extracts rendering-related things from `Completions` structure to the new `render` module.
`render` module declares a `Render` structure (which is a generic renderer interface), `RenderContext` (interface for data/methods not required for completions generating, but required for rendering), and a bunch of smaller `*Render` structures which encapsulate logic behind rendering a certain item.
This is just a step in full separation direction, since the following this are still to be done:
- Move some data from `CompletionContext` to the `RenderContext`;
- Forbid any kind of rendering outside of `render` module;
- Extract score computing into a separate module.
This PR is already pretty big, so not to make it even harder to review I decided to split this process into several subsequent PRs.
Co-authored-by: Igor Aleksanov <[email protected]>
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 | } | ||