1 //! Completion for derives
2 use hir::{HasAttrs, MacroDef, MacroKind};
4 helpers::{import_assets::ImportAssets, insert_use::ImportScope},
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(acc: &mut Completions, ctx: &CompletionContext, attr: &ast::Attr) {
17 let core = ctx.famous_defs().core();
18 let existing_derives: FxHashSet<_> =
19 ctx.sema.resolve_derive_macro(attr).into_iter().flatten().flatten().collect();
21 for (name, mac) in get_derives_in_scope(ctx) {
22 if existing_derives.contains(&mac) {
26 let name = name.to_smol_str();
27 let (label, lookup) = match core.zip(mac.module(ctx.db).map(|it| it.krate())) {
28 // show derive dependencies for `core`/`std` derives
29 Some((core, mac_krate)) if core == mac_krate => {
30 if let Some(derive_completion) = DEFAULT_DERIVE_DEPENDENCIES
32 .find(|derive_completion| derive_completion.label == name)
34 let mut components = vec![derive_completion.label];
35 components.extend(derive_completion.dependencies.iter().filter(
39 .filter_map(|it| it.name(ctx.db))
40 .any(|it| it.to_smol_str() == dependency)
43 let lookup = components.join(", ");
44 let label = Itertools::intersperse(components.into_iter().rev(), ", ");
45 (SmolStr::from_iter(label), Some(lookup))
53 let mut item = CompletionItem::new(SymbolKind::Derive, ctx.source_range(), label);
54 if let Some(docs) = mac.docs(ctx.db) {
55 item.documentation(docs);
57 if let Some(lookup) = lookup {
58 item.lookup_by(lookup);
63 flyimport_derive(acc, ctx);
66 fn get_derives_in_scope(ctx: &CompletionContext) -> Vec<(hir::Name, MacroDef)> {
67 let mut result = Vec::default();
68 ctx.process_all_names(&mut |name, scope_def| {
69 if let hir::ScopeDef::MacroDef(mac) = scope_def {
70 if mac.kind() == hir::MacroKind::Derive {
71 result.push((name, mac));
78 fn flyimport_derive(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
79 if ctx.token.kind() != SyntaxKind::IDENT {
82 let potential_import_name = ctx.token.to_string();
83 let module = ctx.module?;
84 let parent = ctx.token.parent()?;
85 let user_input_lowercased = potential_import_name.to_lowercase();
86 let import_assets = ImportAssets::for_fuzzy_path(
89 potential_import_name,
93 let import_scope = ImportScope::find_insert_use_container(&parent, &ctx.sema)?;
96 .search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
98 .filter_map(|import| match import.original_item {
99 hir::ItemInNs::Macros(mac) => Some((import, mac)),
102 .filter(|&(_, mac)| mac.kind() == MacroKind::Derive)
103 .filter(|&(_, mac)| !ctx.is_item_hidden(&hir::ItemInNs::Macros(mac)))
104 .sorted_by_key(|(import, _)| {
105 compute_fuzzy_completion_order_key(&import.import_path, &user_input_lowercased)
107 .filter_map(|(import, mac)| {
108 let mut item = CompletionItem::new(
111 mac.name(ctx.db)?.to_smol_str(),
113 item.add_import(ImportEdit { import, scope: import_scope.clone() });
114 if let Some(docs) = mac.docs(ctx.db) {
115 item.documentation(docs);
123 struct DeriveDependencies {
125 dependencies: &'static [&'static str],
128 /// Standard Rust derives that have dependencies
129 /// (the dependencies are needed so that the main derive don't break the compilation when added)
130 const DEFAULT_DERIVE_DEPENDENCIES: &[DeriveDependencies] = &[
131 DeriveDependencies { label: "Copy", dependencies: &["Clone"] },
132 DeriveDependencies { label: "Eq", dependencies: &["PartialEq"] },
133 DeriveDependencies { label: "Ord", dependencies: &["PartialOrd", "Eq", "PartialEq"] },
134 DeriveDependencies { label: "PartialOrd", dependencies: &["PartialEq"] },