]> git.proxmox.com Git - rustc.git/blob - src/tools/rust-analyzer/crates/ide-completion/src/completions/attribute.rs
New upstream version 1.64.0+dfsg1
[rustc.git] / src / tools / rust-analyzer / crates / ide-completion / src / 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
5 use ide_db::{
6 generated::lints::{
7 Lint, CLIPPY_LINTS, CLIPPY_LINT_GROUPS, DEFAULT_LINTS, FEATURES, RUSTDOC_LINTS,
8 },
9 syntax_helpers::node_ext::parse_tt_as_comma_sep_paths,
10 FxHashMap, SymbolKind,
11 };
12 use itertools::Itertools;
13 use once_cell::sync::Lazy;
14 use syntax::{
15 ast::{self, AttrKind},
16 AstNode, SyntaxKind, T,
17 };
18
19 use crate::{
20 context::{AttrCtx, CompletionContext, PathCompletionCtx, Qualified},
21 item::CompletionItem,
22 Completions,
23 };
24
25 mod cfg;
26 mod derive;
27 mod lint;
28 mod repr;
29
30 pub(crate) use self::derive::complete_derive_path;
31
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<'_>,
36 &colon_prefix: &bool,
37 fake_attribute_under_caret: &ast::Attr,
38 ) -> Option<()> {
39 let attribute = fake_attribute_under_caret;
40 let name_ref = match attribute.path() {
41 Some(p) => Some(p.as_single_name_ref()?),
42 None => None,
43 };
44 let (path, tt) = name_ref.zip(attribute.token_tree())?;
45 if tt.l_paren_token().is_none() {
46 return None;
47 }
48
49 match path.text().as_str() {
50 "repr" => repr::complete_repr(acc, ctx, tt),
51 "feature" => {
52 lint::complete_lint(acc, ctx, colon_prefix, &parse_tt_as_comma_sep_paths(tt)?, FEATURES)
53 }
54 "allow" | "warn" | "deny" | "forbid" => {
55 let existing_lints = parse_tt_as_comma_sep_paths(tt)?;
56
57 let lints: Vec<Lint> = CLIPPY_LINT_GROUPS
58 .iter()
59 .map(|g| &g.lint)
60 .chain(DEFAULT_LINTS)
61 .chain(CLIPPY_LINTS)
62 .chain(RUSTDOC_LINTS)
63 .cloned()
64 .collect();
65
66 lint::complete_lint(acc, ctx, colon_prefix, &existing_lints, &lints);
67 }
68 "cfg" => cfg::complete_cfg(acc, ctx),
69 _ => (),
70 }
71 Some(())
72 }
73
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,
79 ) {
80 let is_inner = kind == AttrKind::Inner;
81
82 match qualified {
83 Qualified::With {
84 resolution: Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))),
85 super_chain_len,
86 ..
87 } => {
88 acc.add_super_keyword(ctx, *super_chain_len);
89
90 for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
91 match def {
92 hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(ctx.db) => {
93 acc.add_macro(ctx, path_ctx, m, name)
94 }
95 hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
96 acc.add_module(ctx, path_ctx, m, name)
97 }
98 _ => (),
99 }
100 }
101 return;
102 }
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
106 Qualified::No => {
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)
110 }
111 hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
112 acc.add_module(ctx, path_ctx, m, name)
113 }
114 _ => (),
115 });
116 acc.add_nameref_keywords_with_colon(ctx);
117 }
118 Qualified::TypeAnchor { .. } | Qualified::With { .. } => {}
119 }
120
121 let attributes = annotated_item_kind.and_then(|kind| {
122 if ast::Expr::can_cast(kind) {
123 Some(EXPR_ATTRIBUTES)
124 } else {
125 KIND_TO_ATTRIBUTES.get(&kind).copied()
126 }
127 });
128
129 let add_completion = |attr_completion: &AttrCompletion| {
130 let mut item =
131 CompletionItem::new(SymbolKind::Attribute, ctx.source_range(), attr_completion.label);
132
133 if let Some(lookup) = attr_completion.lookup {
134 item.lookup_by(lookup);
135 }
136
137 if let Some((snippet, cap)) = attr_completion.snippet.zip(ctx.config.snippet_cap) {
138 item.insert_snippet(cap, snippet);
139 }
140
141 if is_inner || !attr_completion.prefer_inner {
142 item.add_to(acc);
143 }
144 };
145
146 match attributes {
147 Some(applicable) => applicable
148 .iter()
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),
154 }
155 }
156
157 struct AttrCompletion {
158 label: &'static str,
159 lookup: Option<&'static str>,
160 snippet: Option<&'static str>,
161 prefer_inner: bool,
162 }
163
164 impl AttrCompletion {
165 fn key(&self) -> &'static str {
166 self.lookup.unwrap_or(self.label)
167 }
168
169 const fn prefer_inner(self) -> AttrCompletion {
170 AttrCompletion { prefer_inner: true, ..self }
171 }
172 }
173
174 const fn attr(
175 label: &'static str,
176 lookup: Option<&'static str>,
177 snippet: Option<&'static str>,
178 ) -> AttrCompletion {
179 AttrCompletion { label, lookup, snippet, prefer_inner: false }
180 }
181
182 macro_rules! attrs {
183 // attributes applicable to all items
184 [@ { item $($tt:tt)* } {$($acc:tt)*}] => {
185 attrs!(@ { $($tt)* } { $($acc)*, "deprecated", "doc", "dochidden", "docalias", "must_use", "no_mangle" })
186 };
187 // attributes applicable to all adts
188 [@ { adt $($tt:tt)* } {$($acc:tt)*}] => {
189 attrs!(@ { $($tt)* } { $($acc)*, "derive", "repr" })
190 };
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" })
194 };
195 // error fallback for nicer error message
196 [@ { $ty:ident $($tt:tt)* } {$($acc:tt)*}] => {
197 compile_error!(concat!("unknown attr subtype ", stringify!($ty)))
198 };
199 // general push down accumulation
200 [@ { $lit:literal $($tt:tt)*} {$($acc:tt)*}] => {
201 attrs!(@ { $($tt)* } { $($acc)*, $lit })
202 };
203 [@ {$($tt:tt)+} {$($tt2:tt)*}] => {
204 compile_error!(concat!("Unexpected input ", stringify!($($tt)+)))
205 };
206 // final output construction
207 [@ {} {$($tt:tt)*}] => { &[$($tt)*] as _ };
208 // starting matcher
209 [$($tt:tt),*] => {
210 attrs!(@ { $($tt)* } { "allow", "cfg", "cfg_attr", "deny", "forbid", "warn" })
211 };
212 }
213
214 #[rustfmt::skip]
215 static KIND_TO_ATTRIBUTES: Lazy<FxHashMap<SyntaxKind, &[&str]>> = Lazy::new(|| {
216 use SyntaxKind::*;
217 [
218 (
219 SOURCE_FILE,
220 attrs!(
221 item,
222 "crate_name", "feature", "no_implicit_prelude", "no_main", "no_std",
223 "recursion_limit", "type_length_limit", "windows_subsystem"
224 ),
225 ),
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")),
231 (USE, attrs!(item)),
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)),
237 (
238 FN,
239 attrs!(
240 item, linkable,
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"
244 ),
245 ),
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!()),
254 (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!()),
262 (LITERAL, 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!()),
269 ]
270 .into_iter()
271 .collect()
272 });
273 const EXPR_ATTRIBUTES: &[&str] = attrs!();
274
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}""#))
284 .prefer_inner(),
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)"#)),
291 attr(
292 r#"export_name = """#,
293 Some("export_name"),
294 Some(r#"export_name = "${0:exported_symbol_name}""#),
295 ),
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}""#)),
303 attr(
304 r#"link_section = """#,
305 Some("link_section"),
306 Some(r#"link_section = "${0:section_name}""#),
307 ),
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})")),
322 attr(
323 r#"recursion_limit = """#,
324 Some("recursion_limit"),
325 Some(r#"recursion_limit = "${0:128}""#),
326 )
327 .prefer_inner(),
328 attr("repr(…)", Some("repr"), Some("repr(${0:C})")),
329 attr("should_panic", Some("should_panic"), Some(r#"should_panic"#)),
330 attr(
331 r#"target_feature(enable = "")"#,
332 Some("target_feature"),
333 Some(r#"target_feature(enable = "${0:feature}")"#),
334 ),
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}"))
338 .prefer_inner(),
339 attr("used", None, None),
340 attr("warn(…)", Some("warn"), Some("warn(${0:lint})")),
341 attr(
342 r#"windows_subsystem = """#,
343 Some("windows_subsystem"),
344 Some(r#"windows_subsystem = "${0:subsystem}""#),
345 )
346 .prefer_inner(),
347 ];
348
349 fn parse_comma_sep_expr(input: ast::TokenTree) -> Option<Vec<ast::Expr>> {
350 let r_paren = input.r_paren_token()?;
351 let tokens = input
352 .syntax()
353 .children_with_tokens()
354 .skip(1)
355 .take_while(|it| it.as_token() != Some(&r_paren));
356 let input_expressions = tokens.group_by(|tok| tok.kind() == T![,]);
357 Some(
358 input_expressions
359 .into_iter()
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>>(),
363 )
364 }
365
366 #[test]
367 fn attributes_are_sorted() {
368 let mut attrs = ATTRIBUTES.iter().map(|attr| attr.key());
369 let mut prev = attrs.next().unwrap();
370
371 attrs.for_each(|next| {
372 assert!(
373 prev < next,
374 r#"ATTRIBUTES array is not sorted, "{}" should come after "{}""#,
375 prev,
376 next
377 );
378 prev = next;
379 });
380 }