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
//! Completion for derives
use hir::{HasAttrs, Macro};
use ide_db::SymbolKind;
use itertools::Itertools;
use syntax::SmolStr;

use crate::{
    context::{CompletionContext, PathCompletionCtx, PathKind},
    item::CompletionItem,
    Completions,
};

pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) {
    match ctx.path_context {
        // FIXME: Enable qualified completions
        Some(PathCompletionCtx { kind: Some(PathKind::Derive), qualifier: None, .. }) => (),
        _ => return,
    }

    let core = ctx.famous_defs().core();

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

        let name = name.to_smol_str();
        let (label, lookup) = match (core, mac.module(ctx.db).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| {
                            !ctx.existing_derives
                                .iter()
                                .map(|it| it.name(ctx.db))
                                .any(|it| it.to_smol_str() == dependency)
                        },
                    ));
                    let lookup = components.join(", ");
                    let label = Itertools::intersperse(components.into_iter().rev(), ", ");
                    (SmolStr::from_iter(label), Some(lookup))
                } else {
                    (name, None)
                }
            }
            _ => (name, None),
        };

        let mut item = CompletionItem::new(SymbolKind::Derive, ctx.source_range(), label);
        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, Macro)> {
    let mut result = Vec::default();
    ctx.process_all_names(&mut |name, scope_def| {
        if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) = scope_def {
            if mac.kind(ctx.db) == 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"] },
];