aboutsummaryrefslogtreecommitdiff
path: root/crates/completion/src/render.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/completion/src/render.rs')
-rw-r--r--crates/completion/src/render.rs84
1 files changed, 56 insertions, 28 deletions
diff --git a/crates/completion/src/render.rs b/crates/completion/src/render.rs
index 820dd01d1..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
11mod builder_ext; 11mod builder_ext;
12 12
13use hir::{Documentation, HasAttrs, HirDisplay, Mutability, ScopeDef, Type}; 13use hir::{
14 AsAssocItem, Documentation, HasAttrs, HirDisplay, ModuleDef, Mutability, ScopeDef, Type,
15};
14use ide_db::{helpers::SnippetCap, RootDatabase}; 16use ide_db::{helpers::SnippetCap, RootDatabase};
15use syntax::TextRange; 17use syntax::TextRange;
16use test_utils::mark; 18use test_utils::mark;
@@ -51,16 +53,16 @@ pub(crate) fn render_resolution_with_import<'a>(
51 import_edit: ImportEdit, 53 import_edit: ImportEdit,
52 resolution: &ScopeDef, 54 resolution: &ScopeDef,
53) -> Option<CompletionItem> { 55) -> Option<CompletionItem> {
54 Render::new(ctx) 56 let local_name = match resolution {
55 .render_resolution( 57 ScopeDef::ModuleDef(ModuleDef::Function(f)) => f.name(ctx.completion.db).to_string(),
56 import_edit.import_path.segments.last()?.to_string(), 58 ScopeDef::ModuleDef(ModuleDef::Const(c)) => c.name(ctx.completion.db)?.to_string(),
57 Some(import_edit), 59 ScopeDef::ModuleDef(ModuleDef::TypeAlias(t)) => t.name(ctx.completion.db).to_string(),
58 resolution, 60 _ => import_edit.import_path.segments.last()?.to_string(),
59 ) 61 };
60 .map(|mut item| { 62 Render::new(ctx).render_resolution(local_name, Some(import_edit), resolution).map(|mut item| {
61 item.completion_kind = CompletionKind::Magic; 63 item.completion_kind = CompletionKind::Magic;
62 item 64 item
63 }) 65 })
64} 66}
65 67
66/// Interface for data and methods required for items rendering. 68/// Interface for data and methods required for items rendering.
@@ -87,7 +89,24 @@ impl<'a> RenderContext<'a> {
87 } 89 }
88 90
89 fn is_deprecated(&self, node: impl HasAttrs) -> bool { 91 fn is_deprecated(&self, node: impl HasAttrs) -> bool {
90 node.attrs(self.db()).by_key("deprecated").exists() 92 let attrs = node.attrs(self.db());
93 attrs.by_key("deprecated").exists() || attrs.by_key("rustc_deprecated").exists()
94 }
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)
91 } 110 }
92 111
93 fn docs(&self, node: impl HasAttrs) -> Option<Documentation> { 112 fn docs(&self, node: impl HasAttrs) -> Option<Documentation> {
@@ -206,8 +225,6 @@ impl<'a> Render<'a> {
206 } 225 }
207 }; 226 };
208 227
209 let docs = self.docs(resolution);
210
211 let mut item = 228 let mut item =
212 CompletionItem::new(completion_kind, self.ctx.source_range(), local_name.clone()); 229 CompletionItem::new(completion_kind, self.ctx.source_range(), local_name.clone());
213 if let ScopeDef::Local(local) = resolution { 230 if let ScopeDef::Local(local) = resolution {
@@ -253,13 +270,14 @@ impl<'a> Render<'a> {
253 } 270 }
254 } 271 }
255 272
256 let item = item 273 Some(
257 .kind(kind) 274 item.kind(kind)
258 .add_import(import_to_add) 275 .add_import(import_to_add)
259 .set_documentation(docs) 276 .set_ref_match(ref_match)
260 .set_ref_match(ref_match) 277 .set_documentation(self.docs(resolution))
261 .build(); 278 .set_deprecated(self.is_deprecated(resolution))
262 Some(item) 279 .build(),
280 )
263 } 281 }
264 282
265 fn docs(&self, resolution: &ScopeDef) -> Option<Documentation> { 283 fn docs(&self, resolution: &ScopeDef) -> Option<Documentation> {
@@ -275,6 +293,16 @@ impl<'a> Render<'a> {
275 _ => None, 293 _ => None,
276 } 294 }
277 } 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 }
278} 306}
279 307
280fn compute_score_from_active( 308fn compute_score_from_active(
@@ -485,7 +513,7 @@ fn main() { let _: m::Spam = S$0 }
485 r#" 513 r#"
486#[deprecated] 514#[deprecated]
487fn something_deprecated() {} 515fn something_deprecated() {}
488#[deprecated(since = "1.0.0")] 516#[rustc_deprecated(since = "1.0.0")]
489fn something_else_deprecated() {} 517fn something_else_deprecated() {}
490 518
491fn main() { som$0 } 519fn main() { som$0 }
@@ -494,8 +522,8 @@ fn main() { som$0 }
494 [ 522 [
495 CompletionItem { 523 CompletionItem {
496 label: "main()", 524 label: "main()",
497 source_range: 121..124, 525 source_range: 127..130,
498 delete: 121..124, 526 delete: 127..130,
499 insert: "main()$0", 527 insert: "main()$0",
500 kind: Function, 528 kind: Function,
501 lookup: "main", 529 lookup: "main",
@@ -503,8 +531,8 @@ fn main() { som$0 }
503 }, 531 },
504 CompletionItem { 532 CompletionItem {
505 label: "something_deprecated()", 533 label: "something_deprecated()",
506 source_range: 121..124, 534 source_range: 127..130,
507 delete: 121..124, 535 delete: 127..130,
508 insert: "something_deprecated()$0", 536 insert: "something_deprecated()$0",
509 kind: Function, 537 kind: Function,
510 lookup: "something_deprecated", 538 lookup: "something_deprecated",
@@ -513,8 +541,8 @@ fn main() { som$0 }
513 }, 541 },
514 CompletionItem { 542 CompletionItem {
515 label: "something_else_deprecated()", 543 label: "something_else_deprecated()",
516 source_range: 121..124, 544 source_range: 127..130,
517 delete: 121..124, 545 delete: 127..130,
518 insert: "something_else_deprecated()$0", 546 insert: "something_else_deprecated()$0",
519 kind: Function, 547 kind: Function,
520 lookup: "something_else_deprecated", 548 lookup: "something_else_deprecated",