aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorIgor Aleksanov <[email protected]>2020-11-01 10:10:02 +0000
committerIgor Aleksanov <[email protected]>2020-11-03 07:16:35 +0000
commit15b16917fcd55068d9aba3d4b5d87763ec5deb69 (patch)
treebef2eebbcdfcd660d5eacb14a99c2ff0704c35ea /crates
parent944ccf60758305a1b15224defe622cfca6939aaa (diff)
Add TypeAliasRender
Diffstat (limited to 'crates')
-rw-r--r--crates/completion/src/completions.rs21
-rw-r--r--crates/completion/src/render.rs3
-rw-r--r--crates/completion/src/render/const_.rs3
-rw-r--r--crates/completion/src/render/type_alias.rs46
4 files changed, 54 insertions, 19 deletions
diff --git a/crates/completion/src/completions.rs b/crates/completion/src/completions.rs
index d8dc1b1c3..434366b12 100644
--- a/crates/completion/src/completions.rs
+++ b/crates/completion/src/completions.rs
@@ -14,13 +14,12 @@ pub(crate) mod macro_in_item_position;
14pub(crate) mod trait_impl; 14pub(crate) mod trait_impl;
15pub(crate) mod mod_; 15pub(crate) mod mod_;
16 16
17use hir::{HasAttrs, HasSource, HirDisplay, ModPath, Mutability, ScopeDef, Type}; 17use hir::{HasAttrs, HirDisplay, ModPath, Mutability, ScopeDef, Type};
18use syntax::{ast::NameOwner, display::*};
19use test_utils::mark; 18use test_utils::mark;
20 19
21use crate::{ 20use crate::{
22 item::Builder, 21 item::Builder,
23 render::{ConstRender, EnumVariantRender, FunctionRender, MacroRender}, 22 render::{ConstRender, EnumVariantRender, FunctionRender, MacroRender, TypeAliasRender},
24 CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, CompletionScore, 23 CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, CompletionScore,
25 RootDatabase, 24 RootDatabase,
26}; 25};
@@ -216,19 +215,9 @@ impl Completions {
216 } 215 }
217 216
218 pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) { 217 pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {
219 let type_def = type_alias.source(ctx.db).value; 218 if let Some(item) = TypeAliasRender::new(ctx.into(), type_alias).render() {
220 let name = match type_def.name() { 219 self.add(item)
221 Some(name) => name, 220 }
222 _ => return,
223 };
224 let detail = type_label(&type_def);
225
226 CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string())
227 .kind(CompletionItemKind::TypeAlias)
228 .set_documentation(type_alias.docs(ctx.db))
229 .set_deprecated(is_deprecated(type_alias, ctx.db))
230 .detail(detail)
231 .add_to(self);
232 } 221 }
233 222
234 pub(crate) fn add_qualified_enum_variant( 223 pub(crate) fn add_qualified_enum_variant(
diff --git a/crates/completion/src/render.rs b/crates/completion/src/render.rs
index c614a7172..3a14357f3 100644
--- a/crates/completion/src/render.rs
+++ b/crates/completion/src/render.rs
@@ -6,6 +6,7 @@ mod function;
6mod builder_ext; 6mod builder_ext;
7mod enum_variant; 7mod enum_variant;
8mod const_; 8mod const_;
9mod type_alias;
9 10
10use hir::{Documentation, HasAttrs}; 11use hir::{Documentation, HasAttrs};
11use ide_db::RootDatabase; 12use ide_db::RootDatabase;
@@ -15,7 +16,7 @@ use crate::{config::SnippetCap, CompletionContext};
15 16
16pub(crate) use crate::render::{ 17pub(crate) use crate::render::{
17 const_::ConstRender, enum_variant::EnumVariantRender, function::FunctionRender, 18 const_::ConstRender, enum_variant::EnumVariantRender, function::FunctionRender,
18 macro_::MacroRender, 19 macro_::MacroRender, type_alias::TypeAliasRender,
19}; 20};
20 21
21#[derive(Debug)] 22#[derive(Debug)]
diff --git a/crates/completion/src/render/const_.rs b/crates/completion/src/render/const_.rs
index d88bfa07f..829eb574d 100644
--- a/crates/completion/src/render/const_.rs
+++ b/crates/completion/src/render/const_.rs
@@ -37,8 +37,7 @@ impl<'a> ConstRender<'a> {
37 } 37 }
38 38
39 fn name(&self) -> Option<String> { 39 fn name(&self) -> Option<String> {
40 let ast_node = self.const_.source(self.ctx.db()).value; 40 self.ast_node.name().map(|name| name.text().to_string())
41 ast_node.name().map(|name| name.text().to_string())
42 } 41 }
43 42
44 fn detail(&self) -> String { 43 fn detail(&self) -> String {
diff --git a/crates/completion/src/render/type_alias.rs b/crates/completion/src/render/type_alias.rs
new file mode 100644
index 000000000..378aa8c67
--- /dev/null
+++ b/crates/completion/src/render/type_alias.rs
@@ -0,0 +1,46 @@
1use hir::HasSource;
2use syntax::{
3 ast::{NameOwner, TypeAlias},
4 display::type_label,
5};
6
7use crate::{
8 item::{CompletionItem, CompletionItemKind, CompletionKind},
9 render::RenderContext,
10};
11
12#[derive(Debug)]
13pub(crate) struct TypeAliasRender<'a> {
14 ctx: RenderContext<'a>,
15 type_alias: hir::TypeAlias,
16 ast_node: TypeAlias,
17}
18
19impl<'a> TypeAliasRender<'a> {
20 pub(crate) fn new(ctx: RenderContext<'a>, type_alias: hir::TypeAlias) -> TypeAliasRender<'a> {
21 let ast_node = type_alias.source(ctx.db()).value;
22 TypeAliasRender { ctx, type_alias, 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::TypeAlias)
31 .set_documentation(self.ctx.docs(self.type_alias))
32 .set_deprecated(self.ctx.is_deprecated(self.type_alias))
33 .detail(detail)
34 .build();
35
36 Some(item)
37 }
38
39 fn name(&self) -> Option<String> {
40 self.ast_node.name().map(|name| name.text().to_string())
41 }
42
43 fn detail(&self) -> String {
44 type_label(&self.ast_node)
45 }
46}