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, NodeOrToken, 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 // attributes applicable to all items
109 [@ { item $($tt:tt)* } {$($acc:tt)*}] => {
110 attrs!(@ { $($tt)* } { $($acc)*, "deprecated", "doc", "dochidden", "docalias", "must_use", "no_mangle" })
112 // attributes applicable to all adts
113 [@ { adt $($tt:tt)* } {$($acc:tt)*}] => {
114 attrs!(@ { $($tt)* } { $($acc)*, "derive", "repr" })
116 // attributes applicable to all linkable things aka functions/statics
117 [@ { linkable $($tt:tt)* } {$($acc:tt)*}] => {
118 attrs!(@ { $($tt)* } { $($acc)*, "export_name", "link_name", "link_section" })
120 // error fallback for nicer error message
121 [@ { $ty:ident $($tt:tt)* } {$($acc:tt)*}] => {
122 compile_error!(concat!("unknown attr subtype ", stringify!($ty)))
124 // general push down accumulation
125 [@ { $lit:literal $($tt:tt)*} {$($acc:tt)*}] => {
126 attrs!(@ { $($tt)* } { $($acc)*, $lit })
128 [@ {$($tt:tt)+} {$($tt2:tt)*}] => {
129 compile_error!(concat!("Unexpected input ", stringify!($($tt)+)))
131 // final output construction
132 [@ {} {$($tt:tt)*}] => { &[$($tt)*] as _ };
135 attrs!(@ { $($tt)* } { "allow", "cfg", "cfg_attr", "deny", "forbid", "warn" })
140 static KIND_TO_ATTRIBUTES: Lazy<FxHashMap<SyntaxKind, &[&str]>> = Lazy::new(|| {
142 std::array::IntoIter::new([
147 "crate_name", "feature", "no_implicit_prelude", "no_main", "no_std",
148 "recursion_limit", "type_length_limit", "windows_subsystem"
151 (MODULE, attrs!(item, "no_implicit_prelude", "path")),
152 (ITEM_LIST, attrs!(item, "no_implicit_prelude")),
153 (MACRO_RULES, attrs!(item, "macro_export", "macro_use")),
154 (MACRO_DEF, attrs!(item)),
155 (EXTERN_CRATE, attrs!(item, "macro_use", "no_link")),
157 (TYPE_ALIAS, attrs!(item)),
158 (STRUCT, attrs!(item, adt, "non_exhaustive")),
159 (ENUM, attrs!(item, adt, "non_exhaustive")),
160 (UNION, attrs!(item, adt)),
161 (CONST, attrs!(item)),
166 "cold", "ignore", "inline", "must_use", "panic_handler", "proc_macro",
167 "proc_macro_derive", "proc_macro_attribute", "should_panic", "target_feature",
168 "test", "track_caller"
171 (STATIC, attrs!(item, linkable, "global_allocator", "used")),
172 (TRAIT, attrs!(item, "must_use")),
173 (IMPL, attrs!(item, "automatically_derived")),
174 (ASSOC_ITEM_LIST, attrs!(item)),
175 (EXTERN_BLOCK, attrs!(item, "link")),
176 (EXTERN_ITEM_LIST, attrs!(item, "link")),
177 (MACRO_CALL, attrs!()),
178 (SELF_PARAM, attrs!()),
180 (RECORD_FIELD, attrs!()),
181 (VARIANT, attrs!("non_exhaustive")),
182 (TYPE_PARAM, attrs!()),
183 (CONST_PARAM, attrs!()),
184 (LIFETIME_PARAM, attrs!()),
185 (LET_STMT, attrs!()),
186 (EXPR_STMT, attrs!()),
188 (RECORD_EXPR_FIELD_LIST, attrs!()),
189 (RECORD_EXPR_FIELD, attrs!()),
190 (MATCH_ARM_LIST, attrs!()),
191 (MATCH_ARM, attrs!()),
192 (IDENT_PAT, attrs!()),
193 (RECORD_PAT_FIELD, attrs!()),
197 const EXPR_ATTRIBUTES: &[&str] = attrs!();
199 /// https://doc.rust-lang.org/reference/attributes.html#built-in-attributes-index
200 // Keep these sorted for the binary search!
201 const ATTRIBUTES: &[AttrCompletion] = &[
202 attr("allow(…)", Some("allow"), Some("allow(${0:lint})")),
203 attr("automatically_derived", None, None),
204 attr("cfg(…)", Some("cfg"), Some("cfg(${0:predicate})")),
205 attr("cfg_attr(…)", Some("cfg_attr"), Some("cfg_attr(${1:predicate}, ${0:attr})")),
206 attr("cold", None, None),
207 attr(r#"crate_name = """#, Some("crate_name"), Some(r#"crate_name = "${0:crate_name}""#))
209 attr("deny(…)", Some("deny"), Some("deny(${0:lint})")),
210 attr(r#"deprecated"#, Some("deprecated"), Some(r#"deprecated"#)),
211 attr("derive(…)", Some("derive"), Some(r#"derive(${0:Debug})"#)),
212 attr(r#"doc = "…""#, Some("doc"), Some(r#"doc = "${0:docs}""#)),
213 attr(r#"doc(alias = "…")"#, Some("docalias"), Some(r#"doc(alias = "${0:docs}")"#)),
214 attr(r#"doc(hidden)"#, Some("dochidden"), Some(r#"doc(hidden)"#)),
216 r#"export_name = "…""#,
218 Some(r#"export_name = "${0:exported_symbol_name}""#),
220 attr("feature(…)", Some("feature"), Some("feature(${0:flag})")).prefer_inner(),
221 attr("forbid(…)", Some("forbid"), Some("forbid(${0:lint})")),
222 // FIXME: resolve through macro resolution?
223 attr("global_allocator", None, None).prefer_inner(),
224 attr(r#"ignore = "…""#, Some("ignore"), Some(r#"ignore = "${0:reason}""#)),
225 attr("inline", Some("inline"), Some("inline")),
226 attr("link", None, None),
227 attr(r#"link_name = "…""#, Some("link_name"), Some(r#"link_name = "${0:symbol_name}""#)),
229 r#"link_section = "…""#,
230 Some("link_section"),
231 Some(r#"link_section = "${0:section_name}""#),
233 attr("macro_export", None, None),
234 attr("macro_use", None, None),
235 attr(r#"must_use"#, Some("must_use"), Some(r#"must_use"#)),
236 attr("no_implicit_prelude", None, None).prefer_inner(),
237 attr("no_link", None, None).prefer_inner(),
238 attr("no_main", None, None).prefer_inner(),
239 attr("no_mangle", None, None),
240 attr("no_std", None, None).prefer_inner(),
241 attr("non_exhaustive", None, None),
242 attr("panic_handler", None, None).prefer_inner(),
243 attr(r#"path = "…""#, Some("path"), Some(r#"path ="${0:path}""#)),
244 attr("proc_macro", None, None),
245 attr("proc_macro_attribute", None, None),
246 attr("proc_macro_derive(…)", Some("proc_macro_derive"), Some("proc_macro_derive(${0:Trait})")),
247 attr("recursion_limit = …", Some("recursion_limit"), Some("recursion_limit = ${0:128}"))
249 attr("repr(…)", Some("repr"), Some("repr(${0:C})")),
250 attr("should_panic", Some("should_panic"), Some(r#"should_panic"#)),
252 r#"target_feature = "…""#,
253 Some("target_feature"),
254 Some(r#"target_feature = "${0:feature}""#),
256 attr("test", None, None),
257 attr("track_caller", None, None),
258 attr("type_length_limit = …", Some("type_length_limit"), Some("type_length_limit = ${0:128}"))
260 attr("used", None, None),
261 attr("warn(…)", Some("warn"), Some("warn(${0:lint})")),
263 r#"windows_subsystem = "…""#,
264 Some("windows_subsystem"),
265 Some(r#"windows_subsystem = "${0:subsystem}""#),
270 fn parse_comma_sep_input(derive_input: ast::TokenTree) -> Option<FxHashSet<String>> {
271 let (l_paren, r_paren) = derive_input.l_paren_token().zip(derive_input.r_paren_token())?;
272 let mut input_derives = FxHashSet::default();
273 let mut tokens = derive_input
275 .children_with_tokens()
276 .filter_map(NodeOrToken::into_token)
277 .skip_while(|token| token != &l_paren)
279 .take_while(|token| token != &r_paren)
281 let mut input = String::new();
282 while tokens.peek().is_some() {
283 for token in tokens.by_ref().take_while(|t| t.kind() != T![,]) {
284 input.push_str(token.text());
287 if !input.is_empty() {
288 input_derives.insert(input.trim().to_owned());
301 use expect_test::{expect, Expect};
303 use crate::{test_utils::completion_list, CompletionKind};
306 fn attributes_are_sorted() {
307 let mut attrs = ATTRIBUTES.iter().map(|attr| attr.key());
308 let mut prev = attrs.next().unwrap();
310 attrs.for_each(|next| {
313 r#"ATTRIBUTES array is not sorted, "{}" should come after "{}""#,
321 fn check(ra_fixture: &str, expect: Expect) {
322 let actual = completion_list(ra_fixture, CompletionKind::Attribute);
323 expect.assert_eq(&actual);
327 fn test_attribute_completion_inside_nested_attr() {
328 check(r#"#[cfg($0)]"#, expect![[]])
332 fn test_attribute_completion_with_existing_attr() {
334 r#"#[no_mangle] #[$0] mcall!();"#,
347 fn complete_attribute_on_source_file() {
365 at no_implicit_prelude
368 at recursion_limit = …
369 at type_length_limit = …
370 at windows_subsystem = "…"
376 fn complete_attribute_on_module() {
396 r#"mod foo {#![$0]}"#,
410 at no_implicit_prelude
416 fn complete_attribute_on_macro_rules() {
418 r#"#[$0] macro_rules! foo {}"#,
439 fn complete_attribute_on_macro_def() {
441 r#"#[$0] macro foo {}"#,
460 fn complete_attribute_on_extern_crate() {
462 r#"#[$0] extern crate foo;"#,
482 fn complete_attribute_on_use() {
503 fn complete_attribute_on_type_alias() {
505 r#"#[$0] type foo = ();"#,
524 fn complete_attribute_on_struct() {
526 r#"#[$0] struct Foo;"#,
548 fn complete_attribute_on_enum() {
550 r#"#[$0] enum Foo {}"#,
572 fn complete_attribute_on_const() {
574 r#"#[$0] const FOO: () = ();"#,
593 fn complete_attribute_on_static() {
595 r#"#[$0] static FOO: () = ()"#,
611 at link_section = "…"
618 fn complete_attribute_on_trait() {
620 r#"#[$0] trait Foo {}"#,
640 fn complete_attribute_on_impl() {
642 r#"#[$0] impl () {}"#,
656 at automatically_derived
660 r#"impl () {#![$0]}"#,
679 fn complete_attribute_on_extern_block() {
681 r#"#[$0] extern {}"#,
699 r#"extern {#![$0]}"#,
719 fn complete_attribute_on_variant() {
721 r#"enum Foo { #[$0] Bar }"#,
735 fn complete_attribute_on_expr() {
737 r#"fn main() { #[$0] foo() }"#,
748 r#"fn main() { #[$0] foo(); }"#,