1 //! Completion for attributes
3 //! This module uses a bit of static metadata to provide completions
4 //! for built-in attributes.
5 //! Non-builtin attribute(excluding derives attributes) completions are done in [`super::unqualified_path`].
9 generated_lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES, RUSTDOC_LINTS},
10 parse_tt_as_comma_sep_paths,
14 use itertools::Itertools;
15 use once_cell::sync::Lazy;
16 use rustc_hash::FxHashMap;
17 use syntax::{algo::non_trivia_sibling, ast, AstNode, Direction, SyntaxKind, T};
19 use crate::{context::CompletionContext, item::CompletionItem, Completions};
26 pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
27 let attribute = ctx.attribute_under_caret.as_ref()?;
28 let name_ref = match attribute.path() {
29 Some(p) => Some(p.as_single_name_ref()?),
32 match (name_ref, attribute.token_tree()) {
33 (Some(path), Some(tt)) if tt.l_paren_token().is_some() => match path.text().as_str() {
34 "repr" => repr::complete_repr(acc, ctx, tt),
35 "derive" => derive::complete_derive(acc, ctx, &parse_tt_as_comma_sep_paths(tt)?),
36 "feature" => lint::complete_lint(acc, ctx, &parse_tt_as_comma_sep_paths(tt)?, FEATURES),
37 "allow" | "warn" | "deny" | "forbid" => {
38 let existing_lints = parse_tt_as_comma_sep_paths(tt)?;
39 lint::complete_lint(acc, ctx, &existing_lints, DEFAULT_LINTS);
40 lint::complete_lint(acc, ctx, &existing_lints, CLIPPY_LINTS);
41 lint::complete_lint(acc, ctx, &existing_lints, RUSTDOC_LINTS);
44 cfg::complete_cfg(acc, ctx);
49 (_, None) => complete_new_attribute(acc, ctx, attribute),
54 fn complete_new_attribute(acc: &mut Completions, ctx: &CompletionContext, attribute: &ast::Attr) {
55 let is_inner = attribute.kind() == ast::AttrKind::Inner;
56 let attribute_annotated_item_kind =
57 attribute.syntax().parent().map(|it| it.kind()).filter(|_| {
59 // If we got nothing coming after the attribute it could be anything so filter it the kind out
60 || non_trivia_sibling(attribute.syntax().clone().into(), Direction::Next).is_some()
62 let attributes = attribute_annotated_item_kind.and_then(|kind| {
63 if ast::Expr::can_cast(kind) {
66 KIND_TO_ATTRIBUTES.get(&kind).copied()
70 let add_completion = |attr_completion: &AttrCompletion| {
72 CompletionItem::new(SymbolKind::Attribute, ctx.source_range(), attr_completion.label);
74 if let Some(lookup) = attr_completion.lookup {
75 item.lookup_by(lookup);
78 if let Some((snippet, cap)) = attr_completion.snippet.zip(ctx.config.snippet_cap) {
79 item.insert_snippet(cap, snippet);
82 if is_inner || !attr_completion.prefer_inner {
88 Some(applicable) => applicable
90 .flat_map(|name| ATTRIBUTES.binary_search_by(|attr| attr.key().cmp(name)).ok())
91 .flat_map(|idx| ATTRIBUTES.get(idx))
92 .for_each(add_completion),
93 None if is_inner => ATTRIBUTES.iter().for_each(add_completion),
94 None => ATTRIBUTES.iter().filter(|compl| !compl.prefer_inner).for_each(add_completion),
98 struct AttrCompletion {
100 lookup: Option<&'static str>,
101 snippet: Option<&'static str>,
105 impl AttrCompletion {
106 fn key(&self) -> &'static str {
107 self.lookup.unwrap_or(self.label)
110 const fn prefer_inner(self) -> AttrCompletion {
111 AttrCompletion { prefer_inner: true, ..self }
117 lookup: Option<&'static str>,
118 snippet: Option<&'static str>,
119 ) -> AttrCompletion {
120 AttrCompletion { label, lookup, snippet, prefer_inner: false }
124 // attributes applicable to all items
125 [@ { item $($tt:tt)* } {$($acc:tt)*}] => {
126 attrs!(@ { $($tt)* } { $($acc)*, "deprecated", "doc", "dochidden", "docalias", "must_use", "no_mangle" })
128 // attributes applicable to all adts
129 [@ { adt $($tt:tt)* } {$($acc:tt)*}] => {
130 attrs!(@ { $($tt)* } { $($acc)*, "derive", "repr" })
132 // attributes applicable to all linkable things aka functions/statics
133 [@ { linkable $($tt:tt)* } {$($acc:tt)*}] => {
134 attrs!(@ { $($tt)* } { $($acc)*, "export_name", "link_name", "link_section" })
136 // error fallback for nicer error message
137 [@ { $ty:ident $($tt:tt)* } {$($acc:tt)*}] => {
138 compile_error!(concat!("unknown attr subtype ", stringify!($ty)))
140 // general push down accumulation
141 [@ { $lit:literal $($tt:tt)*} {$($acc:tt)*}] => {
142 attrs!(@ { $($tt)* } { $($acc)*, $lit })
144 [@ {$($tt:tt)+} {$($tt2:tt)*}] => {
145 compile_error!(concat!("Unexpected input ", stringify!($($tt)+)))
147 // final output construction
148 [@ {} {$($tt:tt)*}] => { &[$($tt)*] as _ };
151 attrs!(@ { $($tt)* } { "allow", "cfg", "cfg_attr", "deny", "forbid", "warn" })
156 static KIND_TO_ATTRIBUTES: Lazy<FxHashMap<SyntaxKind, &[&str]>> = Lazy::new(|| {
163 "crate_name", "feature", "no_implicit_prelude", "no_main", "no_std",
164 "recursion_limit", "type_length_limit", "windows_subsystem"
167 (MODULE, attrs!(item, "macro_use", "no_implicit_prelude", "path")),
168 (ITEM_LIST, attrs!(item, "no_implicit_prelude")),
169 (MACRO_RULES, attrs!(item, "macro_export", "macro_use")),
170 (MACRO_DEF, attrs!(item)),
171 (EXTERN_CRATE, attrs!(item, "macro_use", "no_link")),
173 (TYPE_ALIAS, attrs!(item)),
174 (STRUCT, attrs!(item, adt, "non_exhaustive")),
175 (ENUM, attrs!(item, adt, "non_exhaustive")),
176 (UNION, attrs!(item, adt)),
177 (CONST, attrs!(item)),
182 "cold", "ignore", "inline", "must_use", "panic_handler", "proc_macro",
183 "proc_macro_derive", "proc_macro_attribute", "should_panic", "target_feature",
184 "test", "track_caller"
187 (STATIC, attrs!(item, linkable, "global_allocator", "used")),
188 (TRAIT, attrs!(item, "must_use")),
189 (IMPL, attrs!(item, "automatically_derived")),
190 (ASSOC_ITEM_LIST, attrs!(item)),
191 (EXTERN_BLOCK, attrs!(item, "link")),
192 (EXTERN_ITEM_LIST, attrs!(item, "link")),
193 (MACRO_CALL, attrs!()),
194 (SELF_PARAM, attrs!()),
196 (RECORD_FIELD, attrs!()),
197 (VARIANT, attrs!("non_exhaustive")),
198 (TYPE_PARAM, attrs!()),
199 (CONST_PARAM, attrs!()),
200 (LIFETIME_PARAM, attrs!()),
201 (LET_STMT, attrs!()),
202 (EXPR_STMT, attrs!()),
204 (RECORD_EXPR_FIELD_LIST, attrs!()),
205 (RECORD_EXPR_FIELD, attrs!()),
206 (MATCH_ARM_LIST, attrs!()),
207 (MATCH_ARM, attrs!()),
208 (IDENT_PAT, attrs!()),
209 (RECORD_PAT_FIELD, attrs!()),
214 const EXPR_ATTRIBUTES: &[&str] = attrs!();
216 /// <https://doc.rust-lang.org/reference/attributes.html#built-in-attributes-index>
217 // Keep these sorted for the binary search!
218 const ATTRIBUTES: &[AttrCompletion] = &[
219 attr("allow(…)", Some("allow"), Some("allow(${0:lint})")),
220 attr("automatically_derived", None, None),
221 attr("cfg(…)", Some("cfg"), Some("cfg(${0:predicate})")),
222 attr("cfg_attr(…)", Some("cfg_attr"), Some("cfg_attr(${1:predicate}, ${0:attr})")),
223 attr("cold", None, None),
224 attr(r#"crate_name = """#, Some("crate_name"), Some(r#"crate_name = "${0:crate_name}""#))
226 attr("deny(…)", Some("deny"), Some("deny(${0:lint})")),
227 attr(r#"deprecated"#, Some("deprecated"), Some(r#"deprecated"#)),
228 attr("derive(…)", Some("derive"), Some(r#"derive(${0:Debug})"#)),
229 attr(r#"doc = "…""#, Some("doc"), Some(r#"doc = "${0:docs}""#)),
230 attr(r#"doc(alias = "…")"#, Some("docalias"), Some(r#"doc(alias = "${0:docs}")"#)),
231 attr(r#"doc(hidden)"#, Some("dochidden"), Some(r#"doc(hidden)"#)),
233 r#"export_name = "…""#,
235 Some(r#"export_name = "${0:exported_symbol_name}""#),
237 attr("feature(…)", Some("feature"), Some("feature(${0:flag})")).prefer_inner(),
238 attr("forbid(…)", Some("forbid"), Some("forbid(${0:lint})")),
239 attr("global_allocator", None, None),
240 attr(r#"ignore = "…""#, Some("ignore"), Some(r#"ignore = "${0:reason}""#)),
241 attr("inline", Some("inline"), Some("inline")),
242 attr("link", None, None),
243 attr(r#"link_name = "…""#, Some("link_name"), Some(r#"link_name = "${0:symbol_name}""#)),
245 r#"link_section = "…""#,
246 Some("link_section"),
247 Some(r#"link_section = "${0:section_name}""#),
249 attr("macro_export", None, None),
250 attr("macro_use", None, None),
251 attr(r#"must_use"#, Some("must_use"), Some(r#"must_use"#)),
252 attr("no_implicit_prelude", None, None).prefer_inner(),
253 attr("no_link", None, None).prefer_inner(),
254 attr("no_main", None, None).prefer_inner(),
255 attr("no_mangle", None, None),
256 attr("no_std", None, None).prefer_inner(),
257 attr("non_exhaustive", None, None),
258 attr("panic_handler", None, None),
259 attr(r#"path = "…""#, Some("path"), Some(r#"path ="${0:path}""#)),
260 attr("proc_macro", None, None),
261 attr("proc_macro_attribute", None, None),
262 attr("proc_macro_derive(…)", Some("proc_macro_derive"), Some("proc_macro_derive(${0:Trait})")),
264 r#"recursion_limit = "…""#,
265 Some("recursion_limit"),
266 Some(r#"recursion_limit = "${0:128}""#),
269 attr("repr(…)", Some("repr"), Some("repr(${0:C})")),
270 attr("should_panic", Some("should_panic"), Some(r#"should_panic"#)),
272 r#"target_feature = "…""#,
273 Some("target_feature"),
274 Some(r#"target_feature = "${0:feature}""#),
276 attr("test", None, None),
277 attr("track_caller", None, None),
278 attr("type_length_limit = …", Some("type_length_limit"), Some("type_length_limit = ${0:128}"))
280 attr("used", None, None),
281 attr("warn(…)", Some("warn"), Some("warn(${0:lint})")),
283 r#"windows_subsystem = "…""#,
284 Some("windows_subsystem"),
285 Some(r#"windows_subsystem = "${0:subsystem}""#),
290 fn parse_comma_sep_expr(input: ast::TokenTree) -> Option<Vec<ast::Expr>> {
291 let r_paren = input.r_paren_token()?;
294 .children_with_tokens()
296 .take_while(|it| it.as_token() != Some(&r_paren));
297 let input_expressions = tokens.into_iter().group_by(|tok| tok.kind() == T![,]);
301 .filter_map(|(is_sep, group)| (!is_sep).then(|| group))
302 .filter_map(|mut tokens| ast::Expr::parse(&tokens.join("")).ok())
303 .collect::<Vec<ast::Expr>>(),
308 fn attributes_are_sorted() {
309 let mut attrs = ATTRIBUTES.iter().map(|attr| attr.key());
310 let mut prev = attrs.next().unwrap();
312 attrs.for_each(|next| {
315 r#"ATTRIBUTES array is not sorted, "{}" should come after "{}""#,