diff options
author | Martin Asquino <[email protected]> | 2019-11-02 02:52:59 +0000 |
---|---|---|
committer | Martin Asquino <[email protected]> | 2019-11-02 20:37:47 +0000 |
commit | dc3848a6a3e659004ba4f2160503be28ea0f9051 (patch) | |
tree | d146b883f6352cbc647d8ad484377242582a7c39 /crates/ra_ide_api/src/completion/presentation.rs | |
parent | eb8f76a86fe88fcad370577b344f397dd6118cfd (diff) |
Set `deprecated` field on `CompletionItem`s
Diffstat (limited to 'crates/ra_ide_api/src/completion/presentation.rs')
-rw-r--r-- | crates/ra_ide_api/src/completion/presentation.rs | 83 |
1 files changed, 82 insertions, 1 deletions
diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index 65bb639ed..d98201887 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs | |||
@@ -2,7 +2,7 @@ | |||
2 | 2 | ||
3 | use hir::{db::HirDatabase, Docs, HasSource, HirDisplay, ScopeDef, Ty, TypeWalk}; | 3 | use hir::{db::HirDatabase, Docs, HasSource, HirDisplay, ScopeDef, Ty, TypeWalk}; |
4 | use join_to_string::join; | 4 | use join_to_string::join; |
5 | use ra_syntax::ast::NameOwner; | 5 | use ra_syntax::ast::{AttrsOwner, NameOwner}; |
6 | use test_utils::tested_by; | 6 | use test_utils::tested_by; |
7 | 7 | ||
8 | use crate::completion::{ | 8 | use crate::completion::{ |
@@ -18,6 +18,11 @@ impl Completions { | |||
18 | field: hir::StructField, | 18 | field: hir::StructField, |
19 | substs: &hir::Substs, | 19 | substs: &hir::Substs, |
20 | ) { | 20 | ) { |
21 | let ast_node = field.source(ctx.db).ast; | ||
22 | let is_deprecated = match ast_node { | ||
23 | hir::FieldSource::Named(m) => is_deprecated(m), | ||
24 | hir::FieldSource::Pos(m) => is_deprecated(m), | ||
25 | }; | ||
21 | CompletionItem::new( | 26 | CompletionItem::new( |
22 | CompletionKind::Reference, | 27 | CompletionKind::Reference, |
23 | ctx.source_range(), | 28 | ctx.source_range(), |
@@ -26,6 +31,7 @@ impl Completions { | |||
26 | .kind(CompletionItemKind::Field) | 31 | .kind(CompletionItemKind::Field) |
27 | .detail(field.ty(ctx.db).subst(substs).display(ctx.db).to_string()) | 32 | .detail(field.ty(ctx.db).subst(substs).display(ctx.db).to_string()) |
28 | .set_documentation(field.docs(ctx.db)) | 33 | .set_documentation(field.docs(ctx.db)) |
34 | .set_deprecated(is_deprecated) | ||
29 | .add_to(self); | 35 | .add_to(self); |
30 | } | 36 | } |
31 | 37 | ||
@@ -179,6 +185,7 @@ impl Completions { | |||
179 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), ¯o_declaration) | 185 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), ¯o_declaration) |
180 | .kind(CompletionItemKind::Macro) | 186 | .kind(CompletionItemKind::Macro) |
181 | .set_documentation(docs.clone()) | 187 | .set_documentation(docs.clone()) |
188 | .set_deprecated(is_deprecated(ast_node)) | ||
182 | .detail(detail); | 189 | .detail(detail); |
183 | 190 | ||
184 | builder = if ctx.use_item_syntax.is_some() { | 191 | builder = if ctx.use_item_syntax.is_some() { |
@@ -211,6 +218,7 @@ impl Completions { | |||
211 | CompletionItemKind::Function | 218 | CompletionItemKind::Function |
212 | }) | 219 | }) |
213 | .set_documentation(func.docs(ctx.db)) | 220 | .set_documentation(func.docs(ctx.db)) |
221 | .set_deprecated(is_deprecated(ast_node)) | ||
214 | .detail(detail); | 222 | .detail(detail); |
215 | 223 | ||
216 | // Add `<>` for generic types | 224 | // Add `<>` for generic types |
@@ -242,6 +250,7 @@ impl Completions { | |||
242 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string()) | 250 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string()) |
243 | .kind(CompletionItemKind::Const) | 251 | .kind(CompletionItemKind::Const) |
244 | .set_documentation(constant.docs(ctx.db)) | 252 | .set_documentation(constant.docs(ctx.db)) |
253 | .set_deprecated(is_deprecated(ast_node)) | ||
245 | .detail(detail) | 254 | .detail(detail) |
246 | .add_to(self); | 255 | .add_to(self); |
247 | } | 256 | } |
@@ -257,11 +266,13 @@ impl Completions { | |||
257 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string()) | 266 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string()) |
258 | .kind(CompletionItemKind::TypeAlias) | 267 | .kind(CompletionItemKind::TypeAlias) |
259 | .set_documentation(type_alias.docs(ctx.db)) | 268 | .set_documentation(type_alias.docs(ctx.db)) |
269 | .set_deprecated(is_deprecated(type_def)) | ||
260 | .detail(detail) | 270 | .detail(detail) |
261 | .add_to(self); | 271 | .add_to(self); |
262 | } | 272 | } |
263 | 273 | ||
264 | pub(crate) fn add_enum_variant(&mut self, ctx: &CompletionContext, variant: hir::EnumVariant) { | 274 | pub(crate) fn add_enum_variant(&mut self, ctx: &CompletionContext, variant: hir::EnumVariant) { |
275 | let is_deprecated = is_deprecated(variant.source(ctx.db).ast); | ||
265 | let name = match variant.name(ctx.db) { | 276 | let name = match variant.name(ctx.db) { |
266 | Some(it) => it, | 277 | Some(it) => it, |
267 | None => return, | 278 | None => return, |
@@ -274,11 +285,16 @@ impl Completions { | |||
274 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.to_string()) | 285 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.to_string()) |
275 | .kind(CompletionItemKind::EnumVariant) | 286 | .kind(CompletionItemKind::EnumVariant) |
276 | .set_documentation(variant.docs(ctx.db)) | 287 | .set_documentation(variant.docs(ctx.db)) |
288 | .set_deprecated(is_deprecated) | ||
277 | .detail(detail) | 289 | .detail(detail) |
278 | .add_to(self); | 290 | .add_to(self); |
279 | } | 291 | } |
280 | } | 292 | } |
281 | 293 | ||
294 | fn is_deprecated(node: impl AttrsOwner) -> bool { | ||
295 | node.attrs().filter_map(|x| x.simple_name()).any(|x| x == "deprecated") | ||
296 | } | ||
297 | |||
282 | fn has_non_default_type_params(def: hir::GenericDef, db: &db::RootDatabase) -> bool { | 298 | fn has_non_default_type_params(def: hir::GenericDef, db: &db::RootDatabase) -> bool { |
283 | let subst = db.generic_defaults(def); | 299 | let subst = db.generic_defaults(def); |
284 | subst.iter().any(|ty| ty == &Ty::Unknown) | 300 | subst.iter().any(|ty| ty == &Ty::Unknown) |
@@ -296,6 +312,57 @@ mod tests { | |||
296 | } | 312 | } |
297 | 313 | ||
298 | #[test] | 314 | #[test] |
315 | fn sets_deprecated_flag_in_completion_items() { | ||
316 | assert_debug_snapshot!( | ||
317 | do_reference_completion( | ||
318 | r#" | ||
319 | #[deprecated] | ||
320 | fn something_deprecated() {} | ||
321 | |||
322 | #[deprecated(since = "1.0.0")] | ||
323 | fn something_else_deprecated() {} | ||
324 | |||
325 | fn main() { som<|> } | ||
326 | "#, | ||
327 | ), | ||
328 | @r###" | ||
329 | [ | ||
330 | CompletionItem { | ||
331 | label: "main()", | ||
332 | source_range: [203; 206), | ||
333 | delete: [203; 206), | ||
334 | insert: "main()$0", | ||
335 | kind: Function, | ||
336 | lookup: "main", | ||
337 | detail: "fn main()", | ||
338 | deprecated: false, | ||
339 | }, | ||
340 | CompletionItem { | ||
341 | label: "something_deprecated()", | ||
342 | source_range: [203; 206), | ||
343 | delete: [203; 206), | ||
344 | insert: "something_deprecated()$0", | ||
345 | kind: Function, | ||
346 | lookup: "something_deprecated", | ||
347 | detail: "fn something_deprecated()", | ||
348 | deprecated: true, | ||
349 | }, | ||
350 | CompletionItem { | ||
351 | label: "something_else_deprecated()", | ||
352 | source_range: [203; 206), | ||
353 | delete: [203; 206), | ||
354 | insert: "something_else_deprecated()$0", | ||
355 | kind: Function, | ||
356 | lookup: "something_else_deprecated", | ||
357 | detail: "fn something_else_deprecated()", | ||
358 | deprecated: true, | ||
359 | }, | ||
360 | ] | ||
361 | "### | ||
362 | ); | ||
363 | } | ||
364 | |||
365 | #[test] | ||
299 | fn inserts_parens_for_function_calls() { | 366 | fn inserts_parens_for_function_calls() { |
300 | covers!(inserts_parens_for_function_calls); | 367 | covers!(inserts_parens_for_function_calls); |
301 | assert_debug_snapshot!( | 368 | assert_debug_snapshot!( |
@@ -315,6 +382,7 @@ mod tests { | |||
315 | kind: Function, | 382 | kind: Function, |
316 | lookup: "main", | 383 | lookup: "main", |
317 | detail: "fn main()", | 384 | detail: "fn main()", |
385 | deprecated: false, | ||
318 | }, | 386 | }, |
319 | CompletionItem { | 387 | CompletionItem { |
320 | label: "no_args()", | 388 | label: "no_args()", |
@@ -324,6 +392,7 @@ mod tests { | |||
324 | kind: Function, | 392 | kind: Function, |
325 | lookup: "no_args", | 393 | lookup: "no_args", |
326 | detail: "fn no_args()", | 394 | detail: "fn no_args()", |
395 | deprecated: false, | ||
327 | }, | 396 | }, |
328 | ] | 397 | ] |
329 | "### | 398 | "### |
@@ -345,6 +414,7 @@ mod tests { | |||
345 | kind: Function, | 414 | kind: Function, |
346 | lookup: "main", | 415 | lookup: "main", |
347 | detail: "fn main()", | 416 | detail: "fn main()", |
417 | deprecated: false, | ||
348 | }, | 418 | }, |
349 | CompletionItem { | 419 | CompletionItem { |
350 | label: "with_args(…)", | 420 | label: "with_args(…)", |
@@ -354,6 +424,7 @@ mod tests { | |||
354 | kind: Function, | 424 | kind: Function, |
355 | lookup: "with_args", | 425 | lookup: "with_args", |
356 | detail: "fn with_args(x: i32, y: String)", | 426 | detail: "fn with_args(x: i32, y: String)", |
427 | deprecated: false, | ||
357 | }, | 428 | }, |
358 | ] | 429 | ] |
359 | "### | 430 | "### |
@@ -380,6 +451,7 @@ mod tests { | |||
380 | kind: Method, | 451 | kind: Method, |
381 | lookup: "foo", | 452 | lookup: "foo", |
382 | detail: "fn foo(&self)", | 453 | detail: "fn foo(&self)", |
454 | deprecated: false, | ||
383 | }, | 455 | }, |
384 | ] | 456 | ] |
385 | "### | 457 | "### |
@@ -404,6 +476,7 @@ mod tests { | |||
404 | insert: "foo", | 476 | insert: "foo", |
405 | kind: Function, | 477 | kind: Function, |
406 | detail: "pub fn foo()", | 478 | detail: "pub fn foo()", |
479 | deprecated: false, | ||
407 | }, | 480 | }, |
408 | ]"# | 481 | ]"# |
409 | ); | 482 | ); |
@@ -429,6 +502,7 @@ mod tests { | |||
429 | insert: "frobnicate", | 502 | insert: "frobnicate", |
430 | kind: Function, | 503 | kind: Function, |
431 | detail: "fn frobnicate()", | 504 | detail: "fn frobnicate()", |
505 | deprecated: false, | ||
432 | }, | 506 | }, |
433 | CompletionItem { | 507 | CompletionItem { |
434 | label: "main", | 508 | label: "main", |
@@ -437,6 +511,7 @@ mod tests { | |||
437 | insert: "main", | 511 | insert: "main", |
438 | kind: Function, | 512 | kind: Function, |
439 | detail: "fn main()", | 513 | detail: "fn main()", |
514 | deprecated: false, | ||
440 | }, | 515 | }, |
441 | ]"# | 516 | ]"# |
442 | ); | 517 | ); |
@@ -459,6 +534,7 @@ mod tests { | |||
459 | insert: "new", | 534 | insert: "new", |
460 | kind: Function, | 535 | kind: Function, |
461 | detail: "fn new() -> Foo", | 536 | detail: "fn new() -> Foo", |
537 | deprecated: false, | ||
462 | }, | 538 | }, |
463 | ]"# | 539 | ]"# |
464 | ); | 540 | ); |
@@ -492,6 +568,7 @@ mod tests { | |||
492 | kind: Function, | 568 | kind: Function, |
493 | lookup: "foo", | 569 | lookup: "foo", |
494 | detail: "fn foo(xs: Ve)", | 570 | detail: "fn foo(xs: Ve)", |
571 | deprecated: false, | ||
495 | }, | 572 | }, |
496 | ] | 573 | ] |
497 | "### | 574 | "### |
@@ -521,6 +598,7 @@ mod tests { | |||
521 | kind: Function, | 598 | kind: Function, |
522 | lookup: "foo", | 599 | lookup: "foo", |
523 | detail: "fn foo(xs: Ve)", | 600 | detail: "fn foo(xs: Ve)", |
601 | deprecated: false, | ||
524 | }, | 602 | }, |
525 | ] | 603 | ] |
526 | "### | 604 | "### |
@@ -549,6 +627,7 @@ mod tests { | |||
549 | kind: Function, | 627 | kind: Function, |
550 | lookup: "foo", | 628 | lookup: "foo", |
551 | detail: "fn foo(xs: Ve)", | 629 | detail: "fn foo(xs: Ve)", |
630 | deprecated: false, | ||
552 | }, | 631 | }, |
553 | ] | 632 | ] |
554 | "### | 633 | "### |
@@ -577,6 +656,7 @@ mod tests { | |||
577 | kind: Function, | 656 | kind: Function, |
578 | lookup: "foo", | 657 | lookup: "foo", |
579 | detail: "fn foo(xs: Ve<i128>)", | 658 | detail: "fn foo(xs: Ve<i128>)", |
659 | deprecated: false, | ||
580 | }, | 660 | }, |
581 | ] | 661 | ] |
582 | "### | 662 | "### |
@@ -607,6 +687,7 @@ mod tests { | |||
607 | insert: "frobnicate", | 687 | insert: "frobnicate", |
608 | kind: Macro, | 688 | kind: Macro, |
609 | detail: "#[macro_export]\nmacro_rules! frobnicate", | 689 | detail: "#[macro_export]\nmacro_rules! frobnicate", |
690 | deprecated: false, | ||
610 | }, | 691 | }, |
611 | ] | 692 | ] |
612 | "### | 693 | "### |