1use ide_db::SymbolKind;
4use syntax::ast;
5
6use crate::{Completions, context::CompletionContext, item::CompletionItem};
7
8pub(super) fn complete_repr(
9 acc: &mut Completions,
10 ctx: &CompletionContext<'_, '_>,
11 existing_reprs: &[ast::Expr],
12) {
13 for &ReprCompletion { label, snippet, lookup, collides } in REPR_COMPLETIONS {
14 let repr_already_annotated = existing_reprs
15 .iter()
16 .filter_map(|expr| match expr {
17 ast::Expr::PathExpr(path) => path.path()?.as_single_name_ref(),
18 ast::Expr::CallExpr(call) => match call.expr()? {
19 ast::Expr::PathExpr(path) => path.path()?.as_single_name_ref(),
20 _ => None,
21 },
22 _ => None,
23 })
24 .any(|it| {
25 let text = it.text();
26 lookup.unwrap_or(label) == text || collides.contains(&text.as_str())
27 });
28 if repr_already_annotated {
29 continue;
30 }
31
32 let mut item =
33 CompletionItem::new(SymbolKind::BuiltinAttr, ctx.source_range(), label, ctx.edition);
34 if let Some(lookup) = lookup {
35 item.lookup_by(lookup);
36 }
37 if let Some((snippet, cap)) = snippet.zip(ctx.config.snippet_cap) {
38 item.insert_snippet(cap, snippet);
39 }
40 item.add_to(acc, ctx.db);
41 }
42}
43
44struct ReprCompletion {
45 label: &'static str,
46 snippet: Option<&'static str>,
47 lookup: Option<&'static str>,
48 collides: &'static [&'static str],
49}
50
51const fn attr(label: &'static str, collides: &'static [&'static str]) -> ReprCompletion {
52 ReprCompletion { label, snippet: None, lookup: None, collides }
53}
54
55#[rustfmt::skip]
56const REPR_COMPLETIONS: &[ReprCompletion] = &[
57 ReprCompletion { label: "align($0)", snippet: Some("align($0)"), lookup: Some("align"), collides: &["transparent", "packed"] },
58 attr("packed", &["transparent", "align"]),
59 attr("transparent", &["C", "u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "i128", "isize"]),
60 attr("C", &["transparent"]),
61 attr("u8", &["transparent", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "i128", "isize"]),
62 attr("u16", &["transparent", "u8", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "i128", "isize"]),
63 attr("u32", &["transparent", "u8", "u16", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "i128", "isize"]),
64 attr("u64", &["transparent", "u8", "u16", "u32", "u128", "usize", "i8", "i16", "i32", "i64", "i128", "isize"]),
65 attr("u128", &["transparent", "u8", "u16", "u32", "u64", "usize", "i8", "i16", "i32", "i64", "i128", "isize"]),
66 attr("usize", &["transparent", "u8", "u16", "u32", "u64", "u128", "i8", "i16", "i32", "i64", "i128", "isize"]),
67 attr("i8", &["transparent", "u8", "u16", "u32", "u64", "u128", "usize", "i16", "i32", "i64", "i128", "isize"]),
68 attr("i16", &["transparent", "u8", "u16", "u32", "u64", "u128", "usize", "i8", "i32", "i64", "i128", "isize"]),
69 attr("i32", &["transparent", "u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i64", "i128", "isize"]),
70 attr("i64", &["transparent", "u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i128", "isize"]),
71 attr("i28", &["transparent", "u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "isize"]),
72 attr("isize", &["transparent", "u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "i128"]),
73];