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::{algo::non_trivia_sibling, ast, AstNode, Direction, 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 is_inner = attribute.kind() == ast::AttrKind::Inner;
41 let attribute_annotated_item_kind =
42 attribute.syntax().parent().map(|it| it.kind()).filter(|_| {
44 // If we got nothing coming after the attribute it could be anything so filter it the kind out
45 || non_trivia_sibling(attribute.syntax().clone().into(), Direction::Next).is_some()
47 let attributes = attribute_annotated_item_kind.and_then(|kind| {
48 if ast::Expr::can_cast(kind) {
51 KIND_TO_ATTRIBUTES.get(&kind).copied()
55 let add_completion = |attr_completion: &AttrCompletion| {
56 let mut item = CompletionItem::new(
57 CompletionKind::Attribute,
59 attr_completion.label,
61 item.kind(CompletionItemKind::Attribute);
63 if let Some(lookup) = attr_completion.lookup {
64 item.lookup_by(lookup);
67 if let Some((snippet, cap)) = attr_completion.snippet.zip(ctx.config.snippet_cap) {
68 item.insert_snippet(cap, snippet);
71 if is_inner || !attr_completion.prefer_inner {
72 acc.add(item.build());
77 Some(applicable) => applicable
79 .flat_map(|name| ATTRIBUTES.binary_search_by(|attr| attr.key().cmp(name)).ok())
80 .flat_map(|idx| ATTRIBUTES.get(idx))
81 .for_each(add_completion),
82 None if is_inner => ATTRIBUTES.iter().for_each(add_completion),
83 None => ATTRIBUTES.iter().filter(|compl| !compl.prefer_inner).for_each(add_completion),
87 struct AttrCompletion {
89 lookup: Option<&'static str>,
90 snippet: Option<&'static str>,
95 fn key(&self) -> &'static str {
96 self.lookup.unwrap_or(self.label)
99 const fn prefer_inner(self) -> AttrCompletion {
100 AttrCompletion { prefer_inner: true, ..self }
106 lookup: Option<&'static str>,
107 snippet: Option<&'static str>,
108 ) -> AttrCompletion {
109 AttrCompletion { label, lookup, snippet, prefer_inner: false }
113 // attributes applicable to all items
114 [@ { item $($tt:tt)* } {$($acc:tt)*}] => {
115 attrs!(@ { $($tt)* } { $($acc)*, "deprecated", "doc", "dochidden", "docalias", "must_use", "no_mangle" })
117 // attributes applicable to all adts
118 [@ { adt $($tt:tt)* } {$($acc:tt)*}] => {
119 attrs!(@ { $($tt)* } { $($acc)*, "derive", "repr" })
121 // attributes applicable to all linkable things aka functions/statics
122 [@ { linkable $($tt:tt)* } {$($acc:tt)*}] => {
123 attrs!(@ { $($tt)* } { $($acc)*, "export_name", "link_name", "link_section" })
125 // error fallback for nicer error message
126 [@ { $ty:ident $($tt:tt)* } {$($acc:tt)*}] => {
127 compile_error!(concat!("unknown attr subtype ", stringify!($ty)))
129 // general push down accumulation
130 [@ { $lit:literal $($tt:tt)*} {$($acc:tt)*}] => {
131 attrs!(@ { $($tt)* } { $($acc)*, $lit })
133 [@ {$($tt:tt)+} {$($tt2:tt)*}] => {
134 compile_error!(concat!("Unexpected input ", stringify!($($tt)+)))
136 // final output construction
137 [@ {} {$($tt:tt)*}] => { &[$($tt)*] as _ };
140 attrs!(@ { $($tt)* } { "allow", "cfg", "cfg_attr", "deny", "forbid", "warn" })
145 static KIND_TO_ATTRIBUTES: Lazy<FxHashMap<SyntaxKind, &[&str]>> = Lazy::new(|| {
147 std::array::IntoIter::new([
152 "crate_name", "feature", "no_implicit_prelude", "no_main", "no_std",
153 "recursion_limit", "type_length_limit", "windows_subsystem"
156 (MODULE, attrs!(item, "no_implicit_prelude", "path")),
157 (ITEM_LIST, attrs!(item, "no_implicit_prelude")),
158 (MACRO_RULES, attrs!(item, "macro_export", "macro_use")),
159 (MACRO_DEF, attrs!(item)),
160 (EXTERN_CRATE, attrs!(item, "macro_use", "no_link")),
162 (TYPE_ALIAS, attrs!(item)),
163 (STRUCT, attrs!(item, adt, "non_exhaustive")),
164 (ENUM, attrs!(item, adt, "non_exhaustive")),
165 (UNION, attrs!(item, adt)),
166 (CONST, attrs!(item)),
171 "cold", "ignore", "inline", "must_use", "panic_handler", "proc_macro",
172 "proc_macro_derive", "proc_macro_attribute", "should_panic", "target_feature",
173 "test", "track_caller"
176 (STATIC, attrs!(item, linkable, "global_allocator", "used")),
177 (TRAIT, attrs!(item, "must_use")),
178 (IMPL, attrs!(item, "automatically_derived")),
179 (ASSOC_ITEM_LIST, attrs!(item)),
180 (EXTERN_BLOCK, attrs!(item, "link")),
181 (EXTERN_ITEM_LIST, attrs!(item, "link")),
182 (MACRO_CALL, attrs!()),
183 (SELF_PARAM, attrs!()),
185 (RECORD_FIELD, attrs!()),
186 (VARIANT, attrs!("non_exhaustive")),
187 (TYPE_PARAM, attrs!()),
188 (CONST_PARAM, attrs!()),
189 (LIFETIME_PARAM, attrs!()),
190 (LET_STMT, attrs!()),
191 (EXPR_STMT, attrs!()),
193 (RECORD_EXPR_FIELD_LIST, attrs!()),
194 (RECORD_EXPR_FIELD, attrs!()),
195 (MATCH_ARM_LIST, attrs!()),
196 (MATCH_ARM, attrs!()),
197 (IDENT_PAT, attrs!()),
198 (RECORD_PAT_FIELD, attrs!()),
202 const EXPR_ATTRIBUTES: &[&str] = attrs!();
204 /// https://doc.rust-lang.org/reference/attributes.html#built-in-attributes-index
205 // Keep these sorted for the binary search!
206 const ATTRIBUTES: &[AttrCompletion] = &[
207 attr("allow(…)", Some("allow"), Some("allow(${0:lint})")),
208 attr("automatically_derived", None, None),
209 attr("cfg(…)", Some("cfg"), Some("cfg(${0:predicate})")),
210 attr("cfg_attr(…)", Some("cfg_attr"), Some("cfg_attr(${1:predicate}, ${0:attr})")),
211 attr("cold", None, None),
212 attr(r#"crate_name = """#, Some("crate_name"), Some(r#"crate_name = "${0:crate_name}""#))
214 attr("deny(…)", Some("deny"), Some("deny(${0:lint})")),
215 attr(r#"deprecated"#, Some("deprecated"), Some(r#"deprecated"#)),
216 attr("derive(…)", Some("derive"), Some(r#"derive(${0:Debug})"#)),
217 attr(r#"doc = "…""#, Some("doc"), Some(r#"doc = "${0:docs}""#)),
218 attr(r#"doc(alias = "…")"#, Some("docalias"), Some(r#"doc(alias = "${0:docs}")"#)),
219 attr(r#"doc(hidden)"#, Some("dochidden"), Some(r#"doc(hidden)"#)),
221 r#"export_name = "…""#,
223 Some(r#"export_name = "${0:exported_symbol_name}""#),
225 attr("feature(…)", Some("feature"), Some("feature(${0:flag})")).prefer_inner(),
226 attr("forbid(…)", Some("forbid"), Some("forbid(${0:lint})")),
227 attr("global_allocator", None, None),
228 attr(r#"ignore = "…""#, Some("ignore"), Some(r#"ignore = "${0:reason}""#)),
229 attr("inline", Some("inline"), Some("inline")),
230 attr("link", None, None),
231 attr(r#"link_name = "…""#, Some("link_name"), Some(r#"link_name = "${0:symbol_name}""#)),
233 r#"link_section = "…""#,
234 Some("link_section"),
235 Some(r#"link_section = "${0:section_name}""#),
237 attr("macro_export", None, None),
238 attr("macro_use", None, None),
239 attr(r#"must_use"#, Some("must_use"), Some(r#"must_use"#)),
240 attr("no_implicit_prelude", None, None).prefer_inner(),
241 attr("no_link", None, None).prefer_inner(),
242 attr("no_main", None, None).prefer_inner(),
243 attr("no_mangle", None, None),
244 attr("no_std", None, None).prefer_inner(),
245 attr("non_exhaustive", None, None),
246 attr("panic_handler", None, None),
247 attr(r#"path = "…""#, Some("path"), Some(r#"path ="${0:path}""#)),
248 attr("proc_macro", None, None),
249 attr("proc_macro_attribute", None, None),
250 attr("proc_macro_derive(…)", Some("proc_macro_derive"), Some("proc_macro_derive(${0:Trait})")),
251 attr("recursion_limit = …", Some("recursion_limit"), Some("recursion_limit = ${0:128}"))
253 attr("repr(…)", Some("repr"), Some("repr(${0:C})")),
254 attr("should_panic", Some("should_panic"), Some(r#"should_panic"#)),
256 r#"target_feature = "…""#,
257 Some("target_feature"),
258 Some(r#"target_feature = "${0:feature}""#),
260 attr("test", None, None),
261 attr("track_caller", None, None),
262 attr("type_length_limit = …", Some("type_length_limit"), Some("type_length_limit = ${0:128}"))
264 attr("used", None, None),
265 attr("warn(…)", Some("warn"), Some("warn(${0:lint})")),
267 r#"windows_subsystem = "…""#,
268 Some("windows_subsystem"),
269 Some(r#"windows_subsystem = "${0:subsystem}""#),
274 fn parse_comma_sep_input(derive_input: ast::TokenTree) -> Option<FxHashSet<String>> {
275 let (l_paren, r_paren) = derive_input.l_paren_token().zip(derive_input.r_paren_token())?;
276 let mut input_derives = FxHashSet::default();
277 let mut tokens = derive_input
279 .children_with_tokens()
280 .filter_map(NodeOrToken::into_token)
281 .skip_while(|token| token != &l_paren)
283 .take_while(|token| token != &r_paren)
285 let mut input = String::new();
286 while tokens.peek().is_some() {
287 for token in tokens.by_ref().take_while(|t| t.kind() != T![,]) {
288 input.push_str(token.text());
291 if !input.is_empty() {
292 input_derives.insert(input.trim().to_owned());
305 use expect_test::{expect, Expect};
307 use crate::{test_utils::completion_list, CompletionKind};
310 fn attributes_are_sorted() {
311 let mut attrs = ATTRIBUTES.iter().map(|attr| attr.key());
312 let mut prev = attrs.next().unwrap();
314 attrs.for_each(|next| {
317 r#"ATTRIBUTES array is not sorted, "{}" should come after "{}""#,
325 fn check(ra_fixture: &str, expect: Expect) {
326 let actual = completion_list(ra_fixture, CompletionKind::Attribute);
327 expect.assert_eq(&actual);
331 fn test_attribute_completion_inside_nested_attr() {
332 check(r#"#[cfg($0)]"#, expect![[]])
336 fn test_attribute_completion_with_existing_attr() {
338 r#"#[no_mangle] #[$0] mcall!();"#,
351 fn complete_attribute_on_source_file() {
369 at no_implicit_prelude
372 at recursion_limit = …
373 at type_length_limit = …
374 at windows_subsystem = "…"
380 fn complete_attribute_on_module() {
400 r#"mod foo {#![$0]}"#,
414 at no_implicit_prelude
420 fn complete_attribute_on_macro_rules() {
422 r#"#[$0] macro_rules! foo {}"#,
443 fn complete_attribute_on_macro_def() {
445 r#"#[$0] macro foo {}"#,
464 fn complete_attribute_on_extern_crate() {
466 r#"#[$0] extern crate foo;"#,
486 fn complete_attribute_on_use() {
507 fn complete_attribute_on_type_alias() {
509 r#"#[$0] type foo = ();"#,
528 fn complete_attribute_on_struct() {
530 r#"#[$0] struct Foo;"#,
552 fn complete_attribute_on_enum() {
554 r#"#[$0] enum Foo {}"#,
576 fn complete_attribute_on_const() {
578 r#"#[$0] const FOO: () = ();"#,
597 fn complete_attribute_on_static() {
599 r#"#[$0] static FOO: () = ()"#,
615 at link_section = "…"
623 fn complete_attribute_on_trait() {
625 r#"#[$0] trait Foo {}"#,
645 fn complete_attribute_on_impl() {
647 r#"#[$0] impl () {}"#,
661 at automatically_derived
665 r#"impl () {#![$0]}"#,
684 fn complete_attribute_on_extern_block() {
686 r#"#[$0] extern {}"#,
704 r#"extern {#![$0]}"#,
724 fn complete_attribute_on_variant() {
726 r#"enum Foo { #[$0] Bar }"#,
740 fn complete_attribute_on_fn() {
742 r#"#[$0] fn main() {}"#,
758 at link_section = "…"
765 at proc_macro_derive(…)
766 at proc_macro_attribute
768 at target_feature = "…"
776 fn complete_attribute_on_expr() {
778 r#"fn main() { #[$0] foo() }"#,
791 fn complete_attribute_in_source_file_end() {
796 at automatically_derived
813 at link_section = "…"
822 at proc_macro_attribute
823 at proc_macro_derive(…)
826 at target_feature = "…"