diff options
author | Kirill Bulatov <[email protected]> | 2021-01-18 23:08:59 +0000 |
---|---|---|
committer | Kirill Bulatov <[email protected]> | 2021-01-18 23:08:59 +0000 |
commit | ca6548827634155f28e75bc6cb2e734d2d5d1e95 (patch) | |
tree | 49e2050fb27937c02768954e94bcd3b4006ed3d3 /crates/completion/src | |
parent | cc663a7b0c1fc813c58be8b8d84772c52f736d26 (diff) |
Show deprecated completions for deprecated traits
Diffstat (limited to 'crates/completion/src')
-rw-r--r-- | crates/completion/src/completions/flyimport.rs | 12 | ||||
-rw-r--r-- | crates/completion/src/render.rs | 47 | ||||
-rw-r--r-- | crates/completion/src/render/const_.rs | 5 | ||||
-rw-r--r-- | crates/completion/src/render/function.rs | 4 | ||||
-rw-r--r-- | crates/completion/src/render/type_alias.rs | 5 | ||||
-rw-r--r-- | crates/completion/src/test_utils.rs | 3 |
6 files changed, 58 insertions, 18 deletions
diff --git a/crates/completion/src/completions/flyimport.rs b/crates/completion/src/completions/flyimport.rs index 47e797ac8..dc0b38a16 100644 --- a/crates/completion/src/completions/flyimport.rs +++ b/crates/completion/src/completions/flyimport.rs | |||
@@ -48,7 +48,7 @@ | |||
48 | //! Note that having this flag set to `true` does not guarantee that the feature is enabled: your client needs to have the corredponding | 48 | //! Note that having this flag set to `true` does not guarantee that the feature is enabled: your client needs to have the corredponding |
49 | //! capability enabled. | 49 | //! capability enabled. |
50 | 50 | ||
51 | use hir::{ModPath, ScopeDef}; | 51 | use hir::{AsAssocItem, ModPath, ScopeDef}; |
52 | use ide_db::helpers::{ | 52 | use ide_db::helpers::{ |
53 | import_assets::{ImportAssets, ImportCandidate}, | 53 | import_assets::{ImportAssets, ImportCandidate}, |
54 | insert_use::ImportScope, | 54 | insert_use::ImportScope, |
@@ -601,11 +601,12 @@ fn main() { | |||
601 | } | 601 | } |
602 | 602 | ||
603 | #[test] | 603 | #[test] |
604 | fn zero_input_assoc_item_completion() { | 604 | fn zero_input_deprecated_assoc_item_completion() { |
605 | check( | 605 | check( |
606 | r#" | 606 | r#" |
607 | //- /lib.rs crate:dep | 607 | //- /lib.rs crate:dep |
608 | pub mod test_mod { | 608 | pub mod test_mod { |
609 | #[deprecated] | ||
609 | pub trait TestTrait { | 610 | pub trait TestTrait { |
610 | const SPECIAL_CONST: u8; | 611 | const SPECIAL_CONST: u8; |
611 | type HumbleType; | 612 | type HumbleType; |
@@ -628,7 +629,7 @@ fn main() { | |||
628 | } | 629 | } |
629 | "#, | 630 | "#, |
630 | expect![[r#" | 631 | expect![[r#" |
631 | me random_method() (dep::test_mod::TestTrait) fn random_method(&self) | 632 | me random_method() (dep::test_mod::TestTrait) fn random_method(&self) DEPRECATED |
632 | "#]], | 633 | "#]], |
633 | ); | 634 | ); |
634 | 635 | ||
@@ -636,6 +637,7 @@ fn main() { | |||
636 | r#" | 637 | r#" |
637 | //- /lib.rs crate:dep | 638 | //- /lib.rs crate:dep |
638 | pub mod test_mod { | 639 | pub mod test_mod { |
640 | #[deprecated] | ||
639 | pub trait TestTrait { | 641 | pub trait TestTrait { |
640 | const SPECIAL_CONST: u8; | 642 | const SPECIAL_CONST: u8; |
641 | type HumbleType; | 643 | type HumbleType; |
@@ -657,8 +659,8 @@ fn main() { | |||
657 | } | 659 | } |
658 | "#, | 660 | "#, |
659 | expect![[r#" | 661 | expect![[r#" |
660 | ct SPECIAL_CONST (dep::test_mod::TestTrait) | 662 | ct SPECIAL_CONST (dep::test_mod::TestTrait) DEPRECATED |
661 | fn weird_function() (dep::test_mod::TestTrait) fn weird_function() | 663 | fn weird_function() (dep::test_mod::TestTrait) fn weird_function() DEPRECATED |
662 | "#]], | 664 | "#]], |
663 | ); | 665 | ); |
664 | } | 666 | } |
diff --git a/crates/completion/src/render.rs b/crates/completion/src/render.rs index dbb30d233..4f622d28a 100644 --- a/crates/completion/src/render.rs +++ b/crates/completion/src/render.rs | |||
@@ -10,7 +10,9 @@ pub(crate) mod type_alias; | |||
10 | 10 | ||
11 | mod builder_ext; | 11 | mod builder_ext; |
12 | 12 | ||
13 | use hir::{Documentation, HasAttrs, HirDisplay, ModuleDef, Mutability, ScopeDef, Type}; | 13 | use hir::{ |
14 | AsAssocItem, Documentation, HasAttrs, HirDisplay, ModuleDef, Mutability, ScopeDef, Type, | ||
15 | }; | ||
14 | use ide_db::{helpers::SnippetCap, RootDatabase}; | 16 | use ide_db::{helpers::SnippetCap, RootDatabase}; |
15 | use syntax::TextRange; | 17 | use syntax::TextRange; |
16 | use test_utils::mark; | 18 | use test_utils::mark; |
@@ -91,6 +93,22 @@ impl<'a> RenderContext<'a> { | |||
91 | attrs.by_key("deprecated").exists() || attrs.by_key("rustc_deprecated").exists() | 93 | attrs.by_key("deprecated").exists() || attrs.by_key("rustc_deprecated").exists() |
92 | } | 94 | } |
93 | 95 | ||
96 | fn is_deprecated_assoc_item(&self, as_assoc_item: impl AsAssocItem) -> bool { | ||
97 | let db = self.db(); | ||
98 | let assoc = match as_assoc_item.as_assoc_item(db) { | ||
99 | Some(assoc) => assoc, | ||
100 | None => return false, | ||
101 | }; | ||
102 | |||
103 | let is_assoc_deprecated = match assoc { | ||
104 | hir::AssocItem::Function(it) => self.is_deprecated(it), | ||
105 | hir::AssocItem::Const(it) => self.is_deprecated(it), | ||
106 | hir::AssocItem::TypeAlias(it) => self.is_deprecated(it), | ||
107 | }; | ||
108 | is_assoc_deprecated | ||
109 | || assoc.containing_trait(db).map(|trait_| self.is_deprecated(trait_)).unwrap_or(false) | ||
110 | } | ||
111 | |||
94 | fn docs(&self, node: impl HasAttrs) -> Option<Documentation> { | 112 | fn docs(&self, node: impl HasAttrs) -> Option<Documentation> { |
95 | node.docs(self.db()) | 113 | node.docs(self.db()) |
96 | } | 114 | } |
@@ -207,8 +225,6 @@ impl<'a> Render<'a> { | |||
207 | } | 225 | } |
208 | }; | 226 | }; |
209 | 227 | ||
210 | let docs = self.docs(resolution); | ||
211 | |||
212 | let mut item = | 228 | let mut item = |
213 | CompletionItem::new(completion_kind, self.ctx.source_range(), local_name.clone()); | 229 | CompletionItem::new(completion_kind, self.ctx.source_range(), local_name.clone()); |
214 | if let ScopeDef::Local(local) = resolution { | 230 | if let ScopeDef::Local(local) = resolution { |
@@ -254,13 +270,14 @@ impl<'a> Render<'a> { | |||
254 | } | 270 | } |
255 | } | 271 | } |
256 | 272 | ||
257 | let item = item | 273 | Some( |
258 | .kind(kind) | 274 | item.kind(kind) |
259 | .add_import(import_to_add) | 275 | .add_import(import_to_add) |
260 | .set_documentation(docs) | 276 | .set_ref_match(ref_match) |
261 | .set_ref_match(ref_match) | 277 | .set_documentation(self.docs(resolution)) |
262 | .build(); | 278 | .set_deprecated(self.is_deprecated(resolution)) |
263 | Some(item) | 279 | .build(), |
280 | ) | ||
264 | } | 281 | } |
265 | 282 | ||
266 | fn docs(&self, resolution: &ScopeDef) -> Option<Documentation> { | 283 | fn docs(&self, resolution: &ScopeDef) -> Option<Documentation> { |
@@ -276,6 +293,16 @@ impl<'a> Render<'a> { | |||
276 | _ => None, | 293 | _ => None, |
277 | } | 294 | } |
278 | } | 295 | } |
296 | |||
297 | fn is_deprecated(&self, resolution: &ScopeDef) -> bool { | ||
298 | match resolution { | ||
299 | ScopeDef::ModuleDef(it) => self.ctx.is_deprecated_assoc_item(*it), | ||
300 | ScopeDef::MacroDef(it) => self.ctx.is_deprecated(*it), | ||
301 | ScopeDef::GenericParam(it) => self.ctx.is_deprecated(*it), | ||
302 | ScopeDef::AdtSelfType(it) => self.ctx.is_deprecated(*it), | ||
303 | _ => false, | ||
304 | } | ||
305 | } | ||
279 | } | 306 | } |
280 | 307 | ||
281 | fn compute_score_from_active( | 308 | fn compute_score_from_active( |
diff --git a/crates/completion/src/render/const_.rs b/crates/completion/src/render/const_.rs index ce924f309..e46452d4e 100644 --- a/crates/completion/src/render/const_.rs +++ b/crates/completion/src/render/const_.rs | |||
@@ -38,7 +38,10 @@ impl<'a> ConstRender<'a> { | |||
38 | let item = CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name) | 38 | let item = CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name) |
39 | .kind(CompletionItemKind::Const) | 39 | .kind(CompletionItemKind::Const) |
40 | .set_documentation(self.ctx.docs(self.const_)) | 40 | .set_documentation(self.ctx.docs(self.const_)) |
41 | .set_deprecated(self.ctx.is_deprecated(self.const_)) | 41 | .set_deprecated( |
42 | self.ctx.is_deprecated(self.const_) | ||
43 | || self.ctx.is_deprecated_assoc_item(self.const_), | ||
44 | ) | ||
42 | .detail(detail) | 45 | .detail(detail) |
43 | .build(); | 46 | .build(); |
44 | 47 | ||
diff --git a/crates/completion/src/render/function.rs b/crates/completion/src/render/function.rs index f5b0ce3e3..8f4c66211 100644 --- a/crates/completion/src/render/function.rs +++ b/crates/completion/src/render/function.rs | |||
@@ -44,7 +44,9 @@ impl<'a> FunctionRender<'a> { | |||
44 | CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone()) | 44 | CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone()) |
45 | .kind(self.kind()) | 45 | .kind(self.kind()) |
46 | .set_documentation(self.ctx.docs(self.func)) | 46 | .set_documentation(self.ctx.docs(self.func)) |
47 | .set_deprecated(self.ctx.is_deprecated(self.func)) | 47 | .set_deprecated( |
48 | self.ctx.is_deprecated(self.func) || self.ctx.is_deprecated_assoc_item(self.func), | ||
49 | ) | ||
48 | .detail(self.detail()) | 50 | .detail(self.detail()) |
49 | .add_call_parens(self.ctx.completion, self.name, params) | 51 | .add_call_parens(self.ctx.completion, self.name, params) |
50 | .add_import(import_to_add) | 52 | .add_import(import_to_add) |
diff --git a/crates/completion/src/render/type_alias.rs b/crates/completion/src/render/type_alias.rs index 69b445b9c..29287143a 100644 --- a/crates/completion/src/render/type_alias.rs +++ b/crates/completion/src/render/type_alias.rs | |||
@@ -38,7 +38,10 @@ impl<'a> TypeAliasRender<'a> { | |||
38 | let item = CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name) | 38 | let item = CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name) |
39 | .kind(CompletionItemKind::TypeAlias) | 39 | .kind(CompletionItemKind::TypeAlias) |
40 | .set_documentation(self.ctx.docs(self.type_alias)) | 40 | .set_documentation(self.ctx.docs(self.type_alias)) |
41 | .set_deprecated(self.ctx.is_deprecated(self.type_alias)) | 41 | .set_deprecated( |
42 | self.ctx.is_deprecated(self.type_alias) | ||
43 | || self.ctx.is_deprecated_assoc_item(self.type_alias), | ||
44 | ) | ||
42 | .detail(detail) | 45 | .detail(detail) |
43 | .build(); | 46 | .build(); |
44 | 47 | ||
diff --git a/crates/completion/src/test_utils.rs b/crates/completion/src/test_utils.rs index 3faf861b9..baff83305 100644 --- a/crates/completion/src/test_utils.rs +++ b/crates/completion/src/test_utils.rs | |||
@@ -83,6 +83,9 @@ pub(crate) fn completion_list_with_config( | |||
83 | let width = label_width.saturating_sub(monospace_width(it.label())); | 83 | let width = label_width.saturating_sub(monospace_width(it.label())); |
84 | format_to!(buf, "{:width$} {}", "", detail, width = width); | 84 | format_to!(buf, "{:width$} {}", "", detail, width = width); |
85 | } | 85 | } |
86 | if it.deprecated() { | ||
87 | format_to!(buf, " DEPRECATED"); | ||
88 | } | ||
86 | format_to!(buf, "\n"); | 89 | format_to!(buf, "\n"); |
87 | buf | 90 | buf |
88 | }) | 91 | }) |