1 //! Completion for attributes
3 //! This module uses a bit of static metadata to provide completions
4 //! for built-in attributes.
6 use once_cell::sync::Lazy;
7 use rustc_hash::{FxHashMap, FxHashSet};
8 use syntax::{ast, AstNode, SyntaxKind, T};
11 context::CompletionContext,
12 generated_lint_completions::{CLIPPY_LINTS, FEATURES},
13 item::{CompletionItem, CompletionItemKind, CompletionKind},
19 pub(crate) use self::lint::LintCompletion;
21 pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
22 let attribute = ctx.attribute_under_caret.as_ref()?;
23 match (attribute.path().and_then(|p| p.as_single_name_ref()), attribute.token_tree()) {
24 (Some(path), Some(token_tree)) => match path.text().as_str() {
25 "derive" => derive::complete_derive(acc, ctx, token_tree),
26 "feature" => lint::complete_lint(acc, ctx, token_tree, FEATURES),
27 "allow" | "warn" | "deny" | "forbid" => {
28 lint::complete_lint(acc, ctx, token_tree.clone(), lint::DEFAULT_LINT_COMPLETIONS);
29 lint::complete_lint(acc, ctx, token_tree, CLIPPY_LINTS);
33 (None, Some(_)) => (),
34 _ => complete_new_attribute(acc, ctx, attribute),
39 fn complete_new_attribute(acc: &mut Completions, ctx: &CompletionContext, attribute: &ast::Attr) {
40 let attribute_annotated_item_kind = attribute.syntax().parent().map(|it| it.kind());
41 let attributes = attribute_annotated_item_kind.and_then(|kind| {
42 if ast::Expr::can_cast(kind) {
45 KIND_TO_ATTRIBUTES.get(&kind).copied()
48 let is_inner = attribute.kind() == ast::AttrKind::Inner;
50 let add_completion = |attr_completion: &AttrCompletion| {
51 let mut item = CompletionItem::new(
52 CompletionKind::Attribute,
54 attr_completion.label,
56 item.kind(CompletionItemKind::Attribute);
58 if let Some(lookup) = attr_completion.lookup {
59 item.lookup_by(lookup);
62 if let Some((snippet, cap)) = attr_completion.snippet.zip(ctx.config.snippet_cap) {
63 item.insert_snippet(cap, snippet);
66 if is_inner || !attr_completion.prefer_inner {
67 acc.add(item.build());
72 Some(applicable) => applicable
74 .flat_map(|name| ATTRIBUTES.binary_search_by(|attr| attr.key().cmp(name)).ok())
75 .flat_map(|idx| ATTRIBUTES.get(idx))
76 .for_each(add_completion),
77 None if is_inner => ATTRIBUTES.iter().for_each(add_completion),
78 None => ATTRIBUTES.iter().filter(|compl| !compl.prefer_inner).for_each(add_completion),
82 struct AttrCompletion {
84 lookup: Option<&'static str>,
85 snippet: Option<&'static str>,
90 fn key(&self) -> &'static str {
91 self.lookup.unwrap_or(self.label)
94 const fn prefer_inner(self) -> AttrCompletion {
95 AttrCompletion { prefer_inner: true, ..self }
101 lookup: Option<&'static str>,
102 snippet: Option<&'static str>,
103 ) -> AttrCompletion {
104 AttrCompletion { label, lookup, snippet, prefer_inner: false }
108 [@ { item $($tt:tt)* } {$($acc:tt)*}] => {
109 attrs!(@ { $($tt)* } { $($acc)*, "deprecated", "doc", "dochidden", "docalias", "must_use", "no_mangle" })
111 [@ { adt $($tt:tt)* } {$($acc:tt)*}] => {
112 attrs!(@ { $($tt)* } { $($acc)*, "derive", "repr" })
114 [@ { linkable $($tt:tt)* } {$($acc:tt)*}] => {
115 attrs!(@ { $($tt)* } { $($acc)*, "export_name", "link_name", "link_section" }) };
116 [@ { $ty:ident $($tt:tt)* } {$($acc:tt)*}] => { compile_error!(concat!("unknown attr subtype ", stringify!($ty)))
118 [@ { $lit:literal $($tt:tt)*} {$($acc:tt)*}] => {
119 attrs!(@ { $($tt)* } { $($acc)*, $lit })
121 [@ {$($tt:tt)+} {$($tt2:tt)*}] => {
122 compile_error!(concat!("Unexpected input ", stringify!($($tt)+)))
124 [@ {} {$($tt:tt)*}] => { &[$($tt)*] as _ };
126 attrs!(@ { $($tt)* } { "allow", "cfg", "cfg_attr", "deny", "forbid", "warn" })
131 static KIND_TO_ATTRIBUTES: Lazy<FxHashMap<SyntaxKind, &[&str]>> = Lazy::new(|| {
132 std::array::IntoIter::new([
134 SyntaxKind::SOURCE_FILE,
137 "crate_name", "feature", "no_implicit_prelude", "no_main", "no_std",
138 "recursion_limit", "type_length_limit", "windows_subsystem"
141 (SyntaxKind::MODULE, attrs!(item, "no_implicit_prelude", "path")),
142 (SyntaxKind::ITEM_LIST, attrs!(item, "no_implicit_prelude")),
143 (SyntaxKind::MACRO_RULES, attrs!(item, "macro_export", "macro_use")),
144 (SyntaxKind::MACRO_DEF, attrs!(item)),
145 (SyntaxKind::EXTERN_CRATE, attrs!(item, "macro_use", "no_link")),
146 (SyntaxKind::USE, attrs!(item)),
147 (SyntaxKind::TYPE_ALIAS, attrs!(item)),
148 (SyntaxKind::STRUCT, attrs!(item, adt, "non_exhaustive")),
149 (SyntaxKind::ENUM, attrs!(item, adt, "non_exhaustive")),
150 (SyntaxKind::UNION, attrs!(item, adt)),
151 (SyntaxKind::CONST, attrs!(item)),
156 "cold", "ignore", "inline", "must_use", "panic_handler", "proc_macro",
157 "proc_macro_derive", "proc_macro_attribute", "should_panic", "target_feature",
158 "test", "track_caller"
161 (SyntaxKind::STATIC, attrs!(item, linkable, "global_allocator", "used")),
162 (SyntaxKind::TRAIT, attrs!(item, "must_use")),
163 (SyntaxKind::IMPL, attrs!(item, "automatically_derived")),
164 (SyntaxKind::ASSOC_ITEM_LIST, attrs!(item)),
165 (SyntaxKind::EXTERN_BLOCK, attrs!(item, "link")),
166 (SyntaxKind::EXTERN_ITEM_LIST, attrs!(item, "link")),
167 (SyntaxKind::MACRO_CALL, attrs!()),
168 (SyntaxKind::SELF_PARAM, attrs!()),
169 (SyntaxKind::PARAM, attrs!()),
170 (SyntaxKind::RECORD_FIELD, attrs!()),
171 (SyntaxKind::VARIANT, attrs!("non_exhaustive")),
172 (SyntaxKind::TYPE_PARAM, attrs!()),
173 (SyntaxKind::CONST_PARAM, attrs!()),
174 (SyntaxKind::LIFETIME_PARAM, attrs!()),
175 (SyntaxKind::LET_STMT, attrs!()),
176 (SyntaxKind::EXPR_STMT, attrs!()),
177 (SyntaxKind::LITERAL, attrs!()),
178 (SyntaxKind::RECORD_EXPR_FIELD_LIST, attrs!()),
179 (SyntaxKind::RECORD_EXPR_FIELD, attrs!()),
180 (SyntaxKind::MATCH_ARM_LIST, attrs!()),
181 (SyntaxKind::MATCH_ARM, attrs!()),
182 (SyntaxKind::IDENT_PAT, attrs!()),
183 (SyntaxKind::RECORD_PAT_FIELD, attrs!()),
187 const EXPR_ATTRIBUTES: &[&str] = attrs!();
189 /// https://doc.rust-lang.org/reference/attributes.html#built-in-attributes-index
190 // Keep these sorted for the binary search!
191 const ATTRIBUTES: &[AttrCompletion] = &[
192 attr("allow(…)", Some("allow"), Some("allow(${0:lint})")),
193 attr("automatically_derived", None, None),
194 attr("cfg(…)", Some("cfg"), Some("cfg(${0:predicate})")),
195 attr("cfg_attr(…)", Some("cfg_attr"), Some("cfg_attr(${1:predicate}, ${0:attr})")),
196 attr("cold", None, None),
197 attr(r#"crate_name = """#, Some("crate_name"), Some(r#"crate_name = "${0:crate_name}""#))
199 attr("deny(…)", Some("deny"), Some("deny(${0:lint})")),
200 attr(r#"deprecated"#, Some("deprecated"), Some(r#"deprecated"#)),
201 attr("derive(…)", Some("derive"), Some(r#"derive(${0:Debug})"#)),
202 attr(r#"doc = "…""#, Some("doc"), Some(r#"doc = "${0:docs}""#)),
203 attr(r#"doc(alias = "…")"#, Some("docalias"), Some(r#"doc(alias = "${0:docs}")"#)),
204 attr(r#"doc(hidden)"#, Some("dochidden"), Some(r#"doc(hidden)"#)),
206 r#"export_name = "…""#,
208 Some(r#"export_name = "${0:exported_symbol_name}""#),
210 attr("feature(…)", Some("feature"), Some("feature(${0:flag})")).prefer_inner(),
211 attr("forbid(…)", Some("forbid"), Some("forbid(${0:lint})")),
212 // FIXME: resolve through macro resolution?
213 attr("global_allocator", None, None).prefer_inner(),
214 attr(r#"ignore = "…""#, Some("ignore"), Some(r#"ignore = "${0:reason}""#)),
215 attr("inline", Some("inline"), Some("inline")),
216 attr("link", None, None),
217 attr(r#"link_name = "…""#, Some("link_name"), Some(r#"link_name = "${0:symbol_name}""#)),
219 r#"link_section = "…""#,
220 Some("link_section"),
221 Some(r#"link_section = "${0:section_name}""#),
223 attr("macro_export", None, None),
224 attr("macro_use", None, None),
225 attr(r#"must_use"#, Some("must_use"), Some(r#"must_use"#)),
226 attr("no_implicit_prelude", None, None).prefer_inner(),
227 attr("no_link", None, None).prefer_inner(),
228 attr("no_main", None, None).prefer_inner(),
229 attr("no_mangle", None, None),
230 attr("no_std", None, None).prefer_inner(),
231 attr("non_exhaustive", None, None),
232 attr("panic_handler", None, None).prefer_inner(),
233 attr(r#"path = "…""#, Some("path"), Some(r#"path ="${0:path}""#)),
234 attr("proc_macro", None, None),
235 attr("proc_macro_attribute", None, None),
236 attr("proc_macro_derive(…)", Some("proc_macro_derive"), Some("proc_macro_derive(${0:Trait})")),
237 attr("recursion_limit = …", Some("recursion_limit"), Some("recursion_limit = ${0:128}"))
239 attr("repr(…)", Some("repr"), Some("repr(${0:C})")),
240 attr("should_panic", Some("should_panic"), Some(r#"should_panic"#)),
242 r#"target_feature = "…""#,
243 Some("target_feature"),
244 Some(r#"target_feature = "${0:feature}""#),
246 attr("test", None, None),
247 attr("track_caller", None, None),
248 attr("type_length_limit = …", Some("type_length_limit"), Some("type_length_limit = ${0:128}"))
250 attr("used", None, None),
251 attr("warn(…)", Some("warn"), Some("warn(${0:lint})")),
253 r#"windows_subsystem = "…""#,
254 Some("windows_subsystem"),
255 Some(r#"windows_subsystem = "${0:subsystem}""#),
261 fn attributes_are_sorted() {
262 let mut attrs = ATTRIBUTES.iter().map(|attr| attr.key());
263 let mut prev = attrs.next().unwrap();
265 attrs.for_each(|next| {
268 r#"Attributes are not sorted, "{}" should come after "{}""#,
276 fn parse_comma_sep_input(derive_input: ast::TokenTree) -> Result<FxHashSet<String>, ()> {
277 match (derive_input.left_delimiter_token(), derive_input.right_delimiter_token()) {
278 (Some(left_paren), Some(right_paren))
279 if left_paren.kind() == T!['('] && right_paren.kind() == T![')'] =>
281 let mut input_derives = FxHashSet::default();
282 let mut current_derive = String::new();
283 for token in derive_input
285 .children_with_tokens()
286 .filter_map(|token| token.into_token())
287 .skip_while(|token| token != &left_paren)
289 .take_while(|token| token != &right_paren)
291 if T![,] == token.kind() {
292 if !current_derive.is_empty() {
293 input_derives.insert(current_derive);
294 current_derive = String::new();
297 current_derive.push_str(token.text().trim());
301 if !current_derive.is_empty() {
302 input_derives.insert(current_derive);
312 use expect_test::{expect, Expect};
314 use crate::{test_utils::completion_list, CompletionKind};
316 fn check(ra_fixture: &str, expect: Expect) {
317 let actual = completion_list(ra_fixture, CompletionKind::Attribute);
318 expect.assert_eq(&actual);
322 fn test_attribute_completion_inside_nested_attr() {
323 check(r#"#[cfg($0)]"#, expect![[]])
327 fn test_attribute_completion_with_existing_attr() {
329 r#"#[no_mangle] #[$0] mcall!();"#,
342 fn complete_attribute_on_source_file() {
360 at no_implicit_prelude
363 at recursion_limit = …
364 at type_length_limit = …
365 at windows_subsystem = "…"
371 fn complete_attribute_on_module() {
391 r#"mod foo {#![$0]}"#,
405 at no_implicit_prelude
411 fn complete_attribute_on_macro_rules() {
413 r#"#[$0] macro_rules! foo {}"#,
434 fn complete_attribute_on_macro_def() {
436 r#"#[$0] macro foo {}"#,
455 fn complete_attribute_on_extern_crate() {
457 r#"#[$0] extern crate foo;"#,
477 fn complete_attribute_on_use() {
498 fn complete_attribute_on_type_alias() {
500 r#"#[$0] type foo = ();"#,
519 fn complete_attribute_on_struct() {
521 r#"#[$0] struct Foo;"#,
543 fn complete_attribute_on_enum() {
545 r#"#[$0] enum Foo {}"#,
567 fn complete_attribute_on_const() {
569 r#"#[$0] const FOO: () = ();"#,
588 fn complete_attribute_on_static() {
590 r#"#[$0] static FOO: () = ()"#,
606 at link_section = "…"
613 fn complete_attribute_on_trait() {
615 r#"#[$0] trait Foo {}"#,
635 fn complete_attribute_on_impl() {
637 r#"#[$0] impl () {}"#,
651 at automatically_derived
655 r#"impl () {#![$0]}"#,
674 fn complete_attribute_on_extern_block() {
676 r#"#[$0] extern {}"#,
694 r#"extern {#![$0]}"#,
714 fn complete_attribute_on_variant() {
716 r#"enum Foo { #[$0] Bar }"#,
730 fn complete_attribute_on_expr() {
732 r#"fn main() { #[$0] foo() }"#,
743 r#"fn main() { #[$0] foo(); }"#,