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