diff options
Diffstat (limited to 'crates/completion')
| -rw-r--r-- | crates/completion/src/completions.rs | 21 | ||||
| -rw-r--r-- | crates/completion/src/render.rs | 3 | ||||
| -rw-r--r-- | crates/completion/src/render/const_.rs | 3 | ||||
| -rw-r--r-- | crates/completion/src/render/type_alias.rs | 46 |
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; | |||
| 14 | pub(crate) mod trait_impl; | 14 | pub(crate) mod trait_impl; |
| 15 | pub(crate) mod mod_; | 15 | pub(crate) mod mod_; |
| 16 | 16 | ||
| 17 | use hir::{HasAttrs, HasSource, HirDisplay, ModPath, Mutability, ScopeDef, Type}; | 17 | use hir::{HasAttrs, HirDisplay, ModPath, Mutability, ScopeDef, Type}; |
| 18 | use syntax::{ast::NameOwner, display::*}; | ||
| 19 | use test_utils::mark; | 18 | use test_utils::mark; |
| 20 | 19 | ||
| 21 | use crate::{ | 20 | use 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; | |||
| 6 | mod builder_ext; | 6 | mod builder_ext; |
| 7 | mod enum_variant; | 7 | mod enum_variant; |
| 8 | mod const_; | 8 | mod const_; |
| 9 | mod type_alias; | ||
| 9 | 10 | ||
| 10 | use hir::{Documentation, HasAttrs}; | 11 | use hir::{Documentation, HasAttrs}; |
| 11 | use ide_db::RootDatabase; | 12 | use ide_db::RootDatabase; |
| @@ -15,7 +16,7 @@ use crate::{config::SnippetCap, CompletionContext}; | |||
| 15 | 16 | ||
| 16 | pub(crate) use crate::render::{ | 17 | pub(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 @@ | |||
| 1 | use hir::HasSource; | ||
| 2 | use syntax::{ | ||
| 3 | ast::{NameOwner, TypeAlias}, | ||
| 4 | display::type_label, | ||
| 5 | }; | ||
| 6 | |||
| 7 | use crate::{ | ||
| 8 | item::{CompletionItem, CompletionItemKind, CompletionKind}, | ||
| 9 | render::RenderContext, | ||
| 10 | }; | ||
| 11 | |||
| 12 | #[derive(Debug)] | ||
| 13 | pub(crate) struct TypeAliasRender<'a> { | ||
| 14 | ctx: RenderContext<'a>, | ||
| 15 | type_alias: hir::TypeAlias, | ||
| 16 | ast_node: TypeAlias, | ||
| 17 | } | ||
| 18 | |||
| 19 | impl<'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 | } | ||
