]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide_completion/src/completions/attribute/derive.rs
Merge #11393
[rust.git] / crates / ide_completion / src / completions / attribute / derive.rs
index e460a91102ce37e5bc7366b269dde98a72d57a98..29fe096e135608fd9c6817a000c5cb4aee92d7f3 100644 (file)
@@ -1,27 +1,22 @@
 //! Completion for derives
 use hir::{HasAttrs, MacroDef, MacroKind};
-use ide_db::helpers::FamousDefs;
+use ide_db::{
+    helpers::{import_assets::ImportAssets, insert_use::ImportScope},
+    SymbolKind,
+};
 use itertools::Itertools;
 use rustc_hash::FxHashSet;
-use syntax::ast;
+use syntax::{ast, SmolStr, SyntaxKind};
 
 use crate::{
-    context::CompletionContext,
-    item::{CompletionItem, CompletionItemKind, CompletionKind},
-    Completions,
+    completions::flyimport::compute_fuzzy_completion_order_key, context::CompletionContext,
+    item::CompletionItem, Completions, ImportEdit,
 };
 
-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();
+pub(super) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext, attr: &ast::Attr) {
+    let core = ctx.famous_defs().core();
+    let existing_derives: FxHashSet<_> =
+        ctx.sema.resolve_derive_macro(attr).into_iter().flatten().flatten().collect();
 
     for (name, mac) in get_derives_in_scope(ctx) {
         if existing_derives.contains(&mac) {
@@ -29,7 +24,6 @@ pub(super) fn complete_derive(
         }
 
         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 => {
@@ -47,17 +41,16 @@ pub(super) fn complete_derive(
                         },
                     ));
                     let lookup = components.join(", ");
-                    label = components.iter().rev().join(", ");
-                    (label.as_str(), Some(lookup))
+                    let label = Itertools::intersperse(components.into_iter().rev(), ", ");
+                    (SmolStr::from_iter(label), Some(lookup))
                 } else {
-                    (&*name, None)
+                    (name, None)
                 }
             }
-            _ => (&*name, None),
+            _ => (name, None),
         };
 
-        let mut item = CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label);
-        item.kind(CompletionItemKind::Attribute);
+        let mut item = CompletionItem::new(SymbolKind::Derive, ctx.source_range(), label);
         if let Some(docs) = mac.docs(ctx.db) {
             item.documentation(docs);
         }
@@ -66,6 +59,8 @@ pub(super) fn complete_derive(
         }
         item.add_to(acc);
     }
+
+    flyimport_derive(acc, ctx);
 }
 
 fn get_derives_in_scope(ctx: &CompletionContext) -> Vec<(hir::Name, MacroDef)> {
@@ -80,6 +75,51 @@ fn get_derives_in_scope(ctx: &CompletionContext) -> Vec<(hir::Name, MacroDef)> {
     result
 }
 
+fn flyimport_derive(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
+    if ctx.token.kind() != SyntaxKind::IDENT {
+        return None;
+    };
+    let potential_import_name = ctx.token.to_string();
+    let module = ctx.module?;
+    let parent = ctx.token.parent()?;
+    let user_input_lowercased = potential_import_name.to_lowercase();
+    let import_assets = ImportAssets::for_fuzzy_path(
+        module,
+        None,
+        potential_import_name,
+        &ctx.sema,
+        parent.clone(),
+    )?;
+    let import_scope = ImportScope::find_insert_use_container(&parent, &ctx.sema)?;
+    acc.add_all(
+        import_assets
+            .search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
+            .into_iter()
+            .filter_map(|import| match import.original_item {
+                hir::ItemInNs::Macros(mac) => Some((import, mac)),
+                _ => None,
+            })
+            .filter(|&(_, mac)| mac.kind() == MacroKind::Derive)
+            .filter(|&(_, mac)| !ctx.is_item_hidden(&hir::ItemInNs::Macros(mac)))
+            .sorted_by_key(|(import, _)| {
+                compute_fuzzy_completion_order_key(&import.import_path, &user_input_lowercased)
+            })
+            .filter_map(|(import, mac)| {
+                let mut item = CompletionItem::new(
+                    SymbolKind::Derive,
+                    ctx.source_range(),
+                    mac.name(ctx.db)?.to_smol_str(),
+                );
+                item.add_import(ImportEdit { import, scope: import_scope.clone() });
+                if let Some(docs) = mac.docs(ctx.db) {
+                    item.documentation(docs);
+                }
+                Some(item.build())
+            }),
+    );
+    Some(())
+}
+
 struct DeriveDependencies {
     label: &'static str,
     dependencies: &'static [&'static str],