1 //! Completion for derives
3 use itertools::Itertools;
4 use rustc_hash::FxHashMap;
8 context::CompletionContext,
9 item::{CompletionItem, CompletionItemKind, CompletionKind},
13 pub(super) fn complete_derive(
14 acc: &mut Completions,
15 ctx: &CompletionContext,
16 derive_input: ast::TokenTree,
18 if let Some(existing_derives) = super::parse_comma_sep_input(derive_input) {
19 for (derive, docs) in get_derive_names_in_scope(ctx) {
20 let (label, lookup) = if let Some(derive_completion) = DEFAULT_DERIVE_COMPLETIONS
22 .find(|derive_completion| derive_completion.label == derive)
24 let mut components = vec![derive_completion.label];
29 .filter(|&&dependency| !existing_derives.contains(dependency)),
31 let lookup = components.join(", ");
32 let label = components.iter().rev().join(", ");
34 } else if existing_derives.contains(&derive) {
40 CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label);
41 item.kind(CompletionItemKind::Attribute);
42 if let Some(docs) = docs {
43 item.documentation(docs);
45 if let Some(lookup) = lookup {
46 item.lookup_by(lookup);
53 fn get_derive_names_in_scope(
54 ctx: &CompletionContext,
55 ) -> FxHashMap<String, Option<hir::Documentation>> {
56 let mut result = FxHashMap::default();
57 ctx.scope.process_all_names(&mut |name, scope_def| {
58 if let hir::ScopeDef::MacroDef(mac) = scope_def {
59 if mac.kind() == hir::MacroKind::Derive {
60 result.insert(name.to_string(), mac.docs(ctx.db));
67 struct DeriveDependencies {
69 dependencies: &'static [&'static str],
72 /// Standard Rust derives that have dependencies
73 /// (the dependencies are needed so that the main derive don't break the compilation when added)
74 const DEFAULT_DERIVE_COMPLETIONS: &[DeriveDependencies] = &[
75 DeriveDependencies { label: "Copy", dependencies: &["Clone"] },
76 DeriveDependencies { label: "Eq", dependencies: &["PartialEq"] },
77 DeriveDependencies { label: "Ord", dependencies: &["PartialOrd", "Eq", "PartialEq"] },
78 DeriveDependencies { label: "PartialOrd", dependencies: &["PartialEq"] },
83 use expect_test::{expect, Expect};
85 use crate::{tests::filtered_completion_list, CompletionKind};
87 fn check(ra_fixture: &str, expect: Expect) {
88 let builtin_derives = r#"
89 #[rustc_builtin_macro]
91 #[rustc_builtin_macro]
93 #[rustc_builtin_macro]
95 #[rustc_builtin_macro]
97 #[rustc_builtin_macro]
99 #[rustc_builtin_macro]
100 pub macro PartialEq {}
101 #[rustc_builtin_macro]
103 #[rustc_builtin_macro]
104 pub macro PartialOrd {}
105 #[rustc_builtin_macro]
109 let actual = filtered_completion_list(
110 &format!("{} {}", builtin_derives, ra_fixture),
111 CompletionKind::Attribute,
113 expect.assert_eq(&actual);
117 fn no_completion_for_incorrect_derive() {
118 check(r#"#[derive{$0)] struct Test;"#, expect![[]])
124 r#"#[derive($0)] struct Test;"#,
129 at PartialEq, Eq, PartialOrd, Ord
134 at PartialEq, PartialOrd
140 fn derive_with_input() {
142 r#"#[derive(serde::Serialize, PartialEq, $0)] struct Test;"#,
146 at Eq, PartialOrd, Ord
157 fn derive_with_input2() {
159 r#"#[derive($0 serde::Serialize, PartialEq)] struct Test;"#,
163 at Eq, PartialOrd, Ord