1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use crate::{
    formatter::{shape::LineStyle, *},
    utils::{
        map::byte_span::{ByteSpan, LeafSpans},
        {Parenthesis, SquareBracket},
    },
};
use std::fmt::Write;
use sway_ast::{ExprArrayDescriptor, ExprTupleDescriptor};
use sway_types::{ast::Delimiter, Spanned};

impl Format for ExprTupleDescriptor {
    fn format(
        &self,
        formatted_code: &mut FormattedCode,
        formatter: &mut Formatter,
    ) -> Result<(), FormatterError> {
        Self::open_parenthesis(formatted_code, formatter)?;
        match self {
            Self::Nil => {}
            Self::Cons {
                head,
                comma_token,
                tail,
            } => match formatter.shape.code_line.line_style {
                LineStyle::Multiline => {
                    write!(formatted_code, "{}", formatter.indent_to_str()?)?;
                    head.format(formatted_code, formatter)?;
                    write!(formatted_code, "{}", comma_token.span().as_str())?;
                    tail.format(formatted_code, formatter)?;
                }
                _ => {
                    head.format(formatted_code, formatter)?;
                    write!(formatted_code, "{} ", comma_token.span().as_str())?;
                    tail.format(formatted_code, formatter)?;
                }
            },
        }
        Self::close_parenthesis(formatted_code, formatter)?;

        Ok(())
    }
}

impl Parenthesis for ExprTupleDescriptor {
    fn open_parenthesis(
        line: &mut FormattedCode,
        formatter: &mut Formatter,
    ) -> Result<(), FormatterError> {
        match formatter.shape.code_line.line_style {
            LineStyle::Multiline => {
                formatter.indent();
                writeln!(line, "{}", Delimiter::Parenthesis.as_open_char())?;
            }
            _ => write!(line, "{}", Delimiter::Parenthesis.as_open_char())?,
        }

        Ok(())
    }
    fn close_parenthesis(
        line: &mut FormattedCode,
        formatter: &mut Formatter,
    ) -> Result<(), FormatterError> {
        match formatter.shape.code_line.line_style {
            LineStyle::Multiline => {
                formatter.unindent();
                write!(
                    line,
                    "{}{}",
                    formatter.indent_to_str()?,
                    Delimiter::Parenthesis.as_close_char()
                )?;
            }
            _ => write!(line, "{}", Delimiter::Parenthesis.as_close_char())?,
        }

        Ok(())
    }
}

impl Format for ExprArrayDescriptor {
    fn format(
        &self,
        formatted_code: &mut FormattedCode,
        formatter: &mut Formatter,
    ) -> Result<(), FormatterError> {
        Self::open_square_bracket(formatted_code, formatter)?;
        match self {
            Self::Sequence(punct_expr) => {
                punct_expr.format(formatted_code, formatter)?;
            }
            Self::Repeat {
                value,
                semicolon_token,
                length,
            } => {
                value.format(formatted_code, formatter)?;
                write!(formatted_code, "{} ", semicolon_token.span().as_str())?;
                length.format(formatted_code, formatter)?;
            }
        }
        Self::close_square_bracket(formatted_code, formatter)?;

        Ok(())
    }
}

impl SquareBracket for ExprArrayDescriptor {
    fn open_square_bracket(
        line: &mut FormattedCode,
        formatter: &mut Formatter,
    ) -> Result<(), FormatterError> {
        match formatter.shape.code_line.line_style {
            LineStyle::Multiline => {
                formatter.indent();
                write!(line, "{}", Delimiter::Bracket.as_open_char())?;
            }
            _ => write!(line, "{}", Delimiter::Bracket.as_open_char())?,
        }

        Ok(())
    }
    fn close_square_bracket(
        line: &mut FormattedCode,
        formatter: &mut Formatter,
    ) -> Result<(), FormatterError> {
        match formatter.shape.code_line.line_style {
            LineStyle::Multiline => {
                formatter.unindent();
                write!(
                    line,
                    "{}{}",
                    formatter.indent_to_str()?,
                    Delimiter::Bracket.as_close_char()
                )?;
            }
            _ => write!(line, "{}", Delimiter::Bracket.as_close_char())?,
        }

        Ok(())
    }
}

impl LeafSpans for ExprTupleDescriptor {
    fn leaf_spans(&self) -> Vec<ByteSpan> {
        let mut collected_spans = Vec::new();
        if let ExprTupleDescriptor::Cons {
            head,
            comma_token,
            tail,
        } = self
        {
            collected_spans.append(&mut head.leaf_spans());
            collected_spans.push(ByteSpan::from(comma_token.span()));
            collected_spans.append(&mut tail.leaf_spans());
        }
        collected_spans
    }
}

impl LeafSpans for ExprArrayDescriptor {
    fn leaf_spans(&self) -> Vec<ByteSpan> {
        let mut collected_spans = Vec::new();
        if let ExprArrayDescriptor::Repeat {
            value,
            semicolon_token,
            length,
        } = self
        {
            collected_spans.append(&mut value.leaf_spans());
            collected_spans.push(ByteSpan::from(semicolon_token.span()));
            collected_spans.append(&mut length.leaf_spans());
        }
        collected_spans
    }
}