typstyle_core/pretty/
code_misc.rs

1use typst_syntax::ast::*;
2
3use super::{prelude::*, Context, PrettyPrinter};
4
5impl<'a> PrettyPrinter<'a> {
6    pub(super) fn convert_ident(&'a self, ident: Ident<'a>) -> ArenaDoc<'a> {
7        self.convert_trivia(ident)
8    }
9
10    pub(super) fn convert_array_item(
11        &'a self,
12        ctx: Context,
13        array_item: ArrayItem<'a>,
14    ) -> ArenaDoc<'a> {
15        if let Some(res) = self.check_disabled(array_item.to_untyped()) {
16            return res;
17        }
18        match array_item {
19            ArrayItem::Pos(p) => self.convert_expr(ctx, p),
20            ArrayItem::Spread(s) => self.convert_spread(ctx, s),
21        }
22    }
23
24    pub(super) fn convert_dict_item(
25        &'a self,
26        ctx: Context,
27        dict_item: DictItem<'a>,
28    ) -> ArenaDoc<'a> {
29        if let Some(res) = self.check_disabled(dict_item.to_untyped()) {
30            return res;
31        }
32        match dict_item {
33            DictItem::Named(n) => self.convert_named(ctx, n),
34            DictItem::Keyed(k) => self.convert_keyed(ctx, k),
35            DictItem::Spread(s) => self.convert_spread(ctx, s),
36        }
37    }
38
39    pub(super) fn convert_param(&'a self, ctx: Context, param: Param<'a>) -> ArenaDoc<'a> {
40        if let Some(res) = self.check_disabled(param.to_untyped()) {
41            return res;
42        }
43        match param {
44            Param::Pos(p) => self.convert_pattern(ctx, p),
45            Param::Named(n) => self.convert_named(ctx, n),
46            Param::Spread(s) => self.convert_spread(ctx, s),
47        }
48    }
49
50    pub fn convert_pattern(&'a self, ctx: Context, pattern: Pattern<'a>) -> ArenaDoc<'a> {
51        if let Some(res) = self.check_disabled(pattern.to_untyped()) {
52            return res;
53        }
54        match pattern {
55            Pattern::Normal(n) => self.convert_expr(ctx, n),
56            Pattern::Placeholder(_) => self.convert_literal("_"),
57            Pattern::Destructuring(d) => self.convert_destructuring(ctx, d),
58            Pattern::Parenthesized(p) => self.convert_parenthesized(ctx, p),
59        }
60    }
61
62    pub(super) fn convert_destructuring_item(
63        &'a self,
64        ctx: Context,
65        destructuring_item: DestructuringItem<'a>,
66    ) -> ArenaDoc<'a> {
67        if let Some(res) = self.check_disabled(destructuring_item.to_untyped()) {
68            return res;
69        }
70        match destructuring_item {
71            DestructuringItem::Spread(s) => self.convert_spread(ctx, s),
72            DestructuringItem::Named(n) => self.convert_named(ctx, n),
73            DestructuringItem::Pattern(p) => self.convert_pattern(ctx, p),
74        }
75    }
76}