Skip to main content

ide_completion/completions/
attribute.rs

1//! Completion for (built-in) attributes, derives and lints.
2//!
3//! This module uses a bit of static metadata to provide completions for builtin-in attributes and lints.
4
5use std::sync::LazyLock;
6
7use ide_db::{FxHashMap, SymbolKind, syntax_helpers::node_ext::parse_tt_as_comma_sep_paths};
8use itertools::Itertools;
9use syntax::{
10    AstNode, Edition, SyntaxKind, T,
11    ast::{self, AttrKind},
12};
13
14use crate::{
15    Completions,
16    context::{AttrCtx, CompletionContext, PathCompletionCtx, Qualified},
17    item::CompletionItem,
18};
19
20mod cfg;
21mod derive;
22mod diagnostic;
23mod feature;
24mod lint;
25mod macro_use;
26mod repr;
27
28pub(crate) use self::cfg::complete_cfg;
29pub(crate) use self::derive::complete_derive_path;
30
31/// Complete inputs to known builtin attributes as well as derive attributes
32pub(crate) fn complete_known_attribute_input(
33    acc: &mut Completions,
34    ctx: &CompletionContext<'_, '_>,
35    colon_prefix: bool,
36    fake_attribute_under_caret: &ast::TokenTreeMeta,
37    extern_crate: Option<&ast::ExternCrate>,
38) -> Option<()> {
39    let attribute = fake_attribute_under_caret;
40    let path = attribute.path()?;
41    let segments = path.segments().map(|s| s.name_ref()).collect::<Option<Vec<_>>>()?;
42    let segments = segments.iter().map(|n| n.text()).collect::<Vec<_>>();
43    let segments = segments.iter().map(|t| t.as_str()).collect::<Vec<_>>();
44    let tt = attribute.token_tree()?;
45
46    match segments.as_slice() {
47        ["repr"] => repr::complete_repr(acc, ctx, &parse_comma_sep_expr(tt)?),
48        ["feature"] => {
49            feature::complete_feature(acc, ctx, &parse_tt_as_comma_sep_paths(tt, ctx.edition)?)
50        }
51        ["allow" | "expect" | "deny" | "forbid" | "warn"] => lint::complete_lint(
52            acc,
53            ctx,
54            colon_prefix,
55            &parse_tt_as_comma_sep_paths(tt, ctx.edition)?,
56        ),
57        ["macro_use"] => macro_use::complete_macro_use(
58            acc,
59            ctx,
60            extern_crate,
61            &parse_tt_as_comma_sep_paths(tt, ctx.edition)?,
62        ),
63        ["diagnostic", "on_unimplemented"] => {
64            diagnostic::complete_on_unimplemented(acc, ctx, &parse_comma_sep_expr(tt)?)
65        }
66        _ => (),
67    }
68    Some(())
69}
70
71pub(crate) fn complete_attribute_path(
72    acc: &mut Completions,
73    ctx: &CompletionContext<'_, '_>,
74    path_ctx @ PathCompletionCtx { qualified, .. }: &PathCompletionCtx<'_>,
75    &AttrCtx { kind, annotated_item_kind, ref derive_helpers }: &AttrCtx,
76) {
77    let is_inner = kind == AttrKind::Inner;
78
79    for (derive_helper, derive_name) in derive_helpers {
80        let mut item = CompletionItem::new(
81            SymbolKind::Attribute,
82            ctx.source_range(),
83            derive_helper.as_str(),
84            ctx.edition,
85        );
86        item.detail(format!("derive helper of `{derive_name}`"));
87        item.add_to(acc, ctx.db);
88    }
89
90    match qualified {
91        Qualified::With {
92            resolution: Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))),
93            super_chain_len,
94            ..
95        } => {
96            acc.add_super_keyword(ctx, *super_chain_len);
97
98            for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
99                match def {
100                    hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(ctx.db) => {
101                        acc.add_macro(ctx, path_ctx, m, name)
102                    }
103                    hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
104                        acc.add_module(ctx, path_ctx, m, name, vec![])
105                    }
106                    _ => (),
107                }
108            }
109            return;
110        }
111        // fresh use tree with leading colon2, only show crate roots
112        Qualified::Absolute => acc.add_crate_roots(ctx, path_ctx),
113        // only show modules in a fresh UseTree
114        Qualified::No => {
115            ctx.process_all_names(&mut |name, def, doc_aliases| match def {
116                hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(ctx.db) => {
117                    acc.add_macro(ctx, path_ctx, m, name)
118                }
119                hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
120                    acc.add_module(ctx, path_ctx, m, name, doc_aliases)
121                }
122                _ => (),
123            });
124            acc.add_nameref_keywords_with_colon(ctx);
125        }
126        Qualified::TypeAnchor { .. } | Qualified::With { .. } => {}
127    }
128    let qualifier_path =
129        if let Qualified::With { path, .. } = qualified { Some(path) } else { None };
130
131    let attributes = annotated_item_kind.and_then(|kind| {
132        if ast::Expr::can_cast(kind) {
133            Some(EXPR_ATTRIBUTES)
134        } else {
135            KIND_TO_ATTRIBUTES.get(&kind).copied()
136        }
137    });
138
139    let add_completion = |attr_completion: &AttrCompletion| {
140        // if we don't already have the qualifiers of the completion, then
141        // add the missing parts to the label and snippet
142        let mut label = attr_completion.label.to_owned();
143        let mut snippet = attr_completion.snippet.map(|s| s.to_owned());
144        let segments = qualifier_path.iter().flat_map(|q| q.segments()).collect::<Vec<_>>();
145        let qualifiers = attr_completion.qualifiers;
146        let matching_qualifiers = segments
147            .iter()
148            .zip(qualifiers)
149            .take_while(|(s, q)| s.name_ref().is_some_and(|t| t.text() == **q))
150            .count();
151        if matching_qualifiers != qualifiers.len() {
152            let prefix = qualifiers[matching_qualifiers..].join("::");
153            label = format!("{prefix}::{label}");
154            if let Some(s) = snippet.as_mut() {
155                *s = format!("{prefix}::{s}");
156            }
157        }
158
159        let mut item =
160            CompletionItem::new(SymbolKind::Attribute, ctx.source_range(), label, ctx.edition);
161
162        if let Some(lookup) = attr_completion.lookup {
163            item.lookup_by(lookup);
164        }
165
166        if let Some((snippet, cap)) = snippet.zip(ctx.config.snippet_cap) {
167            item.insert_snippet(cap, snippet);
168        }
169
170        if is_inner || !attr_completion.prefer_inner {
171            item.add_to(acc, ctx.db);
172        }
173    };
174
175    match attributes {
176        Some(applicable) => applicable
177            .iter()
178            .flat_map(|name| ATTRIBUTES.binary_search_by_key(name, |attr| attr.key()).ok())
179            .flat_map(|idx| ATTRIBUTES.get(idx))
180            .for_each(add_completion),
181        None if is_inner => ATTRIBUTES.iter().for_each(add_completion),
182        None => ATTRIBUTES.iter().filter(|compl| !compl.prefer_inner).for_each(add_completion),
183    }
184}
185
186struct AttrCompletion {
187    label: &'static str,
188    lookup: Option<&'static str>,
189    snippet: Option<&'static str>,
190    qualifiers: &'static [&'static str],
191    prefer_inner: bool,
192}
193
194impl AttrCompletion {
195    fn key(&self) -> &'static str {
196        self.lookup.unwrap_or(self.label)
197    }
198
199    const fn qualifiers(self, qualifiers: &'static [&'static str]) -> AttrCompletion {
200        AttrCompletion { qualifiers, ..self }
201    }
202
203    const fn prefer_inner(self) -> AttrCompletion {
204        AttrCompletion { prefer_inner: true, ..self }
205    }
206}
207
208const fn attr(
209    label: &'static str,
210    lookup: Option<&'static str>,
211    snippet: Option<&'static str>,
212) -> AttrCompletion {
213    AttrCompletion { label, lookup, snippet, qualifiers: &[], prefer_inner: false }
214}
215
216macro_rules! attrs {
217    // attributes applicable to all items
218    [@ { item $($tt:tt)* } {$($acc:tt)*}] => {
219        attrs!(@ { $($tt)* } { $($acc)*, "deprecated", "doc", "dochidden", "docalias", "docinclude", "must_use", "no_mangle", "unsafe" })
220    };
221    // attributes applicable to all adts
222    [@ { adt $($tt:tt)* } {$($acc:tt)*}] => {
223        attrs!(@ { $($tt)* } { $($acc)*, "derive", "repr" })
224    };
225    // attributes applicable to all linkable things aka functions/statics
226    [@ { linkable $($tt:tt)* } {$($acc:tt)*}] => {
227        attrs!(@ { $($tt)* } { $($acc)*, "export_name", "link_name", "link_section" })
228    };
229    // error fallback for nicer error message
230    [@ { $ty:ident $($tt:tt)* } {$($acc:tt)*}] => {
231        compile_error!(concat!("unknown attr subtype ", stringify!($ty)))
232    };
233    // general push down accumulation
234    [@ { $lit:literal $($tt:tt)*} {$($acc:tt)*}] => {
235        attrs!(@ { $($tt)* } { $($acc)*, $lit })
236    };
237    [@ {$($tt:tt)+} {$($tt2:tt)*}] => {
238        compile_error!(concat!("Unexpected input ", stringify!($($tt)+)))
239    };
240    // final output construction
241    [@ {} {$($tt:tt)*}] => { &[$($tt)*] as _ };
242    // starting matcher
243    [$($tt:tt),*] => {
244        attrs!(@ { $($tt)* } { "allow", "cfg", "cfg_attr", "deny", "expect", "forbid", "warn" })
245    };
246}
247
248#[rustfmt::skip]
249static KIND_TO_ATTRIBUTES: LazyLock<FxHashMap<SyntaxKind, &[&str]>> = LazyLock::new(|| {
250    use SyntaxKind::*;
251    [
252        (
253            SOURCE_FILE,
254            attrs!(
255                item,
256                "crate_name", "feature", "no_implicit_prelude", "no_main", "no_std",
257                "recursion_limit", "type_length_limit", "windows_subsystem"
258            ),
259        ),
260        (MODULE, attrs!(item, "macro_use", "no_implicit_prelude", "path")),
261        (ITEM_LIST, attrs!(item, "no_implicit_prelude")),
262        (MACRO_RULES, attrs!(item, "macro_export", "macro_use")),
263        (MACRO_DEF, attrs!(item)),
264        (EXTERN_CRATE, attrs!(item, "macro_use", "no_link")),
265        (USE, attrs!(item)),
266        (TYPE_ALIAS, attrs!(item)),
267        (STRUCT, attrs!(item, adt, "non_exhaustive")),
268        (ENUM, attrs!(item, adt, "non_exhaustive")),
269        (UNION, attrs!(item, adt)),
270        (CONST, attrs!(item)),
271        (
272            FN,
273            attrs!(
274                item, linkable,
275                "cold", "ignore", "inline", "panic_handler", "proc_macro",
276                "proc_macro_derive", "proc_macro_attribute", "should_panic", "target_feature",
277                "test", "track_caller"
278            ),
279        ),
280        (STATIC, attrs!(item, linkable, "global_allocator", "used")),
281        (TRAIT, attrs!(item, "diagnostic::on_unimplemented")),
282        (IMPL, attrs!(item, "automatically_derived", "diagnostic::do_not_recommend")),
283        (ASSOC_ITEM_LIST, attrs!(item)),
284        (EXTERN_BLOCK, attrs!(item, "link")),
285        (EXTERN_ITEM_LIST, attrs!(item, "link")),
286        (MACRO_CALL, attrs!()),
287        (SELF_PARAM, attrs!()),
288        (PARAM, attrs!()),
289        (RECORD_FIELD, attrs!()),
290        (VARIANT, attrs!("non_exhaustive")),
291        (TYPE_PARAM, attrs!()),
292        (CONST_PARAM, attrs!()),
293        (LIFETIME_PARAM, attrs!()),
294        (LET_STMT, attrs!()),
295        (EXPR_STMT, attrs!()),
296        (LITERAL, attrs!()),
297        (RECORD_EXPR_FIELD_LIST, attrs!()),
298        (RECORD_EXPR_FIELD, attrs!()),
299        (MATCH_ARM_LIST, attrs!()),
300        (MATCH_ARM, attrs!()),
301        (IDENT_PAT, attrs!()),
302        (RECORD_PAT_FIELD, attrs!()),
303    ]
304    .into_iter()
305    .collect()
306});
307const EXPR_ATTRIBUTES: &[&str] = attrs!();
308
309/// <https://doc.rust-lang.org/reference/attributes.html#built-in-attributes-index>
310// Keep these sorted for the binary search!
311const ATTRIBUTES: &[AttrCompletion] = &[
312    attr("allow(…)", Some("allow"), Some("allow(${0:lint})")),
313    attr("automatically_derived", None, None),
314    attr("cfg(…)", Some("cfg"), Some("cfg(${0:predicate})")),
315    attr("cfg_attr(…)", Some("cfg_attr"), Some("cfg_attr(${1:predicate}, ${0:attr})")),
316    attr("cold", None, None),
317    attr(r#"crate_name = """#, Some("crate_name"), Some(r#"crate_name = "${0:crate_name}""#))
318        .prefer_inner(),
319    attr("deny(…)", Some("deny"), Some("deny(${0:lint})")),
320    attr(r#"deprecated"#, Some("deprecated"), Some(r#"deprecated"#)),
321    attr("derive(…)", Some("derive"), Some(r#"derive(${0:Debug})"#)),
322    attr("do_not_recommend", Some("diagnostic::do_not_recommend"), None)
323        .qualifiers(&["diagnostic"]),
324    attr(
325        "on_unimplemented",
326        Some("diagnostic::on_unimplemented"),
327        Some(r#"on_unimplemented(${0:keys})"#),
328    )
329    .qualifiers(&["diagnostic"]),
330    attr(r#"doc = "…""#, Some("doc"), Some(r#"doc = "${0:docs}""#)),
331    attr(r#"doc(alias = "…")"#, Some("docalias"), Some(r#"doc(alias = "${0:docs}")"#)),
332    attr(r#"doc(hidden)"#, Some("dochidden"), Some(r#"doc(hidden)"#)),
333    attr(r#"doc = include_str!("…")"#, Some("docinclude"), Some(r#"doc = include_str!("$0")"#)),
334    attr("expect(…)", Some("expect"), Some("expect(${0:lint})")),
335    attr(
336        r#"export_name = "…""#,
337        Some("export_name"),
338        Some(r#"export_name = "${0:exported_symbol_name}""#),
339    ),
340    attr("feature(…)", Some("feature"), Some("feature(${0:flag})")).prefer_inner(),
341    attr("forbid(…)", Some("forbid"), Some("forbid(${0:lint})")),
342    attr("global_allocator", None, None),
343    attr(r#"ignore = "…""#, Some("ignore"), Some(r#"ignore = "${0:reason}""#)),
344    attr("inline", Some("inline"), Some("inline")),
345    attr("link", None, None),
346    attr(r#"link_name = "…""#, Some("link_name"), Some(r#"link_name = "${0:symbol_name}""#)),
347    attr(
348        r#"link_section = "…""#,
349        Some("link_section"),
350        Some(r#"link_section = "${0:section_name}""#),
351    ),
352    attr("macro_export", None, None),
353    attr("macro_use", None, None),
354    attr(r#"must_use"#, Some("must_use"), Some(r#"must_use"#)),
355    attr("no_implicit_prelude", None, None).prefer_inner(),
356    attr("no_link", None, None).prefer_inner(),
357    attr("no_main", None, None).prefer_inner(),
358    attr("no_mangle", None, None),
359    attr("no_std", None, None).prefer_inner(),
360    attr("non_exhaustive", None, None),
361    attr("panic_handler", None, None),
362    attr(r#"path = "…""#, Some("path"), Some(r#"path ="${0:path}""#)),
363    attr("proc_macro", None, None),
364    attr("proc_macro_attribute", None, None),
365    attr("proc_macro_derive(…)", Some("proc_macro_derive"), Some("proc_macro_derive(${0:Trait})")),
366    attr(
367        r#"recursion_limit = "…""#,
368        Some("recursion_limit"),
369        Some(r#"recursion_limit = "${0:128}""#),
370    )
371    .prefer_inner(),
372    attr("repr(…)", Some("repr"), Some("repr(${0:C})")),
373    attr("should_panic", Some("should_panic"), Some(r#"should_panic"#)),
374    attr(
375        r#"target_feature(enable = "…")"#,
376        Some("target_feature"),
377        Some(r#"target_feature(enable = "${0:feature}")"#),
378    ),
379    attr("test", None, None),
380    attr("track_caller", None, None),
381    attr("type_length_limit = …", Some("type_length_limit"), Some("type_length_limit = ${0:128}"))
382        .prefer_inner(),
383    attr("unsafe(…)", Some("unsafe"), Some("unsafe($0)")),
384    attr("used", None, None),
385    attr("warn(…)", Some("warn"), Some("warn(${0:lint})")),
386    attr(
387        r#"windows_subsystem = "…""#,
388        Some("windows_subsystem"),
389        Some(r#"windows_subsystem = "${0:subsystem}""#),
390    )
391    .prefer_inner(),
392];
393
394fn parse_comma_sep_expr(input: ast::TokenTree) -> Option<Vec<ast::Expr>> {
395    let r_paren = input.r_paren_token()?;
396    let tokens = input
397        .syntax()
398        .children_with_tokens()
399        .skip(1)
400        .take_while(|it| it.as_token() != Some(&r_paren));
401    let input_expressions = tokens.chunk_by(|tok| tok.kind() == T![,]);
402    Some(
403        input_expressions
404            .into_iter()
405            .filter_map(|(is_sep, group)| (!is_sep).then_some(group))
406            .filter_map(|mut tokens| {
407                syntax::hacks::parse_expr_from_str(&tokens.join(""), Edition::CURRENT)
408            })
409            .collect::<Vec<ast::Expr>>(),
410    )
411}
412
413#[test]
414fn attributes_are_sorted() {
415    let mut attrs = ATTRIBUTES.iter().map(|attr| attr.key());
416    let mut prev = attrs.next().unwrap();
417
418    attrs.for_each(|next| {
419        assert!(
420            prev < next,
421            r#"ATTRIBUTES array is not sorted, "{prev}" should come after "{next}""#
422        );
423        prev = next;
424    });
425}