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