Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! Completion for derives
use hir::{HasAttrs, MacroDef, MacroKind};
use ide_db::helpers::FamousDefs;
use itertools::Itertools;
use rustc_hash::FxHashSet;
use syntax::ast;

use crate::{
    context::CompletionContext,
    item::{CompletionItem, CompletionItemKind, CompletionKind},
    Completions,
};

pub(super) fn complete_derive(
    acc: &mut Completions,
    ctx: &CompletionContext,
    existing_derives: &[ast::Path],
) {
    let core = FamousDefs(&ctx.sema, ctx.krate).core();
    let existing_derives: FxHashSet<_> = existing_derives
        .into_iter()
        .filter_map(|path| ctx.scope.speculative_resolve_as_mac(&path))
        .filter(|mac| mac.kind() == MacroKind::Derive)
        .collect();

    for (name, mac) in get_derives_in_scope(ctx) {
        if existing_derives.contains(&mac) {
            continue;
        }

        let name = name.to_smol_str();
        let label;
        let (label, lookup) = match core.zip(mac.module(ctx.db).map(|it| it.krate())) {
            // show derive dependencies for `core`/`std` derives
            Some((core, mac_krate)) if core == mac_krate => {
                if let Some(derive_completion) = DEFAULT_DERIVE_DEPENDENCIES
                    .iter()
                    .find(|derive_completion| derive_completion.label == name)
                {
                    let mut components = vec![derive_completion.label];
                    components.extend(derive_completion.dependencies.iter().filter(
                        |&&dependency| {
                            !existing_derives
                                .iter()
                                .filter_map(|it| it.name(ctx.db))
                                .any(|it| it.to_smol_str() == dependency)
                        },
                    ));
                    let lookup = components.join(", ");
                    label = components.iter().rev().join(", ");
                    (label.as_str(), Some(lookup))
                } else {
                    (&*name, None)
                }
            }
            _ => (&*name, None),
        };

        let mut item = CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label);
        item.kind(CompletionItemKind::Attribute);
        if let Some(docs) = mac.docs(ctx.db) {
            item.documentation(docs);
        }
        if let Some(lookup) = lookup {
            item.lookup_by(lookup);
        }
        item.add_to(acc);
    }
}

fn get_derives_in_scope(ctx: &CompletionContext) -> Vec<(hir::Name, MacroDef)> {
    let mut result = Vec::default();
    ctx.process_all_names(&mut |name, scope_def| {
        if let hir::ScopeDef::MacroDef(mac) = scope_def {
            if mac.kind() == hir::MacroKind::Derive {
                result.push((name, mac));
            }
        }
    });
    result
}

struct DeriveDependencies {
    label: &'static str,
    dependencies: &'static [&'static str],
}

/// Standard Rust derives that have dependencies
/// (the dependencies are needed so that the main derive don't break the compilation when added)
const DEFAULT_DERIVE_DEPENDENCIES: &[DeriveDependencies] = &[
    DeriveDependencies { label: "Copy", dependencies: &["Clone"] },
    DeriveDependencies { label: "Eq", dependencies: &["PartialEq"] },
    DeriveDependencies { label: "Ord", dependencies: &["PartialOrd", "Eq", "PartialEq"] },
    DeriveDependencies { label: "PartialOrd", dependencies: &["PartialEq"] },
];