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 let name_ref = match attribute.path() {
26 Some(p) => Some(p.as_single_name_ref()?),
29 match (name_ref, attribute.token_tree()) {
30 (Some(path), Some(token_tree)) => match path.text().as_str() {
31 "derive" => derive::complete_derive(acc, ctx, token_tree),
32 "repr" => repr::complete_repr(acc, ctx, token_tree),
33 "feature" => lint::complete_lint(acc, ctx, token_tree, FEATURES),
34 "allow" | "warn" | "deny" | "forbid" => {
35 lint::complete_lint(acc, ctx, token_tree.clone(), DEFAULT_LINTS);
36 lint::complete_lint(acc, ctx, token_tree, CLIPPY_LINTS);
39 cfg::complete_cfg(acc, ctx);
43 (None, Some(_)) => (),
44 _ => complete_new_attribute(acc, ctx, attribute),
49 fn complete_new_attribute(acc: &mut Completions, ctx: &CompletionContext, attribute: &ast::Attr) {
50 let is_inner = attribute.kind() == ast::AttrKind::Inner;
51 let attribute_annotated_item_kind =
52 attribute.syntax().parent().map(|it| it.kind()).filter(|_| {
54 // If we got nothing coming after the attribute it could be anything so filter it the kind out
55 || non_trivia_sibling(attribute.syntax().clone().into(), Direction::Next).is_some()
57 let attributes = attribute_annotated_item_kind.and_then(|kind| {
58 if ast::Expr::can_cast(kind) {
61 KIND_TO_ATTRIBUTES.get(&kind).copied()
65 let add_completion = |attr_completion: &AttrCompletion| {
66 let mut item = CompletionItem::new(
67 CompletionKind::Attribute,
69 attr_completion.label,
71 item.kind(CompletionItemKind::Attribute);
73 if let Some(lookup) = attr_completion.lookup {
74 item.lookup_by(lookup);
77 if let Some((snippet, cap)) = attr_completion.snippet.zip(ctx.config.snippet_cap) {
78 item.insert_snippet(cap, snippet);
81 if is_inner || !attr_completion.prefer_inner {
87 Some(applicable) => applicable
89 .flat_map(|name| ATTRIBUTES.binary_search_by(|attr| attr.key().cmp(name)).ok())
90 .flat_map(|idx| ATTRIBUTES.get(idx))
91 .for_each(add_completion),
92 None if is_inner => ATTRIBUTES.iter().for_each(add_completion),
93 None => ATTRIBUTES.iter().filter(|compl| !compl.prefer_inner).for_each(add_completion),
96 // FIXME: write a test for this when we can
97 ctx.scope.process_all_names(&mut |name, scope_def| {
98 if let hir::ScopeDef::MacroDef(mac) = scope_def {
99 if mac.kind() == hir::MacroKind::Attr {
100 let mut item = CompletionItem::new(
101 CompletionKind::Attribute,
105 item.kind(CompletionItemKind::Attribute);
106 if let Some(docs) = mac.docs(ctx.sema.db) {
107 item.documentation(docs);
115 struct AttrCompletion {
117 lookup: Option<&'static str>,
118 snippet: Option<&'static str>,
122 impl AttrCompletion {
123 fn key(&self) -> &'static str {
124 self.lookup.unwrap_or(self.label)
127 const fn prefer_inner(self) -> AttrCompletion {
128 AttrCompletion { prefer_inner: true, ..self }
134 lookup: Option<&'static str>,
135 snippet: Option<&'static str>,
136 ) -> AttrCompletion {
137 AttrCompletion { label, lookup, snippet, prefer_inner: false }
141 // attributes applicable to all items
142 [@ { item $($tt:tt)* } {$($acc:tt)*}] => {
143 attrs!(@ { $($tt)* } { $($acc)*, "deprecated", "doc", "dochidden", "docalias", "must_use", "no_mangle" })
145 // attributes applicable to all adts
146 [@ { adt $($tt:tt)* } {$($acc:tt)*}] => {
147 attrs!(@ { $($tt)* } { $($acc)*, "derive", "repr" })
149 // attributes applicable to all linkable things aka functions/statics
150 [@ { linkable $($tt:tt)* } {$($acc:tt)*}] => {
151 attrs!(@ { $($tt)* } { $($acc)*, "export_name", "link_name", "link_section" })
153 // error fallback for nicer error message
154 [@ { $ty:ident $($tt:tt)* } {$($acc:tt)*}] => {
155 compile_error!(concat!("unknown attr subtype ", stringify!($ty)))
157 // general push down accumulation
158 [@ { $lit:literal $($tt:tt)*} {$($acc:tt)*}] => {
159 attrs!(@ { $($tt)* } { $($acc)*, $lit })
161 [@ {$($tt:tt)+} {$($tt2:tt)*}] => {
162 compile_error!(concat!("Unexpected input ", stringify!($($tt)+)))
164 // final output construction
165 [@ {} {$($tt:tt)*}] => { &[$($tt)*] as _ };
168 attrs!(@ { $($tt)* } { "allow", "cfg", "cfg_attr", "deny", "forbid", "warn" })
173 static KIND_TO_ATTRIBUTES: Lazy<FxHashMap<SyntaxKind, &[&str]>> = Lazy::new(|| {
175 std::array::IntoIter::new([
180 "crate_name", "feature", "no_implicit_prelude", "no_main", "no_std",
181 "recursion_limit", "type_length_limit", "windows_subsystem"
184 (MODULE, attrs!(item, "macro_use", "no_implicit_prelude", "path")),
185 (ITEM_LIST, attrs!(item, "no_implicit_prelude")),
186 (MACRO_RULES, attrs!(item, "macro_export", "macro_use")),
187 (MACRO_DEF, attrs!(item)),
188 (EXTERN_CRATE, attrs!(item, "macro_use", "no_link")),
190 (TYPE_ALIAS, attrs!(item)),
191 (STRUCT, attrs!(item, adt, "non_exhaustive")),
192 (ENUM, attrs!(item, adt, "non_exhaustive")),
193 (UNION, attrs!(item, adt)),
194 (CONST, attrs!(item)),
199 "cold", "ignore", "inline", "must_use", "panic_handler", "proc_macro",
200 "proc_macro_derive", "proc_macro_attribute", "should_panic", "target_feature",
201 "test", "track_caller"
204 (STATIC, attrs!(item, linkable, "global_allocator", "used")),
205 (TRAIT, attrs!(item, "must_use")),
206 (IMPL, attrs!(item, "automatically_derived")),
207 (ASSOC_ITEM_LIST, attrs!(item)),
208 (EXTERN_BLOCK, attrs!(item, "link")),
209 (EXTERN_ITEM_LIST, attrs!(item, "link")),
210 (MACRO_CALL, attrs!()),
211 (SELF_PARAM, attrs!()),
213 (RECORD_FIELD, attrs!()),
214 (VARIANT, attrs!("non_exhaustive")),
215 (TYPE_PARAM, attrs!()),
216 (CONST_PARAM, attrs!()),
217 (LIFETIME_PARAM, attrs!()),
218 (LET_STMT, attrs!()),
219 (EXPR_STMT, attrs!()),
221 (RECORD_EXPR_FIELD_LIST, attrs!()),
222 (RECORD_EXPR_FIELD, attrs!()),
223 (MATCH_ARM_LIST, attrs!()),
224 (MATCH_ARM, attrs!()),
225 (IDENT_PAT, attrs!()),
226 (RECORD_PAT_FIELD, attrs!()),
230 const EXPR_ATTRIBUTES: &[&str] = attrs!();
232 /// <https://doc.rust-lang.org/reference/attributes.html#built-in-attributes-index>
233 // Keep these sorted for the binary search!
234 const ATTRIBUTES: &[AttrCompletion] = &[
235 attr("allow(…)", Some("allow"), Some("allow(${0:lint})")),
236 attr("automatically_derived", None, None),
237 attr("cfg(…)", Some("cfg"), Some("cfg(${0:predicate})")),
238 attr("cfg_attr(…)", Some("cfg_attr"), Some("cfg_attr(${1:predicate}, ${0:attr})")),
239 attr("cold", None, None),
240 attr(r#"crate_name = """#, Some("crate_name"), Some(r#"crate_name = "${0:crate_name}""#))
242 attr("deny(…)", Some("deny"), Some("deny(${0:lint})")),
243 attr(r#"deprecated"#, Some("deprecated"), Some(r#"deprecated"#)),
244 attr("derive(…)", Some("derive"), Some(r#"derive(${0:Debug})"#)),
245 attr(r#"doc = "…""#, Some("doc"), Some(r#"doc = "${0:docs}""#)),
246 attr(r#"doc(alias = "…")"#, Some("docalias"), Some(r#"doc(alias = "${0:docs}")"#)),
247 attr(r#"doc(hidden)"#, Some("dochidden"), Some(r#"doc(hidden)"#)),
249 r#"export_name = "…""#,
251 Some(r#"export_name = "${0:exported_symbol_name}""#),
253 attr("feature(…)", Some("feature"), Some("feature(${0:flag})")).prefer_inner(),
254 attr("forbid(…)", Some("forbid"), Some("forbid(${0:lint})")),
255 attr("global_allocator", None, None),
256 attr(r#"ignore = "…""#, Some("ignore"), Some(r#"ignore = "${0:reason}""#)),
257 attr("inline", Some("inline"), Some("inline")),
258 attr("link", None, None),
259 attr(r#"link_name = "…""#, Some("link_name"), Some(r#"link_name = "${0:symbol_name}""#)),
261 r#"link_section = "…""#,
262 Some("link_section"),
263 Some(r#"link_section = "${0:section_name}""#),
265 attr("macro_export", None, None),
266 attr("macro_use", None, None),
267 attr(r#"must_use"#, Some("must_use"), Some(r#"must_use"#)),
268 attr("no_implicit_prelude", None, None).prefer_inner(),
269 attr("no_link", None, None).prefer_inner(),
270 attr("no_main", None, None).prefer_inner(),
271 attr("no_mangle", None, None),
272 attr("no_std", None, None).prefer_inner(),
273 attr("non_exhaustive", None, None),
274 attr("panic_handler", None, None),
275 attr(r#"path = "…""#, Some("path"), Some(r#"path ="${0:path}""#)),
276 attr("proc_macro", None, None),
277 attr("proc_macro_attribute", None, None),
278 attr("proc_macro_derive(…)", Some("proc_macro_derive"), Some("proc_macro_derive(${0:Trait})")),
280 r#"recursion_limit = "…""#,
281 Some("recursion_limit"),
282 Some(r#"recursion_limit = "${0:128}""#),
285 attr("repr(…)", Some("repr"), Some("repr(${0:C})")),
286 attr("should_panic", Some("should_panic"), Some(r#"should_panic"#)),
288 r#"target_feature = "…""#,
289 Some("target_feature"),
290 Some(r#"target_feature = "${0:feature}""#),
292 attr("test", None, None),
293 attr("track_caller", None, None),
294 attr("type_length_limit = …", Some("type_length_limit"), Some("type_length_limit = ${0:128}"))
296 attr("used", None, None),
297 attr("warn(…)", Some("warn"), Some("warn(${0:lint})")),
299 r#"windows_subsystem = "…""#,
300 Some("windows_subsystem"),
301 Some(r#"windows_subsystem = "${0:subsystem}""#),
306 fn parse_comma_sep_input(derive_input: ast::TokenTree) -> Option<FxHashSet<String>> {
307 let (l_paren, r_paren) = derive_input.l_paren_token().zip(derive_input.r_paren_token())?;
308 let mut input_derives = FxHashSet::default();
309 let mut tokens = derive_input
311 .children_with_tokens()
312 .filter_map(NodeOrToken::into_token)
313 .skip_while(|token| token != &l_paren)
315 .take_while(|token| token != &r_paren)
317 let mut input = String::new();
318 while tokens.peek().is_some() {
319 for token in tokens.by_ref().take_while(|t| t.kind() != T![,]) {
320 input.push_str(token.text());
323 if !input.is_empty() {
324 input_derives.insert(input.trim().to_owned());
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 "{}""#,