]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/completions/attribute.rs
fix: Don't duplicate attribute completions
[rust.git] / crates / ide_completion / src / completions / attribute.rs
1 //! Completion for attributes
2 //!
3 //! This module uses a bit of static metadata to provide completions
4 //! for built-in attributes.
5 //! Non-builtin attribute(excluding derives attributes) completions are done in [`super::unqualified_path`].
6
7 use ide_db::{
8     helpers::{
9         generated_lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES, RUSTDOC_LINTS},
10         parse_tt_as_comma_sep_paths,
11     },
12     SymbolKind,
13 };
14 use itertools::Itertools;
15 use once_cell::sync::Lazy;
16 use rustc_hash::FxHashMap;
17 use syntax::{algo::non_trivia_sibling, ast, AstNode, Direction, SyntaxKind, T};
18
19 use crate::{context::CompletionContext, item::CompletionItem, Completions};
20
21 mod cfg;
22 mod derive;
23 mod lint;
24 mod repr;
25
26 pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
27     let attribute = ctx.attribute_under_caret.as_ref()?;
28     let name_ref = match attribute.path() {
29         Some(p) => Some(p.as_single_name_ref()?),
30         None => None,
31     };
32     match (name_ref, attribute.token_tree()) {
33         (Some(path), Some(tt)) if tt.l_paren_token().is_some() => match path.text().as_str() {
34             "repr" => repr::complete_repr(acc, ctx, tt),
35             "derive" => derive::complete_derive(acc, ctx, &parse_tt_as_comma_sep_paths(tt)?),
36             "feature" => lint::complete_lint(acc, ctx, &parse_tt_as_comma_sep_paths(tt)?, FEATURES),
37             "allow" | "warn" | "deny" | "forbid" => {
38                 let existing_lints = parse_tt_as_comma_sep_paths(tt)?;
39                 lint::complete_lint(acc, ctx, &existing_lints, DEFAULT_LINTS);
40                 lint::complete_lint(acc, ctx, &existing_lints, CLIPPY_LINTS);
41                 lint::complete_lint(acc, ctx, &existing_lints, RUSTDOC_LINTS);
42             }
43             "cfg" => {
44                 cfg::complete_cfg(acc, ctx);
45             }
46             _ => (),
47         },
48         (_, Some(_)) => (),
49         (_, None) => complete_new_attribute(acc, ctx, attribute),
50     }
51     Some(())
52 }
53
54 fn complete_new_attribute(acc: &mut Completions, ctx: &CompletionContext, attribute: &ast::Attr) {
55     let is_inner = attribute.kind() == ast::AttrKind::Inner;
56     let attribute_annotated_item_kind =
57         attribute.syntax().parent().map(|it| it.kind()).filter(|_| {
58             is_inner
59             // If we got nothing coming after the attribute it could be anything so filter it the kind out
60                 || non_trivia_sibling(attribute.syntax().clone().into(), Direction::Next).is_some()
61         });
62     let attributes = attribute_annotated_item_kind.and_then(|kind| {
63         if ast::Expr::can_cast(kind) {
64             Some(EXPR_ATTRIBUTES)
65         } else {
66             KIND_TO_ATTRIBUTES.get(&kind).copied()
67         }
68     });
69
70     let add_completion = |attr_completion: &AttrCompletion| {
71         let mut item =
72             CompletionItem::new(SymbolKind::Attribute, ctx.source_range(), attr_completion.label);
73
74         if let Some(lookup) = attr_completion.lookup {
75             item.lookup_by(lookup);
76         }
77
78         if let Some((snippet, cap)) = attr_completion.snippet.zip(ctx.config.snippet_cap) {
79             item.insert_snippet(cap, snippet);
80         }
81
82         if is_inner || !attr_completion.prefer_inner {
83             item.add_to(acc);
84         }
85     };
86
87     match attributes {
88         Some(applicable) => applicable
89             .iter()
90             .flat_map(|name| ATTRIBUTES.binary_search_by(|attr| attr.key().cmp(name)).ok())
91             .flat_map(|idx| ATTRIBUTES.get(idx))
92             .for_each(add_completion),
93         None if is_inner => ATTRIBUTES.iter().for_each(add_completion),
94         None => ATTRIBUTES.iter().filter(|compl| !compl.prefer_inner).for_each(add_completion),
95     }
96 }
97
98 struct AttrCompletion {
99     label: &'static str,
100     lookup: Option<&'static str>,
101     snippet: Option<&'static str>,
102     prefer_inner: bool,
103 }
104
105 impl AttrCompletion {
106     fn key(&self) -> &'static str {
107         self.lookup.unwrap_or(self.label)
108     }
109
110     const fn prefer_inner(self) -> AttrCompletion {
111         AttrCompletion { prefer_inner: true, ..self }
112     }
113 }
114
115 const fn attr(
116     label: &'static str,
117     lookup: Option<&'static str>,
118     snippet: Option<&'static str>,
119 ) -> AttrCompletion {
120     AttrCompletion { label, lookup, snippet, prefer_inner: false }
121 }
122
123 macro_rules! attrs {
124     // attributes applicable to all items
125     [@ { item $($tt:tt)* } {$($acc:tt)*}] => {
126         attrs!(@ { $($tt)* } { $($acc)*, "deprecated", "doc", "dochidden", "docalias", "must_use", "no_mangle" })
127     };
128     // attributes applicable to all adts
129     [@ { adt $($tt:tt)* } {$($acc:tt)*}] => {
130         attrs!(@ { $($tt)* } { $($acc)*, "derive", "repr" })
131     };
132     // attributes applicable to all linkable things aka functions/statics
133     [@ { linkable $($tt:tt)* } {$($acc:tt)*}] => {
134         attrs!(@ { $($tt)* } { $($acc)*, "export_name", "link_name", "link_section" })
135     };
136     // error fallback for nicer error message
137     [@ { $ty:ident $($tt:tt)* } {$($acc:tt)*}] => {
138         compile_error!(concat!("unknown attr subtype ", stringify!($ty)))
139     };
140     // general push down accumulation
141     [@ { $lit:literal $($tt:tt)*} {$($acc:tt)*}] => {
142         attrs!(@ { $($tt)* } { $($acc)*, $lit })
143     };
144     [@ {$($tt:tt)+} {$($tt2:tt)*}] => {
145         compile_error!(concat!("Unexpected input ", stringify!($($tt)+)))
146     };
147     // final output construction
148     [@ {} {$($tt:tt)*}] => { &[$($tt)*] as _ };
149     // starting matcher
150     [$($tt:tt),*] => {
151         attrs!(@ { $($tt)* } { "allow", "cfg", "cfg_attr", "deny", "forbid", "warn" })
152     };
153 }
154
155 #[rustfmt::skip]
156 static KIND_TO_ATTRIBUTES: Lazy<FxHashMap<SyntaxKind, &[&str]>> = Lazy::new(|| {
157     use SyntaxKind::*;
158     [
159         (
160             SOURCE_FILE,
161             attrs!(
162                 item,
163                 "crate_name", "feature", "no_implicit_prelude", "no_main", "no_std",
164                 "recursion_limit", "type_length_limit", "windows_subsystem"
165             ),
166         ),
167         (MODULE, attrs!(item, "macro_use", "no_implicit_prelude", "path")),
168         (ITEM_LIST, attrs!(item, "no_implicit_prelude")),
169         (MACRO_RULES, attrs!(item, "macro_export", "macro_use")),
170         (MACRO_DEF, attrs!(item)),
171         (EXTERN_CRATE, attrs!(item, "macro_use", "no_link")),
172         (USE, attrs!(item)),
173         (TYPE_ALIAS, attrs!(item)),
174         (STRUCT, attrs!(item, adt, "non_exhaustive")),
175         (ENUM, attrs!(item, adt, "non_exhaustive")),
176         (UNION, attrs!(item, adt)),
177         (CONST, attrs!(item)),
178         (
179             FN,
180             attrs!(
181                 item, linkable,
182                 "cold", "ignore", "inline", "must_use", "panic_handler", "proc_macro",
183                 "proc_macro_derive", "proc_macro_attribute", "should_panic", "target_feature",
184                 "test", "track_caller"
185             ),
186         ),
187         (STATIC, attrs!(item, linkable, "global_allocator", "used")),
188         (TRAIT, attrs!(item, "must_use")),
189         (IMPL, attrs!(item, "automatically_derived")),
190         (ASSOC_ITEM_LIST, attrs!(item)),
191         (EXTERN_BLOCK, attrs!(item, "link")),
192         (EXTERN_ITEM_LIST, attrs!(item, "link")),
193         (MACRO_CALL, attrs!()),
194         (SELF_PARAM, attrs!()),
195         (PARAM, attrs!()),
196         (RECORD_FIELD, attrs!()),
197         (VARIANT, attrs!("non_exhaustive")),
198         (TYPE_PARAM, attrs!()),
199         (CONST_PARAM, attrs!()),
200         (LIFETIME_PARAM, attrs!()),
201         (LET_STMT, attrs!()),
202         (EXPR_STMT, attrs!()),
203         (LITERAL, attrs!()),
204         (RECORD_EXPR_FIELD_LIST, attrs!()),
205         (RECORD_EXPR_FIELD, attrs!()),
206         (MATCH_ARM_LIST, attrs!()),
207         (MATCH_ARM, attrs!()),
208         (IDENT_PAT, attrs!()),
209         (RECORD_PAT_FIELD, attrs!()),
210     ]
211     .into_iter()
212     .collect()
213 });
214 const EXPR_ATTRIBUTES: &[&str] = attrs!();
215
216 /// <https://doc.rust-lang.org/reference/attributes.html#built-in-attributes-index>
217 // Keep these sorted for the binary search!
218 const ATTRIBUTES: &[AttrCompletion] = &[
219     attr("allow(…)", Some("allow"), Some("allow(${0:lint})")),
220     attr("automatically_derived", None, None),
221     attr("cfg(…)", Some("cfg"), Some("cfg(${0:predicate})")),
222     attr("cfg_attr(…)", Some("cfg_attr"), Some("cfg_attr(${1:predicate}, ${0:attr})")),
223     attr("cold", None, None),
224     attr(r#"crate_name = """#, Some("crate_name"), Some(r#"crate_name = "${0:crate_name}""#))
225         .prefer_inner(),
226     attr("deny(…)", Some("deny"), Some("deny(${0:lint})")),
227     attr(r#"deprecated"#, Some("deprecated"), Some(r#"deprecated"#)),
228     attr("derive(…)", Some("derive"), Some(r#"derive(${0:Debug})"#)),
229     attr(r#"doc = "…""#, Some("doc"), Some(r#"doc = "${0:docs}""#)),
230     attr(r#"doc(alias = "…")"#, Some("docalias"), Some(r#"doc(alias = "${0:docs}")"#)),
231     attr(r#"doc(hidden)"#, Some("dochidden"), Some(r#"doc(hidden)"#)),
232     attr(
233         r#"export_name = "…""#,
234         Some("export_name"),
235         Some(r#"export_name = "${0:exported_symbol_name}""#),
236     ),
237     attr("feature(…)", Some("feature"), Some("feature(${0:flag})")).prefer_inner(),
238     attr("forbid(…)", Some("forbid"), Some("forbid(${0:lint})")),
239     attr("global_allocator", None, None),
240     attr(r#"ignore = "…""#, Some("ignore"), Some(r#"ignore = "${0:reason}""#)),
241     attr("inline", Some("inline"), Some("inline")),
242     attr("link", None, None),
243     attr(r#"link_name = "…""#, Some("link_name"), Some(r#"link_name = "${0:symbol_name}""#)),
244     attr(
245         r#"link_section = "…""#,
246         Some("link_section"),
247         Some(r#"link_section = "${0:section_name}""#),
248     ),
249     attr("macro_export", None, None),
250     attr("macro_use", None, None),
251     attr(r#"must_use"#, Some("must_use"), Some(r#"must_use"#)),
252     attr("no_implicit_prelude", None, None).prefer_inner(),
253     attr("no_link", None, None).prefer_inner(),
254     attr("no_main", None, None).prefer_inner(),
255     attr("no_mangle", None, None),
256     attr("no_std", None, None).prefer_inner(),
257     attr("non_exhaustive", None, None),
258     attr("panic_handler", None, None),
259     attr(r#"path = "…""#, Some("path"), Some(r#"path ="${0:path}""#)),
260     attr("proc_macro", None, None),
261     attr("proc_macro_attribute", None, None),
262     attr("proc_macro_derive(…)", Some("proc_macro_derive"), Some("proc_macro_derive(${0:Trait})")),
263     attr(
264         r#"recursion_limit = "…""#,
265         Some("recursion_limit"),
266         Some(r#"recursion_limit = "${0:128}""#),
267     )
268     .prefer_inner(),
269     attr("repr(…)", Some("repr"), Some("repr(${0:C})")),
270     attr("should_panic", Some("should_panic"), Some(r#"should_panic"#)),
271     attr(
272         r#"target_feature = "…""#,
273         Some("target_feature"),
274         Some(r#"target_feature = "${0:feature}""#),
275     ),
276     attr("test", None, None),
277     attr("track_caller", None, None),
278     attr("type_length_limit = …", Some("type_length_limit"), Some("type_length_limit = ${0:128}"))
279         .prefer_inner(),
280     attr("used", None, None),
281     attr("warn(…)", Some("warn"), Some("warn(${0:lint})")),
282     attr(
283         r#"windows_subsystem = "…""#,
284         Some("windows_subsystem"),
285         Some(r#"windows_subsystem = "${0:subsystem}""#),
286     )
287     .prefer_inner(),
288 ];
289
290 fn parse_comma_sep_expr(input: ast::TokenTree) -> Option<Vec<ast::Expr>> {
291     let r_paren = input.r_paren_token()?;
292     let tokens = input
293         .syntax()
294         .children_with_tokens()
295         .skip(1)
296         .take_while(|it| it.as_token() != Some(&r_paren));
297     let input_expressions = tokens.into_iter().group_by(|tok| tok.kind() == T![,]);
298     Some(
299         input_expressions
300             .into_iter()
301             .filter_map(|(is_sep, group)| (!is_sep).then(|| group))
302             .filter_map(|mut tokens| ast::Expr::parse(&tokens.join("")).ok())
303             .collect::<Vec<ast::Expr>>(),
304     )
305 }
306
307 #[test]
308 fn attributes_are_sorted() {
309     let mut attrs = ATTRIBUTES.iter().map(|attr| attr.key());
310     let mut prev = attrs.next().unwrap();
311
312     attrs.for_each(|next| {
313         assert!(
314             prev < next,
315             r#"ATTRIBUTES array is not sorted, "{}" should come after "{}""#,
316             prev,
317             next
318         );
319         prev = next;
320     });
321 }