1 //! Completion for attributes
3 //! This module uses a bit of static metadata to provide completions
4 //! for built-in attributes.
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};
13 context::CompletionContext,
14 item::{CompletionItem, CompletionItemKind, CompletionKind},
23 pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
24 let attribute = ctx.attribute_under_caret.as_ref()?;
25 match (attribute.path().and_then(|p| p.as_single_name_ref()), attribute.token_tree()) {
26 (Some(path), Some(token_tree)) => match path.text().as_str() {
27 "derive" => derive::complete_derive(acc, ctx, token_tree),
28 "repr" => repr::complete_repr(acc, ctx, token_tree),
29 "feature" => lint::complete_lint(acc, ctx, token_tree, FEATURES),
30 "allow" | "warn" | "deny" | "forbid" => {
31 lint::complete_lint(acc, ctx, token_tree.clone(), DEFAULT_LINTS);
32 lint::complete_lint(acc, ctx, token_tree, CLIPPY_LINTS);
35 cfg::complete_cfg(acc, ctx);
39 (None, Some(_)) => (),
40 _ => complete_new_attribute(acc, ctx, attribute),
45 fn complete_new_attribute(acc: &mut Completions, ctx: &CompletionContext, attribute: &ast::Attr) {
46 let is_inner = attribute.kind() == ast::AttrKind::Inner;
47 let attribute_annotated_item_kind =
48 attribute.syntax().parent().map(|it| it.kind()).filter(|_| {
50 // If we got nothing coming after the attribute it could be anything so filter it the kind out
51 || non_trivia_sibling(attribute.syntax().clone().into(), Direction::Next).is_some()
53 let attributes = attribute_annotated_item_kind.and_then(|kind| {
54 if ast::Expr::can_cast(kind) {
57 KIND_TO_ATTRIBUTES.get(&kind).copied()
61 let add_completion = |attr_completion: &AttrCompletion| {
62 let mut item = CompletionItem::new(
63 CompletionKind::Attribute,
65 attr_completion.label,
67 item.kind(CompletionItemKind::Attribute);
69 if let Some(lookup) = attr_completion.lookup {
70 item.lookup_by(lookup);
73 if let Some((snippet, cap)) = attr_completion.snippet.zip(ctx.config.snippet_cap) {
74 item.insert_snippet(cap, snippet);
77 if is_inner || !attr_completion.prefer_inner {
83 Some(applicable) => applicable
85 .flat_map(|name| ATTRIBUTES.binary_search_by(|attr| attr.key().cmp(name)).ok())
86 .flat_map(|idx| ATTRIBUTES.get(idx))
87 .for_each(add_completion),
88 None if is_inner => ATTRIBUTES.iter().for_each(add_completion),
89 None => ATTRIBUTES.iter().filter(|compl| !compl.prefer_inner).for_each(add_completion),
92 // FIXME: write a test for this when we can
93 ctx.scope.process_all_names(&mut |name, scope_def| {
94 if let hir::ScopeDef::MacroDef(mac) = scope_def {
95 if mac.kind() == hir::MacroKind::Attr {
96 let mut item = CompletionItem::new(
97 CompletionKind::Attribute,
101 item.kind(CompletionItemKind::Attribute);
102 if let Some(docs) = mac.docs(ctx.sema.db) {
103 item.documentation(docs);
111 struct AttrCompletion {
113 lookup: Option<&'static str>,
114 snippet: Option<&'static str>,
118 impl AttrCompletion {
119 fn key(&self) -> &'static str {
120 self.lookup.unwrap_or(self.label)
123 const fn prefer_inner(self) -> AttrCompletion {
124 AttrCompletion { prefer_inner: true, ..self }
130 lookup: Option<&'static str>,
131 snippet: Option<&'static str>,
132 ) -> AttrCompletion {
133 AttrCompletion { label, lookup, snippet, prefer_inner: false }
137 // attributes applicable to all items
138 [@ { item $($tt:tt)* } {$($acc:tt)*}] => {
139 attrs!(@ { $($tt)* } { $($acc)*, "deprecated", "doc", "dochidden", "docalias", "must_use", "no_mangle" })
141 // attributes applicable to all adts
142 [@ { adt $($tt:tt)* } {$($acc:tt)*}] => {
143 attrs!(@ { $($tt)* } { $($acc)*, "derive", "repr" })
145 // attributes applicable to all linkable things aka functions/statics
146 [@ { linkable $($tt:tt)* } {$($acc:tt)*}] => {
147 attrs!(@ { $($tt)* } { $($acc)*, "export_name", "link_name", "link_section" })
149 // error fallback for nicer error message
150 [@ { $ty:ident $($tt:tt)* } {$($acc:tt)*}] => {
151 compile_error!(concat!("unknown attr subtype ", stringify!($ty)))
153 // general push down accumulation
154 [@ { $lit:literal $($tt:tt)*} {$($acc:tt)*}] => {
155 attrs!(@ { $($tt)* } { $($acc)*, $lit })
157 [@ {$($tt:tt)+} {$($tt2:tt)*}] => {
158 compile_error!(concat!("Unexpected input ", stringify!($($tt)+)))
160 // final output construction
161 [@ {} {$($tt:tt)*}] => { &[$($tt)*] as _ };
164 attrs!(@ { $($tt)* } { "allow", "cfg", "cfg_attr", "deny", "forbid", "warn" })
169 static KIND_TO_ATTRIBUTES: Lazy<FxHashMap<SyntaxKind, &[&str]>> = Lazy::new(|| {
171 std::array::IntoIter::new([
176 "crate_name", "feature", "no_implicit_prelude", "no_main", "no_std",
177 "recursion_limit", "type_length_limit", "windows_subsystem"
180 (MODULE, attrs!(item, "no_implicit_prelude", "path")),
181 (ITEM_LIST, attrs!(item, "no_implicit_prelude")),
182 (MACRO_RULES, attrs!(item, "macro_export", "macro_use")),
183 (MACRO_DEF, attrs!(item)),
184 (EXTERN_CRATE, attrs!(item, "macro_use", "no_link")),
186 (TYPE_ALIAS, attrs!(item)),
187 (STRUCT, attrs!(item, adt, "non_exhaustive")),
188 (ENUM, attrs!(item, adt, "non_exhaustive")),
189 (UNION, attrs!(item, adt)),
190 (CONST, attrs!(item)),
195 "cold", "ignore", "inline", "must_use", "panic_handler", "proc_macro",
196 "proc_macro_derive", "proc_macro_attribute", "should_panic", "target_feature",
197 "test", "track_caller"
200 (STATIC, attrs!(item, linkable, "global_allocator", "used")),
201 (TRAIT, attrs!(item, "must_use")),
202 (IMPL, attrs!(item, "automatically_derived")),
203 (ASSOC_ITEM_LIST, attrs!(item)),
204 (EXTERN_BLOCK, attrs!(item, "link")),
205 (EXTERN_ITEM_LIST, attrs!(item, "link")),
206 (MACRO_CALL, attrs!()),
207 (SELF_PARAM, attrs!()),
209 (RECORD_FIELD, attrs!()),
210 (VARIANT, attrs!("non_exhaustive")),
211 (TYPE_PARAM, attrs!()),
212 (CONST_PARAM, attrs!()),
213 (LIFETIME_PARAM, attrs!()),
214 (LET_STMT, attrs!()),
215 (EXPR_STMT, attrs!()),
217 (RECORD_EXPR_FIELD_LIST, attrs!()),
218 (RECORD_EXPR_FIELD, attrs!()),
219 (MATCH_ARM_LIST, attrs!()),
220 (MATCH_ARM, attrs!()),
221 (IDENT_PAT, attrs!()),
222 (RECORD_PAT_FIELD, attrs!()),
226 const EXPR_ATTRIBUTES: &[&str] = attrs!();
228 /// <https://doc.rust-lang.org/reference/attributes.html#built-in-attributes-index>
229 // Keep these sorted for the binary search!
230 const ATTRIBUTES: &[AttrCompletion] = &[
231 attr("allow(…)", Some("allow"), Some("allow(${0:lint})")),
232 attr("automatically_derived", None, None),
233 attr("cfg(…)", Some("cfg"), Some("cfg(${0:predicate})")),
234 attr("cfg_attr(…)", Some("cfg_attr"), Some("cfg_attr(${1:predicate}, ${0:attr})")),
235 attr("cold", None, None),
236 attr(r#"crate_name = """#, Some("crate_name"), Some(r#"crate_name = "${0:crate_name}""#))
238 attr("deny(…)", Some("deny"), Some("deny(${0:lint})")),
239 attr(r#"deprecated"#, Some("deprecated"), Some(r#"deprecated"#)),
240 attr("derive(…)", Some("derive"), Some(r#"derive(${0:Debug})"#)),
241 attr(r#"doc = "…""#, Some("doc"), Some(r#"doc = "${0:docs}""#)),
242 attr(r#"doc(alias = "…")"#, Some("docalias"), Some(r#"doc(alias = "${0:docs}")"#)),
243 attr(r#"doc(hidden)"#, Some("dochidden"), Some(r#"doc(hidden)"#)),
245 r#"export_name = "…""#,
247 Some(r#"export_name = "${0:exported_symbol_name}""#),
249 attr("feature(…)", Some("feature"), Some("feature(${0:flag})")).prefer_inner(),
250 attr("forbid(…)", Some("forbid"), Some("forbid(${0:lint})")),
251 attr("global_allocator", None, None),
252 attr(r#"ignore = "…""#, Some("ignore"), Some(r#"ignore = "${0:reason}""#)),
253 attr("inline", Some("inline"), Some("inline")),
254 attr("link", None, None),
255 attr(r#"link_name = "…""#, Some("link_name"), Some(r#"link_name = "${0:symbol_name}""#)),
257 r#"link_section = "…""#,
258 Some("link_section"),
259 Some(r#"link_section = "${0:section_name}""#),
261 attr("macro_export", None, None),
262 attr("macro_use", None, None),
263 attr(r#"must_use"#, Some("must_use"), Some(r#"must_use"#)),
264 attr("no_implicit_prelude", None, None).prefer_inner(),
265 attr("no_link", None, None).prefer_inner(),
266 attr("no_main", None, None).prefer_inner(),
267 attr("no_mangle", None, None),
268 attr("no_std", None, None).prefer_inner(),
269 attr("non_exhaustive", None, None),
270 attr("panic_handler", None, None),
271 attr(r#"path = "…""#, Some("path"), Some(r#"path ="${0:path}""#)),
272 attr("proc_macro", None, None),
273 attr("proc_macro_attribute", None, None),
274 attr("proc_macro_derive(…)", Some("proc_macro_derive"), Some("proc_macro_derive(${0:Trait})")),
275 attr("recursion_limit = …", Some("recursion_limit"), Some("recursion_limit = ${0:128}"))
277 attr("repr(…)", Some("repr"), Some("repr(${0:C})")),
278 attr("should_panic", Some("should_panic"), Some(r#"should_panic"#)),
280 r#"target_feature = "…""#,
281 Some("target_feature"),
282 Some(r#"target_feature = "${0:feature}""#),
284 attr("test", None, None),
285 attr("track_caller", None, None),
286 attr("type_length_limit = …", Some("type_length_limit"), Some("type_length_limit = ${0:128}"))
288 attr("used", None, None),
289 attr("warn(…)", Some("warn"), Some("warn(${0:lint})")),
291 r#"windows_subsystem = "…""#,
292 Some("windows_subsystem"),
293 Some(r#"windows_subsystem = "${0:subsystem}""#),
298 fn parse_comma_sep_input(derive_input: ast::TokenTree) -> Option<FxHashSet<String>> {
299 let (l_paren, r_paren) = derive_input.l_paren_token().zip(derive_input.r_paren_token())?;
300 let mut input_derives = FxHashSet::default();
301 let mut tokens = derive_input
303 .children_with_tokens()
304 .filter_map(NodeOrToken::into_token)
305 .skip_while(|token| token != &l_paren)
307 .take_while(|token| token != &r_paren)
309 let mut input = String::new();
310 while tokens.peek().is_some() {
311 for token in tokens.by_ref().take_while(|t| t.kind() != T![,]) {
312 input.push_str(token.text());
315 if !input.is_empty() {
316 input_derives.insert(input.trim().to_owned());
329 use expect_test::{expect, Expect};
331 use crate::tests::completion_list;
334 fn attributes_are_sorted() {
335 let mut attrs = ATTRIBUTES.iter().map(|attr| attr.key());
336 let mut prev = attrs.next().unwrap();
338 attrs.for_each(|next| {
341 r#"ATTRIBUTES array is not sorted, "{}" should come after "{}""#,
349 fn check(ra_fixture: &str, expect: Expect) {
350 let actual = completion_list(ra_fixture);
351 expect.assert_eq(&actual);
355 fn test_attribute_completion_inside_nested_attr() {
356 check(r#"#[cfg($0)]"#, expect![[]])
360 fn test_attribute_completion_with_existing_attr() {
362 r#"#[no_mangle] #[$0] mcall!();"#,
375 fn complete_attribute_on_source_file() {
393 at no_implicit_prelude
396 at recursion_limit = …
397 at type_length_limit = …
398 at windows_subsystem = "…"
404 fn complete_attribute_on_module() {
424 r#"mod foo {#![$0]}"#,
438 at no_implicit_prelude
444 fn complete_attribute_on_macro_rules() {
446 r#"#[$0] macro_rules! foo {}"#,
467 fn complete_attribute_on_macro_def() {
469 r#"#[$0] macro foo {}"#,
488 fn complete_attribute_on_extern_crate() {
490 r#"#[$0] extern crate foo;"#,
510 fn complete_attribute_on_use() {
531 fn complete_attribute_on_type_alias() {
533 r#"#[$0] type foo = ();"#,
552 fn complete_attribute_on_struct() {
554 r#"#[$0] struct Foo;"#,
576 fn complete_attribute_on_enum() {
578 r#"#[$0] enum Foo {}"#,
600 fn complete_attribute_on_const() {
602 r#"#[$0] const FOO: () = ();"#,
621 fn complete_attribute_on_static() {
623 r#"#[$0] static FOO: () = ()"#,
639 at link_section = "…"
647 fn complete_attribute_on_trait() {
649 r#"#[$0] trait Foo {}"#,
669 fn complete_attribute_on_impl() {
671 r#"#[$0] impl () {}"#,
685 at automatically_derived
689 r#"impl () {#![$0]}"#,
708 fn complete_attribute_on_extern_block() {
710 r#"#[$0] extern {}"#,
728 r#"extern {#![$0]}"#,
748 fn complete_attribute_on_variant() {
750 r#"enum Foo { #[$0] Bar }"#,
764 fn complete_attribute_on_fn() {
766 r#"#[$0] fn main() {}"#,
782 at link_section = "…"
789 at proc_macro_derive(…)
790 at proc_macro_attribute
792 at target_feature = "…"
800 fn complete_attribute_on_expr() {
801 cov_mark::check!(no_keyword_completion_in_attr_of_expr);
803 r#"fn main() { #[$0] foo() }"#,
816 fn complete_attribute_in_source_file_end() {
821 at automatically_derived
838 at link_section = "…"
847 at proc_macro_attribute
848 at proc_macro_derive(…)
851 at target_feature = "…"
863 r#"#[cfg(target_endian = $0"#,