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},
22 pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
23 let attribute = ctx.attribute_under_caret.as_ref()?;
24 match (attribute.path().and_then(|p| p.as_single_name_ref()), attribute.token_tree()) {
25 (Some(path), Some(token_tree)) => match path.text().as_str() {
26 "derive" => derive::complete_derive(acc, ctx, token_tree),
27 "repr" => repr::complete_repr(acc, ctx, token_tree),
28 "feature" => lint::complete_lint(acc, ctx, token_tree, FEATURES),
29 "allow" | "warn" | "deny" | "forbid" => {
30 lint::complete_lint(acc, ctx, token_tree.clone(), DEFAULT_LINTS);
31 lint::complete_lint(acc, ctx, token_tree, CLIPPY_LINTS);
35 (None, Some(_)) => (),
36 _ => complete_new_attribute(acc, ctx, attribute),
41 fn complete_new_attribute(acc: &mut Completions, ctx: &CompletionContext, attribute: &ast::Attr) {
42 let is_inner = attribute.kind() == ast::AttrKind::Inner;
43 let attribute_annotated_item_kind =
44 attribute.syntax().parent().map(|it| it.kind()).filter(|_| {
46 // If we got nothing coming after the attribute it could be anything so filter it the kind out
47 || non_trivia_sibling(attribute.syntax().clone().into(), Direction::Next).is_some()
49 let attributes = attribute_annotated_item_kind.and_then(|kind| {
50 if ast::Expr::can_cast(kind) {
53 KIND_TO_ATTRIBUTES.get(&kind).copied()
57 let add_completion = |attr_completion: &AttrCompletion| {
58 let mut item = CompletionItem::new(
59 CompletionKind::Attribute,
61 attr_completion.label,
63 item.kind(CompletionItemKind::Attribute);
65 if let Some(lookup) = attr_completion.lookup {
66 item.lookup_by(lookup);
69 if let Some((snippet, cap)) = attr_completion.snippet.zip(ctx.config.snippet_cap) {
70 item.insert_snippet(cap, snippet);
73 if is_inner || !attr_completion.prefer_inner {
79 Some(applicable) => applicable
81 .flat_map(|name| ATTRIBUTES.binary_search_by(|attr| attr.key().cmp(name)).ok())
82 .flat_map(|idx| ATTRIBUTES.get(idx))
83 .for_each(add_completion),
84 None if is_inner => ATTRIBUTES.iter().for_each(add_completion),
85 None => ATTRIBUTES.iter().filter(|compl| !compl.prefer_inner).for_each(add_completion),
88 // FIXME: write a test for this when we can
89 ctx.scope.process_all_names(&mut |name, scope_def| {
90 if let hir::ScopeDef::MacroDef(mac) = scope_def {
91 if mac.kind() == hir::MacroKind::Attr {
92 let mut item = CompletionItem::new(
93 CompletionKind::Attribute,
97 item.kind(CompletionItemKind::Attribute);
98 if let Some(docs) = mac.docs(ctx.sema.db) {
99 item.documentation(docs);
107 struct AttrCompletion {
109 lookup: Option<&'static str>,
110 snippet: Option<&'static str>,
114 impl AttrCompletion {
115 fn key(&self) -> &'static str {
116 self.lookup.unwrap_or(self.label)
119 const fn prefer_inner(self) -> AttrCompletion {
120 AttrCompletion { prefer_inner: true, ..self }
126 lookup: Option<&'static str>,
127 snippet: Option<&'static str>,
128 ) -> AttrCompletion {
129 AttrCompletion { label, lookup, snippet, prefer_inner: false }
133 // attributes applicable to all items
134 [@ { item $($tt:tt)* } {$($acc:tt)*}] => {
135 attrs!(@ { $($tt)* } { $($acc)*, "deprecated", "doc", "dochidden", "docalias", "must_use", "no_mangle" })
137 // attributes applicable to all adts
138 [@ { adt $($tt:tt)* } {$($acc:tt)*}] => {
139 attrs!(@ { $($tt)* } { $($acc)*, "derive", "repr" })
141 // attributes applicable to all linkable things aka functions/statics
142 [@ { linkable $($tt:tt)* } {$($acc:tt)*}] => {
143 attrs!(@ { $($tt)* } { $($acc)*, "export_name", "link_name", "link_section" })
145 // error fallback for nicer error message
146 [@ { $ty:ident $($tt:tt)* } {$($acc:tt)*}] => {
147 compile_error!(concat!("unknown attr subtype ", stringify!($ty)))
149 // general push down accumulation
150 [@ { $lit:literal $($tt:tt)*} {$($acc:tt)*}] => {
151 attrs!(@ { $($tt)* } { $($acc)*, $lit })
153 [@ {$($tt:tt)+} {$($tt2:tt)*}] => {
154 compile_error!(concat!("Unexpected input ", stringify!($($tt)+)))
156 // final output construction
157 [@ {} {$($tt:tt)*}] => { &[$($tt)*] as _ };
160 attrs!(@ { $($tt)* } { "allow", "cfg", "cfg_attr", "deny", "forbid", "warn" })
165 static KIND_TO_ATTRIBUTES: Lazy<FxHashMap<SyntaxKind, &[&str]>> = Lazy::new(|| {
167 std::array::IntoIter::new([
172 "crate_name", "feature", "no_implicit_prelude", "no_main", "no_std",
173 "recursion_limit", "type_length_limit", "windows_subsystem"
176 (MODULE, attrs!(item, "no_implicit_prelude", "path")),
177 (ITEM_LIST, attrs!(item, "no_implicit_prelude")),
178 (MACRO_RULES, attrs!(item, "macro_export", "macro_use")),
179 (MACRO_DEF, attrs!(item)),
180 (EXTERN_CRATE, attrs!(item, "macro_use", "no_link")),
182 (TYPE_ALIAS, attrs!(item)),
183 (STRUCT, attrs!(item, adt, "non_exhaustive")),
184 (ENUM, attrs!(item, adt, "non_exhaustive")),
185 (UNION, attrs!(item, adt)),
186 (CONST, attrs!(item)),
191 "cold", "ignore", "inline", "must_use", "panic_handler", "proc_macro",
192 "proc_macro_derive", "proc_macro_attribute", "should_panic", "target_feature",
193 "test", "track_caller"
196 (STATIC, attrs!(item, linkable, "global_allocator", "used")),
197 (TRAIT, attrs!(item, "must_use")),
198 (IMPL, attrs!(item, "automatically_derived")),
199 (ASSOC_ITEM_LIST, attrs!(item)),
200 (EXTERN_BLOCK, attrs!(item, "link")),
201 (EXTERN_ITEM_LIST, attrs!(item, "link")),
202 (MACRO_CALL, attrs!()),
203 (SELF_PARAM, attrs!()),
205 (RECORD_FIELD, attrs!()),
206 (VARIANT, attrs!("non_exhaustive")),
207 (TYPE_PARAM, attrs!()),
208 (CONST_PARAM, attrs!()),
209 (LIFETIME_PARAM, attrs!()),
210 (LET_STMT, attrs!()),
211 (EXPR_STMT, attrs!()),
213 (RECORD_EXPR_FIELD_LIST, attrs!()),
214 (RECORD_EXPR_FIELD, attrs!()),
215 (MATCH_ARM_LIST, attrs!()),
216 (MATCH_ARM, attrs!()),
217 (IDENT_PAT, attrs!()),
218 (RECORD_PAT_FIELD, attrs!()),
222 const EXPR_ATTRIBUTES: &[&str] = attrs!();
224 /// <https://doc.rust-lang.org/reference/attributes.html#built-in-attributes-index>
225 // Keep these sorted for the binary search!
226 const ATTRIBUTES: &[AttrCompletion] = &[
227 attr("allow(…)", Some("allow"), Some("allow(${0:lint})")),
228 attr("automatically_derived", None, None),
229 attr("cfg(…)", Some("cfg"), Some("cfg(${0:predicate})")),
230 attr("cfg_attr(…)", Some("cfg_attr"), Some("cfg_attr(${1:predicate}, ${0:attr})")),
231 attr("cold", None, None),
232 attr(r#"crate_name = """#, Some("crate_name"), Some(r#"crate_name = "${0:crate_name}""#))
234 attr("deny(…)", Some("deny"), Some("deny(${0:lint})")),
235 attr(r#"deprecated"#, Some("deprecated"), Some(r#"deprecated"#)),
236 attr("derive(…)", Some("derive"), Some(r#"derive(${0:Debug})"#)),
237 attr(r#"doc = "…""#, Some("doc"), Some(r#"doc = "${0:docs}""#)),
238 attr(r#"doc(alias = "…")"#, Some("docalias"), Some(r#"doc(alias = "${0:docs}")"#)),
239 attr(r#"doc(hidden)"#, Some("dochidden"), Some(r#"doc(hidden)"#)),
241 r#"export_name = "…""#,
243 Some(r#"export_name = "${0:exported_symbol_name}""#),
245 attr("feature(…)", Some("feature"), Some("feature(${0:flag})")).prefer_inner(),
246 attr("forbid(…)", Some("forbid"), Some("forbid(${0:lint})")),
247 attr("global_allocator", None, None),
248 attr(r#"ignore = "…""#, Some("ignore"), Some(r#"ignore = "${0:reason}""#)),
249 attr("inline", Some("inline"), Some("inline")),
250 attr("link", None, None),
251 attr(r#"link_name = "…""#, Some("link_name"), Some(r#"link_name = "${0:symbol_name}""#)),
253 r#"link_section = "…""#,
254 Some("link_section"),
255 Some(r#"link_section = "${0:section_name}""#),
257 attr("macro_export", None, None),
258 attr("macro_use", None, None),
259 attr(r#"must_use"#, Some("must_use"), Some(r#"must_use"#)),
260 attr("no_implicit_prelude", None, None).prefer_inner(),
261 attr("no_link", None, None).prefer_inner(),
262 attr("no_main", None, None).prefer_inner(),
263 attr("no_mangle", None, None),
264 attr("no_std", None, None).prefer_inner(),
265 attr("non_exhaustive", None, None),
266 attr("panic_handler", None, None),
267 attr(r#"path = "…""#, Some("path"), Some(r#"path ="${0:path}""#)),
268 attr("proc_macro", None, None),
269 attr("proc_macro_attribute", None, None),
270 attr("proc_macro_derive(…)", Some("proc_macro_derive"), Some("proc_macro_derive(${0:Trait})")),
271 attr("recursion_limit = …", Some("recursion_limit"), Some("recursion_limit = ${0:128}"))
273 attr("repr(…)", Some("repr"), Some("repr(${0:C})")),
274 attr("should_panic", Some("should_panic"), Some(r#"should_panic"#)),
276 r#"target_feature = "…""#,
277 Some("target_feature"),
278 Some(r#"target_feature = "${0:feature}""#),
280 attr("test", None, None),
281 attr("track_caller", None, None),
282 attr("type_length_limit = …", Some("type_length_limit"), Some("type_length_limit = ${0:128}"))
284 attr("used", None, None),
285 attr("warn(…)", Some("warn"), Some("warn(${0:lint})")),
287 r#"windows_subsystem = "…""#,
288 Some("windows_subsystem"),
289 Some(r#"windows_subsystem = "${0:subsystem}""#),
294 fn parse_comma_sep_input(derive_input: ast::TokenTree) -> Option<FxHashSet<String>> {
295 let (l_paren, r_paren) = derive_input.l_paren_token().zip(derive_input.r_paren_token())?;
296 let mut input_derives = FxHashSet::default();
297 let mut tokens = derive_input
299 .children_with_tokens()
300 .filter_map(NodeOrToken::into_token)
301 .skip_while(|token| token != &l_paren)
303 .take_while(|token| token != &r_paren)
305 let mut input = String::new();
306 while tokens.peek().is_some() {
307 for token in tokens.by_ref().take_while(|t| t.kind() != T![,]) {
308 input.push_str(token.text());
311 if !input.is_empty() {
312 input_derives.insert(input.trim().to_owned());
325 use expect_test::{expect, Expect};
327 use crate::tests::completion_list;
330 fn attributes_are_sorted() {
331 let mut attrs = ATTRIBUTES.iter().map(|attr| attr.key());
332 let mut prev = attrs.next().unwrap();
334 attrs.for_each(|next| {
337 r#"ATTRIBUTES array is not sorted, "{}" should come after "{}""#,
345 fn check(ra_fixture: &str, expect: Expect) {
346 let actual = completion_list(ra_fixture);
347 expect.assert_eq(&actual);
351 fn test_attribute_completion_inside_nested_attr() {
352 check(r#"#[cfg($0)]"#, expect![[]])
356 fn test_attribute_completion_with_existing_attr() {
358 r#"#[no_mangle] #[$0] mcall!();"#,
371 fn complete_attribute_on_source_file() {
389 at no_implicit_prelude
392 at recursion_limit = …
393 at type_length_limit = …
394 at windows_subsystem = "…"
400 fn complete_attribute_on_module() {
420 r#"mod foo {#![$0]}"#,
434 at no_implicit_prelude
440 fn complete_attribute_on_macro_rules() {
442 r#"#[$0] macro_rules! foo {}"#,
463 fn complete_attribute_on_macro_def() {
465 r#"#[$0] macro foo {}"#,
484 fn complete_attribute_on_extern_crate() {
486 r#"#[$0] extern crate foo;"#,
506 fn complete_attribute_on_use() {
527 fn complete_attribute_on_type_alias() {
529 r#"#[$0] type foo = ();"#,
548 fn complete_attribute_on_struct() {
550 r#"#[$0] struct Foo;"#,
572 fn complete_attribute_on_enum() {
574 r#"#[$0] enum Foo {}"#,
596 fn complete_attribute_on_const() {
598 r#"#[$0] const FOO: () = ();"#,
617 fn complete_attribute_on_static() {
619 r#"#[$0] static FOO: () = ()"#,
635 at link_section = "…"
643 fn complete_attribute_on_trait() {
645 r#"#[$0] trait Foo {}"#,
665 fn complete_attribute_on_impl() {
667 r#"#[$0] impl () {}"#,
681 at automatically_derived
685 r#"impl () {#![$0]}"#,
704 fn complete_attribute_on_extern_block() {
706 r#"#[$0] extern {}"#,
724 r#"extern {#![$0]}"#,
744 fn complete_attribute_on_variant() {
746 r#"enum Foo { #[$0] Bar }"#,
760 fn complete_attribute_on_fn() {
762 r#"#[$0] fn main() {}"#,
778 at link_section = "…"
785 at proc_macro_derive(…)
786 at proc_macro_attribute
788 at target_feature = "…"
797 fn complete_attribute_on_expr() {
799 r#"fn main() { #[$0] foo() }"#,
813 fn complete_attribute_in_source_file_end() {
818 at automatically_derived
835 at link_section = "…"
844 at proc_macro_attribute
845 at proc_macro_derive(…)
848 at target_feature = "…"