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