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