1 //! Completion for derives
2 use hir::{HasAttrs, MacroDef, MacroKind};
4 helpers::{import_assets::ImportAssets, insert_use::ImportScope, FamousDefs},
7 use itertools::Itertools;
8 use rustc_hash::FxHashSet;
9 use syntax::{ast, SmolStr, SyntaxKind};
12 completions::flyimport::compute_fuzzy_completion_order_key, context::CompletionContext,
13 item::CompletionItem, Completions, ImportEdit,
16 pub(super) fn complete_derive(
17 acc: &mut Completions,
18 ctx: &CompletionContext,
19 existing_derives: &[ast::Path],
21 let core = FamousDefs(&ctx.sema, ctx.krate).core();
22 let existing_derives: FxHashSet<_> = existing_derives
24 .filter_map(|path| ctx.scope.speculative_resolve_as_mac(&path))
25 .filter(|mac| mac.kind() == MacroKind::Derive)
28 for (name, mac) in get_derives_in_scope(ctx) {
29 if existing_derives.contains(&mac) {
33 let name = name.to_smol_str();
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
39 .find(|derive_completion| derive_completion.label == name)
41 let mut components = vec![derive_completion.label];
42 components.extend(derive_completion.dependencies.iter().filter(
46 .filter_map(|it| it.name(ctx.db))
47 .any(|it| it.to_smol_str() == dependency)
50 let lookup = components.join(", ");
51 let label = Itertools::intersperse(components.into_iter().rev(), ", ");
52 (SmolStr::from_iter(label), Some(lookup))
60 let mut item = CompletionItem::new(SymbolKind::Derive, ctx.source_range(), label);
61 if let Some(docs) = mac.docs(ctx.db) {
62 item.documentation(docs);
64 if let Some(lookup) = lookup {
65 item.lookup_by(lookup);
70 flyimport_derive(acc, ctx);
73 fn get_derives_in_scope(ctx: &CompletionContext) -> Vec<(hir::Name, MacroDef)> {
74 let mut result = Vec::default();
75 ctx.process_all_names(&mut |name, scope_def| {
76 if let hir::ScopeDef::MacroDef(mac) = scope_def {
77 if mac.kind() == hir::MacroKind::Derive {
78 result.push((name, mac));
85 fn flyimport_derive(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
86 if ctx.token.kind() != SyntaxKind::IDENT {
89 let potential_import_name = ctx.token.to_string();
90 let module = ctx.scope.module()?;
91 let parent = ctx.token.parent()?;
92 let user_input_lowercased = potential_import_name.to_lowercase();
93 let import_assets = ImportAssets::for_fuzzy_path(
96 potential_import_name,
100 let import_scope = ImportScope::find_insert_use_container(&parent, &ctx.sema)?;
103 .search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
105 .filter_map(|import| match import.original_item {
106 hir::ItemInNs::Macros(mac) => Some((import, mac)),
109 .filter(|&(_, mac)| mac.kind() == MacroKind::Derive)
110 .filter(|&(_, mac)| !ctx.is_item_hidden(&hir::ItemInNs::Macros(mac)))
111 .sorted_by_key(|(import, _)| {
112 compute_fuzzy_completion_order_key(&import.import_path, &user_input_lowercased)
114 .filter_map(|(import, mac)| {
115 let mut item = CompletionItem::new(
118 mac.name(ctx.db)?.to_smol_str(),
120 item.add_import(ImportEdit { import, scope: import_scope.clone() });
121 if let Some(docs) = mac.docs(ctx.db) {
122 item.documentation(docs);
130 struct DeriveDependencies {
132 dependencies: &'static [&'static str],
135 /// Standard Rust derives that have dependencies
136 /// (the dependencies are needed so that the main derive don't break the compilation when added)
137 const DEFAULT_DERIVE_DEPENDENCIES: &[DeriveDependencies] = &[
138 DeriveDependencies { label: "Copy", dependencies: &["Clone"] },
139 DeriveDependencies { label: "Eq", dependencies: &["PartialEq"] },
140 DeriveDependencies { label: "Ord", dependencies: &["PartialOrd", "Eq", "PartialEq"] },
141 DeriveDependencies { label: "PartialOrd", dependencies: &["PartialEq"] },