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
177
178
179
180
181
182
use crate::{
config::{items::ItemBraceStyle, user_def::FieldAlignment},
formatter::{
shape::{ExprKind, LineStyle},
*,
},
utils::{
map::byte_span::{ByteSpan, LeafSpans},
CurlyBrace,
},
};
use std::fmt::Write;
use sway_ast::{
token::{Delimiter, PunctKind},
ItemEnum,
};
use sway_types::Spanned;
#[cfg(test)]
mod tests;
impl Format for ItemEnum {
fn format(
&self,
formatted_code: &mut FormattedCode,
formatter: &mut Formatter,
) -> Result<(), FormatterError> {
formatter.with_shape(
formatter
.shape
.with_code_line_from(LineStyle::Multiline, ExprKind::default()),
|formatter| -> Result<(), FormatterError> {
if let Some(visibility) = &self.visibility {
write!(formatted_code, "{} ", visibility.span().as_str())?;
}
write!(formatted_code, "{} ", self.enum_token.span().as_str())?;
self.name.format(formatted_code, formatter)?;
if let Some(generics) = &self.generics {
generics.format(formatted_code, formatter)?;
}
let fields = self.fields.get();
Self::open_curly_brace(formatted_code, formatter)?;
match formatter.config.structures.field_alignment {
FieldAlignment::AlignFields(enum_variant_align_threshold) => {
writeln!(formatted_code)?;
let value_pairs = &fields
.value_separator_pairs
.iter()
.map(|pair| (&pair.0.value, &pair.1))
.collect::<Vec<_>>();
let variant_length: Vec<usize> = value_pairs
.iter()
.map(|(type_field, _)| type_field.name.as_str().len())
.collect();
let mut max_valid_variant_length = 0;
variant_length.iter().for_each(|length| {
if *length > max_valid_variant_length
&& *length < enum_variant_align_threshold
{
max_valid_variant_length = *length;
}
});
let value_pairs_iter = value_pairs.iter().enumerate();
for (var_index, (type_field, comma_token)) in value_pairs_iter.clone() {
write!(
formatted_code,
"{}",
&formatter.shape.indent.to_string(&formatter.config)?
)?;
type_field.name.format(formatted_code, formatter)?;
let current_variant_length = variant_length[var_index];
if current_variant_length < max_valid_variant_length {
let mut required_alignment =
max_valid_variant_length - current_variant_length;
while required_alignment != 0 {
write!(formatted_code, " ")?;
required_alignment -= 1;
}
}
write!(
formatted_code,
" {} ",
type_field.colon_token.span().as_str(),
)?;
type_field.ty.format(formatted_code, formatter)?;
writeln!(formatted_code, "{}", comma_token.span().as_str())?;
}
if let Some(final_value) = &fields.final_value_opt {
let final_value = &final_value.value;
writeln!(
formatted_code,
"{}{}",
final_value.span().as_str(),
PunctKind::Comma.as_char(),
)?;
}
}
FieldAlignment::Off => fields.format(formatted_code, formatter)?,
}
Self::close_curly_brace(formatted_code, formatter)?;
Ok(())
},
)?;
Ok(())
}
}
impl CurlyBrace for ItemEnum {
fn open_curly_brace(
line: &mut String,
formatter: &mut Formatter,
) -> Result<(), FormatterError> {
let brace_style = formatter.config.items.item_brace_style;
let open_brace = Delimiter::Brace.as_open_char();
match brace_style {
ItemBraceStyle::AlwaysNextLine => {
writeln!(line, "\n{}", open_brace)?;
formatter.shape.block_indent(&formatter.config);
}
_ => {
write!(line, " {}", open_brace)?;
formatter.shape.block_indent(&formatter.config);
}
}
Ok(())
}
fn close_curly_brace(
line: &mut String,
formatter: &mut Formatter,
) -> Result<(), FormatterError> {
formatter.shape.block_unindent(&formatter.config);
write!(
line,
"{}{}",
formatter.shape.indent.to_string(&formatter.config)?,
Delimiter::Brace.as_close_char()
)?;
Ok(())
}
}
impl LeafSpans for ItemEnum {
fn leaf_spans(&self) -> Vec<ByteSpan> {
let mut collected_spans = Vec::new();
if let Some(visibility) = &self.visibility {
collected_spans.push(ByteSpan::from(visibility.span()));
}
collected_spans.push(ByteSpan::from(self.enum_token.span()));
collected_spans.push(ByteSpan::from(self.name.span()));
if let Some(generics) = &self.generics {
collected_spans.push(ByteSpan::from(generics.parameters.span()))
}
collected_spans.append(&mut self.fields.leaf_spans());
collected_spans
}
}