]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/completions/attribute/derive.rs
Merge #10587
[rust.git] / crates / ide_completion / src / completions / attribute / derive.rs
1 //! Completion for derives
2 use hir::{HasAttrs, MacroDef, MacroKind};
3 use ide_db::helpers::FamousDefs;
4 use itertools::Itertools;
5 use rustc_hash::FxHashSet;
6 use syntax::ast;
7
8 use crate::{
9     context::CompletionContext,
10     item::{CompletionItem, CompletionItemKind, CompletionKind},
11     Completions,
12 };
13
14 pub(super) fn complete_derive(
15     acc: &mut Completions,
16     ctx: &CompletionContext,
17     derive_input: ast::TokenTree,
18 ) {
19     if let Some(existing_derives) = super::parse_comma_sep_paths(derive_input.clone()) {
20         let core = FamousDefs(&ctx.sema, ctx.krate).core();
21         let existing_derives: FxHashSet<_> = existing_derives
22             .into_iter()
23             .filter_map(|path| ctx.scope.speculative_resolve_as_mac(&path))
24             .filter(|mac| mac.kind() == MacroKind::Derive)
25             .collect();
26
27         for (name, mac) in get_derives_in_scope(ctx) {
28             if existing_derives.contains(&mac) {
29                 continue;
30             }
31
32             let name = name.to_smol_str();
33             let label;
34             let (label, lookup) = match core.zip(mac.module(ctx.db).map(|it| it.krate())) {
35                 // show derive dependencies for `core`/`std` derives
36                 Some((core, mac_krate)) if core == mac_krate => {
37                     if let Some(derive_completion) = DEFAULT_DERIVE_DEPENDENCIES
38                         .iter()
39                         .find(|derive_completion| derive_completion.label == name)
40                     {
41                         let mut components = vec![derive_completion.label];
42                         components.extend(derive_completion.dependencies.iter().filter(
43                             |&&dependency| {
44                                 !existing_derives
45                                     .iter()
46                                     .filter_map(|it| it.name(ctx.db))
47                                     .any(|it| it.to_smol_str() == dependency)
48                             },
49                         ));
50                         let lookup = components.join(", ");
51                         label = components.iter().rev().join(", ");
52                         (label.as_str(), Some(lookup))
53                     } else {
54                         (&*name, None)
55                     }
56                 }
57                 _ => (&*name, None),
58             };
59
60             let mut item =
61                 CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label);
62             item.kind(CompletionItemKind::Attribute);
63             if let Some(docs) = mac.docs(ctx.db) {
64                 item.documentation(docs);
65             }
66             if let Some(lookup) = lookup {
67                 item.lookup_by(lookup);
68             }
69             item.add_to(acc);
70         }
71     }
72 }
73
74 fn get_derives_in_scope(ctx: &CompletionContext) -> Vec<(hir::Name, MacroDef)> {
75     let mut result = Vec::default();
76     ctx.process_all_names(&mut |name, scope_def| {
77         if let hir::ScopeDef::MacroDef(mac) = scope_def {
78             if mac.kind() == hir::MacroKind::Derive {
79                 result.push((name, mac));
80             }
81         }
82     });
83     result
84 }
85
86 struct DeriveDependencies {
87     label: &'static str,
88     dependencies: &'static [&'static str],
89 }
90
91 /// Standard Rust derives that have dependencies
92 /// (the dependencies are needed so that the main derive don't break the compilation when added)
93 const DEFAULT_DERIVE_DEPENDENCIES: &[DeriveDependencies] = &[
94     DeriveDependencies { label: "Copy", dependencies: &["Clone"] },
95     DeriveDependencies { label: "Eq", dependencies: &["PartialEq"] },
96     DeriveDependencies { label: "Ord", dependencies: &["PartialOrd", "Eq", "PartialEq"] },
97     DeriveDependencies { label: "PartialOrd", dependencies: &["PartialEq"] },
98 ];