1 //! Completion for (built-in) attributes, derives and lints.
3 //! This module uses a bit of static metadata to provide completions for builtin-in attributes and lints.
7 Lint, CLIPPY_LINTS, CLIPPY_LINT_GROUPS, DEFAULT_LINTS, FEATURES, RUSTDOC_LINTS,
9 syntax_helpers::node_ext::parse_tt_as_comma_sep_paths,
10 FxHashMap, SymbolKind,
12 use itertools::Itertools;
13 use once_cell::sync::Lazy;
15 ast::{self, AttrKind},
16 AstNode, SyntaxKind, T,
20 context::{AttrCtx, CompletionContext, PathCompletionCtx, Qualified},
30 pub(crate) use self::derive::complete_derive_path;
32 /// Complete inputs to known builtin attributes as well as derive attributes
33 pub(crate) fn complete_known_attribute_input(
34 acc: &mut Completions,
35 ctx: &CompletionContext<'_>,
37 fake_attribute_under_caret: &ast::Attr,
39 let attribute = fake_attribute_under_caret;
40 let name_ref = match attribute.path() {
41 Some(p) => Some(p.as_single_name_ref()?),
44 let (path, tt) = name_ref.zip(attribute.token_tree())?;
45 if tt.l_paren_token().is_none() {
49 match path.text().as_str() {
50 "repr" => repr::complete_repr(acc, ctx, tt),
52 lint::complete_lint(acc, ctx, colon_prefix, &parse_tt_as_comma_sep_paths(tt)?, FEATURES)
54 "allow" | "warn" | "deny" | "forbid" => {
55 let existing_lints = parse_tt_as_comma_sep_paths(tt)?;
57 let lints: Vec<Lint> = CLIPPY_LINT_GROUPS
66 lint::complete_lint(acc, ctx, colon_prefix, &existing_lints, &lints);
68 "cfg" => cfg::complete_cfg(acc, ctx),
74 pub(crate) fn complete_attribute_path(
75 acc: &mut Completions,
76 ctx: &CompletionContext<'_>,
77 path_ctx @ PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
78 &AttrCtx { kind, annotated_item_kind }: &AttrCtx,
80 let is_inner = kind == AttrKind::Inner;
84 resolution: Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))),
88 acc.add_super_keyword(ctx, *super_chain_len);
90 for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
92 hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(ctx.db) => {
93 acc.add_macro(ctx, path_ctx, m, name)
95 hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
96 acc.add_module(ctx, path_ctx, m, name)
103 // fresh use tree with leading colon2, only show crate roots
104 Qualified::Absolute => acc.add_crate_roots(ctx, path_ctx),
105 // only show modules in a fresh UseTree
107 ctx.process_all_names(&mut |name, def| match def {
108 hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(ctx.db) => {
109 acc.add_macro(ctx, path_ctx, m, name)
111 hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
112 acc.add_module(ctx, path_ctx, m, name)
116 acc.add_nameref_keywords_with_colon(ctx);
118 Qualified::Infer | Qualified::With { .. } => {}
121 let attributes = annotated_item_kind.and_then(|kind| {
122 if ast::Expr::can_cast(kind) {
123 Some(EXPR_ATTRIBUTES)
125 KIND_TO_ATTRIBUTES.get(&kind).copied()
129 let add_completion = |attr_completion: &AttrCompletion| {
131 CompletionItem::new(SymbolKind::Attribute, ctx.source_range(), attr_completion.label);
133 if let Some(lookup) = attr_completion.lookup {
134 item.lookup_by(lookup);
137 if let Some((snippet, cap)) = attr_completion.snippet.zip(ctx.config.snippet_cap) {
138 item.insert_snippet(cap, snippet);
141 if is_inner || !attr_completion.prefer_inner {
147 Some(applicable) => applicable
149 .flat_map(|name| ATTRIBUTES.binary_search_by(|attr| attr.key().cmp(name)).ok())
150 .flat_map(|idx| ATTRIBUTES.get(idx))
151 .for_each(add_completion),
152 None if is_inner => ATTRIBUTES.iter().for_each(add_completion),
153 None => ATTRIBUTES.iter().filter(|compl| !compl.prefer_inner).for_each(add_completion),
157 struct AttrCompletion {
159 lookup: Option<&'static str>,
160 snippet: Option<&'static str>,
164 impl AttrCompletion {
165 fn key(&self) -> &'static str {
166 self.lookup.unwrap_or(self.label)
169 const fn prefer_inner(self) -> AttrCompletion {
170 AttrCompletion { prefer_inner: true, ..self }
176 lookup: Option<&'static str>,
177 snippet: Option<&'static str>,
178 ) -> AttrCompletion {
179 AttrCompletion { label, lookup, snippet, prefer_inner: false }
183 // attributes applicable to all items
184 [@ { item $($tt:tt)* } {$($acc:tt)*}] => {
185 attrs!(@ { $($tt)* } { $($acc)*, "deprecated", "doc", "dochidden", "docalias", "must_use", "no_mangle" })
187 // attributes applicable to all adts
188 [@ { adt $($tt:tt)* } {$($acc:tt)*}] => {
189 attrs!(@ { $($tt)* } { $($acc)*, "derive", "repr" })
191 // attributes applicable to all linkable things aka functions/statics
192 [@ { linkable $($tt:tt)* } {$($acc:tt)*}] => {
193 attrs!(@ { $($tt)* } { $($acc)*, "export_name", "link_name", "link_section" })
195 // error fallback for nicer error message
196 [@ { $ty:ident $($tt:tt)* } {$($acc:tt)*}] => {
197 compile_error!(concat!("unknown attr subtype ", stringify!($ty)))
199 // general push down accumulation
200 [@ { $lit:literal $($tt:tt)*} {$($acc:tt)*}] => {
201 attrs!(@ { $($tt)* } { $($acc)*, $lit })
203 [@ {$($tt:tt)+} {$($tt2:tt)*}] => {
204 compile_error!(concat!("Unexpected input ", stringify!($($tt)+)))
206 // final output construction
207 [@ {} {$($tt:tt)*}] => { &[$($tt)*] as _ };
210 attrs!(@ { $($tt)* } { "allow", "cfg", "cfg_attr", "deny", "forbid", "warn" })
215 static KIND_TO_ATTRIBUTES: Lazy<FxHashMap<SyntaxKind, &[&str]>> = Lazy::new(|| {
222 "crate_name", "feature", "no_implicit_prelude", "no_main", "no_std",
223 "recursion_limit", "type_length_limit", "windows_subsystem"
226 (MODULE, attrs!(item, "macro_use", "no_implicit_prelude", "path")),
227 (ITEM_LIST, attrs!(item, "no_implicit_prelude")),
228 (MACRO_RULES, attrs!(item, "macro_export", "macro_use")),
229 (MACRO_DEF, attrs!(item)),
230 (EXTERN_CRATE, attrs!(item, "macro_use", "no_link")),
232 (TYPE_ALIAS, attrs!(item)),
233 (STRUCT, attrs!(item, adt, "non_exhaustive")),
234 (ENUM, attrs!(item, adt, "non_exhaustive")),
235 (UNION, attrs!(item, adt)),
236 (CONST, attrs!(item)),
241 "cold", "ignore", "inline", "must_use", "panic_handler", "proc_macro",
242 "proc_macro_derive", "proc_macro_attribute", "should_panic", "target_feature",
243 "test", "track_caller"
246 (STATIC, attrs!(item, linkable, "global_allocator", "used")),
247 (TRAIT, attrs!(item, "must_use")),
248 (IMPL, attrs!(item, "automatically_derived")),
249 (ASSOC_ITEM_LIST, attrs!(item)),
250 (EXTERN_BLOCK, attrs!(item, "link")),
251 (EXTERN_ITEM_LIST, attrs!(item, "link")),
252 (MACRO_CALL, attrs!()),
253 (SELF_PARAM, attrs!()),
255 (RECORD_FIELD, attrs!()),
256 (VARIANT, attrs!("non_exhaustive")),
257 (TYPE_PARAM, attrs!()),
258 (CONST_PARAM, attrs!()),
259 (LIFETIME_PARAM, attrs!()),
260 (LET_STMT, attrs!()),
261 (EXPR_STMT, attrs!()),
263 (RECORD_EXPR_FIELD_LIST, attrs!()),
264 (RECORD_EXPR_FIELD, attrs!()),
265 (MATCH_ARM_LIST, attrs!()),
266 (MATCH_ARM, attrs!()),
267 (IDENT_PAT, attrs!()),
268 (RECORD_PAT_FIELD, attrs!()),
273 const EXPR_ATTRIBUTES: &[&str] = attrs!();
275 /// <https://doc.rust-lang.org/reference/attributes.html#built-in-attributes-index>
276 // Keep these sorted for the binary search!
277 const ATTRIBUTES: &[AttrCompletion] = &[
278 attr("allow(…)", Some("allow"), Some("allow(${0:lint})")),
279 attr("automatically_derived", None, None),
280 attr("cfg(…)", Some("cfg"), Some("cfg(${0:predicate})")),
281 attr("cfg_attr(…)", Some("cfg_attr"), Some("cfg_attr(${1:predicate}, ${0:attr})")),
282 attr("cold", None, None),
283 attr(r#"crate_name = """#, Some("crate_name"), Some(r#"crate_name = "${0:crate_name}""#))
285 attr("deny(…)", Some("deny"), Some("deny(${0:lint})")),
286 attr(r#"deprecated"#, Some("deprecated"), Some(r#"deprecated"#)),
287 attr("derive(…)", Some("derive"), Some(r#"derive(${0:Debug})"#)),
288 attr(r#"doc = "…""#, Some("doc"), Some(r#"doc = "${0:docs}""#)),
289 attr(r#"doc(alias = "…")"#, Some("docalias"), Some(r#"doc(alias = "${0:docs}")"#)),
290 attr(r#"doc(hidden)"#, Some("dochidden"), Some(r#"doc(hidden)"#)),
292 r#"export_name = "…""#,
294 Some(r#"export_name = "${0:exported_symbol_name}""#),
296 attr("feature(…)", Some("feature"), Some("feature(${0:flag})")).prefer_inner(),
297 attr("forbid(…)", Some("forbid"), Some("forbid(${0:lint})")),
298 attr("global_allocator", None, None),
299 attr(r#"ignore = "…""#, Some("ignore"), Some(r#"ignore = "${0:reason}""#)),
300 attr("inline", Some("inline"), Some("inline")),
301 attr("link", None, None),
302 attr(r#"link_name = "…""#, Some("link_name"), Some(r#"link_name = "${0:symbol_name}""#)),
304 r#"link_section = "…""#,
305 Some("link_section"),
306 Some(r#"link_section = "${0:section_name}""#),
308 attr("macro_export", None, None),
309 attr("macro_use", None, None),
310 attr(r#"must_use"#, Some("must_use"), Some(r#"must_use"#)),
311 attr("no_implicit_prelude", None, None).prefer_inner(),
312 attr("no_link", None, None).prefer_inner(),
313 attr("no_main", None, None).prefer_inner(),
314 attr("no_mangle", None, None),
315 attr("no_std", None, None).prefer_inner(),
316 attr("non_exhaustive", None, None),
317 attr("panic_handler", None, None),
318 attr(r#"path = "…""#, Some("path"), Some(r#"path ="${0:path}""#)),
319 attr("proc_macro", None, None),
320 attr("proc_macro_attribute", None, None),
321 attr("proc_macro_derive(…)", Some("proc_macro_derive"), Some("proc_macro_derive(${0:Trait})")),
323 r#"recursion_limit = "…""#,
324 Some("recursion_limit"),
325 Some(r#"recursion_limit = "${0:128}""#),
328 attr("repr(…)", Some("repr"), Some("repr(${0:C})")),
329 attr("should_panic", Some("should_panic"), Some(r#"should_panic"#)),
331 r#"target_feature(enable = "…")"#,
332 Some("target_feature"),
333 Some(r#"target_feature(enable = "${0:feature}")"#),
335 attr("test", None, None),
336 attr("track_caller", None, None),
337 attr("type_length_limit = …", Some("type_length_limit"), Some("type_length_limit = ${0:128}"))
339 attr("used", None, None),
340 attr("warn(…)", Some("warn"), Some("warn(${0:lint})")),
342 r#"windows_subsystem = "…""#,
343 Some("windows_subsystem"),
344 Some(r#"windows_subsystem = "${0:subsystem}""#),
349 fn parse_comma_sep_expr(input: ast::TokenTree) -> Option<Vec<ast::Expr>> {
350 let r_paren = input.r_paren_token()?;
353 .children_with_tokens()
355 .take_while(|it| it.as_token() != Some(&r_paren));
356 let input_expressions = tokens.group_by(|tok| tok.kind() == T![,]);
360 .filter_map(|(is_sep, group)| (!is_sep).then(|| group))
361 .filter_map(|mut tokens| syntax::hacks::parse_expr_from_str(&tokens.join("")))
362 .collect::<Vec<ast::Expr>>(),
367 fn attributes_are_sorted() {
368 let mut attrs = ATTRIBUTES.iter().map(|attr| attr.key());
369 let mut prev = attrs.next().unwrap();
371 attrs.for_each(|next| {
374 r#"ATTRIBUTES array is not sorted, "{}" should come after "{}""#,