1 //! Completion for attributes
3 //! This module uses a bit of static metadata to provide completions
4 //! for built-in attributes.
6 use ide_db::helpers::generated_lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES};
7 use once_cell::sync::Lazy;
8 use rustc_hash::{FxHashMap, FxHashSet};
9 use syntax::{algo::non_trivia_sibling, ast, AstNode, Direction, NodeOrToken, SyntaxKind, T};
12 context::CompletionContext,
13 item::{CompletionItem, CompletionItemKind, CompletionKind},
20 pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
21 let attribute = ctx.attribute_under_caret.as_ref()?;
22 match (attribute.path().and_then(|p| p.as_single_name_ref()), attribute.token_tree()) {
23 (Some(path), Some(token_tree)) => match path.text().as_str() {
24 "derive" => derive::complete_derive(acc, ctx, token_tree),
25 "feature" => lint::complete_lint(acc, ctx, token_tree, FEATURES),
26 "allow" | "warn" | "deny" | "forbid" => {
27 lint::complete_lint(acc, ctx, token_tree.clone(), DEFAULT_LINTS);
28 lint::complete_lint(acc, ctx, token_tree, CLIPPY_LINTS);
32 (None, Some(_)) => (),
33 _ => complete_new_attribute(acc, ctx, attribute),
38 fn complete_new_attribute(acc: &mut Completions, ctx: &CompletionContext, attribute: &ast::Attr) {
39 let is_inner = attribute.kind() == ast::AttrKind::Inner;
40 let attribute_annotated_item_kind =
41 attribute.syntax().parent().map(|it| it.kind()).filter(|_| {
43 // If we got nothing coming after the attribute it could be anything so filter it the kind out
44 || non_trivia_sibling(attribute.syntax().clone().into(), Direction::Next).is_some()
46 let attributes = attribute_annotated_item_kind.and_then(|kind| {
47 if ast::Expr::can_cast(kind) {
50 KIND_TO_ATTRIBUTES.get(&kind).copied()
54 let add_completion = |attr_completion: &AttrCompletion| {
55 let mut item = CompletionItem::new(
56 CompletionKind::Attribute,
58 attr_completion.label,
60 item.kind(CompletionItemKind::Attribute);
62 if let Some(lookup) = attr_completion.lookup {
63 item.lookup_by(lookup);
66 if let Some((snippet, cap)) = attr_completion.snippet.zip(ctx.config.snippet_cap) {
67 item.insert_snippet(cap, snippet);
70 if is_inner || !attr_completion.prefer_inner {
71 acc.add(item.build());
76 Some(applicable) => applicable
78 .flat_map(|name| ATTRIBUTES.binary_search_by(|attr| attr.key().cmp(name)).ok())
79 .flat_map(|idx| ATTRIBUTES.get(idx))
80 .for_each(add_completion),
81 None if is_inner => ATTRIBUTES.iter().for_each(add_completion),
82 None => ATTRIBUTES.iter().filter(|compl| !compl.prefer_inner).for_each(add_completion),
86 struct AttrCompletion {
88 lookup: Option<&'static str>,
89 snippet: Option<&'static str>,
94 fn key(&self) -> &'static str {
95 self.lookup.unwrap_or(self.label)
98 const fn prefer_inner(self) -> AttrCompletion {
99 AttrCompletion { prefer_inner: true, ..self }
105 lookup: Option<&'static str>,
106 snippet: Option<&'static str>,
107 ) -> AttrCompletion {
108 AttrCompletion { label, lookup, snippet, prefer_inner: false }
112 // attributes applicable to all items
113 [@ { item $($tt:tt)* } {$($acc:tt)*}] => {
114 attrs!(@ { $($tt)* } { $($acc)*, "deprecated", "doc", "dochidden", "docalias", "must_use", "no_mangle" })
116 // attributes applicable to all adts
117 [@ { adt $($tt:tt)* } {$($acc:tt)*}] => {
118 attrs!(@ { $($tt)* } { $($acc)*, "derive", "repr" })
120 // attributes applicable to all linkable things aka functions/statics
121 [@ { linkable $($tt:tt)* } {$($acc:tt)*}] => {
122 attrs!(@ { $($tt)* } { $($acc)*, "export_name", "link_name", "link_section" })
124 // error fallback for nicer error message
125 [@ { $ty:ident $($tt:tt)* } {$($acc:tt)*}] => {
126 compile_error!(concat!("unknown attr subtype ", stringify!($ty)))
128 // general push down accumulation
129 [@ { $lit:literal $($tt:tt)*} {$($acc:tt)*}] => {
130 attrs!(@ { $($tt)* } { $($acc)*, $lit })
132 [@ {$($tt:tt)+} {$($tt2:tt)*}] => {
133 compile_error!(concat!("Unexpected input ", stringify!($($tt)+)))
135 // final output construction
136 [@ {} {$($tt:tt)*}] => { &[$($tt)*] as _ };
139 attrs!(@ { $($tt)* } { "allow", "cfg", "cfg_attr", "deny", "forbid", "warn" })
144 static KIND_TO_ATTRIBUTES: Lazy<FxHashMap<SyntaxKind, &[&str]>> = Lazy::new(|| {
146 std::array::IntoIter::new([
151 "crate_name", "feature", "no_implicit_prelude", "no_main", "no_std",
152 "recursion_limit", "type_length_limit", "windows_subsystem"
155 (MODULE, attrs!(item, "no_implicit_prelude", "path")),
156 (ITEM_LIST, attrs!(item, "no_implicit_prelude")),
157 (MACRO_RULES, attrs!(item, "macro_export", "macro_use")),
158 (MACRO_DEF, attrs!(item)),
159 (EXTERN_CRATE, attrs!(item, "macro_use", "no_link")),
161 (TYPE_ALIAS, attrs!(item)),
162 (STRUCT, attrs!(item, adt, "non_exhaustive")),
163 (ENUM, attrs!(item, adt, "non_exhaustive")),
164 (UNION, attrs!(item, adt)),
165 (CONST, attrs!(item)),
170 "cold", "ignore", "inline", "must_use", "panic_handler", "proc_macro",
171 "proc_macro_derive", "proc_macro_attribute", "should_panic", "target_feature",
172 "test", "track_caller"
175 (STATIC, attrs!(item, linkable, "global_allocator", "used")),
176 (TRAIT, attrs!(item, "must_use")),
177 (IMPL, attrs!(item, "automatically_derived")),
178 (ASSOC_ITEM_LIST, attrs!(item)),
179 (EXTERN_BLOCK, attrs!(item, "link")),
180 (EXTERN_ITEM_LIST, attrs!(item, "link")),
181 (MACRO_CALL, attrs!()),
182 (SELF_PARAM, attrs!()),
184 (RECORD_FIELD, attrs!()),
185 (VARIANT, attrs!("non_exhaustive")),
186 (TYPE_PARAM, attrs!()),
187 (CONST_PARAM, attrs!()),
188 (LIFETIME_PARAM, attrs!()),
189 (LET_STMT, attrs!()),
190 (EXPR_STMT, attrs!()),
192 (RECORD_EXPR_FIELD_LIST, attrs!()),
193 (RECORD_EXPR_FIELD, attrs!()),
194 (MATCH_ARM_LIST, attrs!()),
195 (MATCH_ARM, attrs!()),
196 (IDENT_PAT, attrs!()),
197 (RECORD_PAT_FIELD, attrs!()),
201 const EXPR_ATTRIBUTES: &[&str] = attrs!();
203 /// https://doc.rust-lang.org/reference/attributes.html#built-in-attributes-index
204 // Keep these sorted for the binary search!
205 const ATTRIBUTES: &[AttrCompletion] = &[
206 attr("allow(…)", Some("allow"), Some("allow(${0:lint})")),
207 attr("automatically_derived", None, None),
208 attr("cfg(…)", Some("cfg"), Some("cfg(${0:predicate})")),
209 attr("cfg_attr(…)", Some("cfg_attr"), Some("cfg_attr(${1:predicate}, ${0:attr})")),
210 attr("cold", None, None),
211 attr(r#"crate_name = """#, Some("crate_name"), Some(r#"crate_name = "${0:crate_name}""#))
213 attr("deny(…)", Some("deny"), Some("deny(${0:lint})")),
214 attr(r#"deprecated"#, Some("deprecated"), Some(r#"deprecated"#)),
215 attr("derive(…)", Some("derive"), Some(r#"derive(${0:Debug})"#)),
216 attr(r#"doc = "…""#, Some("doc"), Some(r#"doc = "${0:docs}""#)),
217 attr(r#"doc(alias = "…")"#, Some("docalias"), Some(r#"doc(alias = "${0:docs}")"#)),
218 attr(r#"doc(hidden)"#, Some("dochidden"), Some(r#"doc(hidden)"#)),
220 r#"export_name = "…""#,
222 Some(r#"export_name = "${0:exported_symbol_name}""#),
224 attr("feature(…)", Some("feature"), Some("feature(${0:flag})")).prefer_inner(),
225 attr("forbid(…)", Some("forbid"), Some("forbid(${0:lint})")),
226 attr("global_allocator", None, None),
227 attr(r#"ignore = "…""#, Some("ignore"), Some(r#"ignore = "${0:reason}""#)),
228 attr("inline", Some("inline"), Some("inline")),
229 attr("link", None, None),
230 attr(r#"link_name = "…""#, Some("link_name"), Some(r#"link_name = "${0:symbol_name}""#)),
232 r#"link_section = "…""#,
233 Some("link_section"),
234 Some(r#"link_section = "${0:section_name}""#),
236 attr("macro_export", None, None),
237 attr("macro_use", None, None),
238 attr(r#"must_use"#, Some("must_use"), Some(r#"must_use"#)),
239 attr("no_implicit_prelude", None, None).prefer_inner(),
240 attr("no_link", None, None).prefer_inner(),
241 attr("no_main", None, None).prefer_inner(),
242 attr("no_mangle", None, None),
243 attr("no_std", None, None).prefer_inner(),
244 attr("non_exhaustive", None, None),
245 attr("panic_handler", None, None),
246 attr(r#"path = "…""#, Some("path"), Some(r#"path ="${0:path}""#)),
247 attr("proc_macro", None, None),
248 attr("proc_macro_attribute", None, None),
249 attr("proc_macro_derive(…)", Some("proc_macro_derive"), Some("proc_macro_derive(${0:Trait})")),
250 attr("recursion_limit = …", Some("recursion_limit"), Some("recursion_limit = ${0:128}"))
252 attr("repr(…)", Some("repr"), Some("repr(${0:C})")),
253 attr("should_panic", Some("should_panic"), Some(r#"should_panic"#)),
255 r#"target_feature = "…""#,
256 Some("target_feature"),
257 Some(r#"target_feature = "${0:feature}""#),
259 attr("test", None, None),
260 attr("track_caller", None, None),
261 attr("type_length_limit = …", Some("type_length_limit"), Some("type_length_limit = ${0:128}"))
263 attr("used", None, None),
264 attr("warn(…)", Some("warn"), Some("warn(${0:lint})")),
266 r#"windows_subsystem = "…""#,
267 Some("windows_subsystem"),
268 Some(r#"windows_subsystem = "${0:subsystem}""#),
273 fn parse_comma_sep_input(derive_input: ast::TokenTree) -> Option<FxHashSet<String>> {
274 let (l_paren, r_paren) = derive_input.l_paren_token().zip(derive_input.r_paren_token())?;
275 let mut input_derives = FxHashSet::default();
276 let mut tokens = derive_input
278 .children_with_tokens()
279 .filter_map(NodeOrToken::into_token)
280 .skip_while(|token| token != &l_paren)
282 .take_while(|token| token != &r_paren)
284 let mut input = String::new();
285 while tokens.peek().is_some() {
286 for token in tokens.by_ref().take_while(|t| t.kind() != T![,]) {
287 input.push_str(token.text());
290 if !input.is_empty() {
291 input_derives.insert(input.trim().to_owned());
304 use expect_test::{expect, Expect};
306 use crate::{test_utils::completion_list, CompletionKind};
309 fn attributes_are_sorted() {
310 let mut attrs = ATTRIBUTES.iter().map(|attr| attr.key());
311 let mut prev = attrs.next().unwrap();
313 attrs.for_each(|next| {
316 r#"ATTRIBUTES array is not sorted, "{}" should come after "{}""#,
324 fn check(ra_fixture: &str, expect: Expect) {
325 let actual = completion_list(ra_fixture, CompletionKind::Attribute);
326 expect.assert_eq(&actual);
330 fn test_attribute_completion_inside_nested_attr() {
331 check(r#"#[cfg($0)]"#, expect![[]])
335 fn test_attribute_completion_with_existing_attr() {
337 r#"#[no_mangle] #[$0] mcall!();"#,
350 fn complete_attribute_on_source_file() {
368 at no_implicit_prelude
371 at recursion_limit = …
372 at type_length_limit = …
373 at windows_subsystem = "…"
379 fn complete_attribute_on_module() {
399 r#"mod foo {#![$0]}"#,
413 at no_implicit_prelude
419 fn complete_attribute_on_macro_rules() {
421 r#"#[$0] macro_rules! foo {}"#,
442 fn complete_attribute_on_macro_def() {
444 r#"#[$0] macro foo {}"#,
463 fn complete_attribute_on_extern_crate() {
465 r#"#[$0] extern crate foo;"#,
485 fn complete_attribute_on_use() {
506 fn complete_attribute_on_type_alias() {
508 r#"#[$0] type foo = ();"#,
527 fn complete_attribute_on_struct() {
529 r#"#[$0] struct Foo;"#,
551 fn complete_attribute_on_enum() {
553 r#"#[$0] enum Foo {}"#,
575 fn complete_attribute_on_const() {
577 r#"#[$0] const FOO: () = ();"#,
596 fn complete_attribute_on_static() {
598 r#"#[$0] static FOO: () = ()"#,
614 at link_section = "…"
622 fn complete_attribute_on_trait() {
624 r#"#[$0] trait Foo {}"#,
644 fn complete_attribute_on_impl() {
646 r#"#[$0] impl () {}"#,
660 at automatically_derived
664 r#"impl () {#![$0]}"#,
683 fn complete_attribute_on_extern_block() {
685 r#"#[$0] extern {}"#,
703 r#"extern {#![$0]}"#,
723 fn complete_attribute_on_variant() {
725 r#"enum Foo { #[$0] Bar }"#,
739 fn complete_attribute_on_fn() {
741 r#"#[$0] fn main() {}"#,
757 at link_section = "…"
764 at proc_macro_derive(…)
765 at proc_macro_attribute
767 at target_feature = "…"
775 fn complete_attribute_on_expr() {
777 r#"fn main() { #[$0] foo() }"#,
790 fn complete_attribute_in_source_file_end() {
795 at automatically_derived
812 at link_section = "…"
821 at proc_macro_attribute
822 at proc_macro_derive(…)
825 at target_feature = "…"