Skip to content

Commit 15cc8f8

Browse files
authored
Updates user_def with FieldAlignment & fix corresponding use cases (#2153)
* update user_def with AlignFields & fix corresponding use cases * rmv unused consts * update doc comments in ItemEnum
1 parent 92ec0a6 commit 15cc8f8

File tree

8 files changed

+339
-216
lines changed

8 files changed

+339
-216
lines changed

sway-fmt-v2/src/config/heuristics.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ pub struct WidthHeuristics {
6969
// Maximum width of the args of a function-like attributes before falling
7070
// back to vertical formatting.
7171
pub(crate) attr_fn_like_width: usize,
72-
// Maximum width in the body of a struct lit before falling back to
72+
// Maximum width in the body of a user-defined structure literal before falling back to
7373
// vertical formatting.
74-
pub(crate) struct_lit_width: usize,
75-
// Maximum width in the body of a struct variant before falling back
74+
pub(crate) structure_lit_width: usize,
75+
// Maximum width in the body of a user-defined structure field before falling back
7676
// to vertical formatting.
77-
pub(crate) struct_variant_width: usize,
77+
pub(crate) structure_field_width: usize,
7878
// Maximum width of an array literal before falling back to vertical
7979
// formatting.
8080
pub(crate) array_width: usize,
@@ -97,8 +97,8 @@ impl WidthHeuristics {
9797
WidthHeuristics {
9898
fn_call_width: usize::max_value(),
9999
attr_fn_like_width: usize::max_value(),
100-
struct_lit_width: 0,
101-
struct_variant_width: 0,
100+
structure_lit_width: 0,
101+
structure_field_width: 0,
102102
array_width: usize::max_value(),
103103
chain_width: usize::max_value(),
104104
single_line_if_else_max_width: 0,
@@ -109,8 +109,8 @@ impl WidthHeuristics {
109109
WidthHeuristics {
110110
fn_call_width: max_width,
111111
attr_fn_like_width: max_width,
112-
struct_lit_width: max_width,
113-
struct_variant_width: max_width,
112+
structure_lit_width: max_width,
113+
structure_field_width: max_width,
114114
array_width: max_width,
115115
chain_width: max_width,
116116
single_line_if_else_max_width: max_width,
@@ -131,8 +131,9 @@ impl WidthHeuristics {
131131
fn_call_width: (DEFAULT_FN_CALL_WIDTH as f32 * max_width_ratio).round() as usize,
132132
attr_fn_like_width: (DEFAULT_ATTR_FN_LIKE_WIDTH as f32 * max_width_ratio).round()
133133
as usize,
134-
struct_lit_width: (DEFAULT_STRUCT_LIT_WIDTH as f32 * max_width_ratio).round() as usize,
135-
struct_variant_width: (DEFAULT_STRUCT_VAR_WIDTH as f32 * max_width_ratio).round()
134+
structure_lit_width: (DEFAULT_STRUCT_LIT_WIDTH as f32 * max_width_ratio).round()
135+
as usize,
136+
structure_field_width: (DEFAULT_STRUCT_VAR_WIDTH as f32 * max_width_ratio).round()
136137
as usize,
137138
array_width: (DEFAULT_ARRAY_WIDTH as f32 * max_width_ratio).round() as usize,
138139
chain_width: (DEFAULT_CHAIN_WIDTH as f32 * max_width_ratio).round() as usize,

sway-fmt-v2/src/config/user_def.rs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
11
//! Configuration options related to formatting user-defined structures.
22
3-
use crate::constants::{
4-
DEFAULT_ENUM_VARIANT_ALIGN_THRESHOLD, DEFAULT_STORAGE_FIELD_ALIGN_THRESHOLD,
5-
DEFAULT_STRUCT_FIELD_ALIGN_THRESHOLD,
6-
};
3+
use serde::{Deserialize, Serialize};
74

85
use super::user_opts::StructuresOptions;
96

107
/// Styling preferences for user-defined structures like `struct`s or `enum`s.
118
#[derive(Debug)]
129
pub struct Structures {
13-
/// Align enum variants discrims, if their diffs fit within threshold.
14-
pub enum_variant_align_threshold: usize,
15-
/// Align struct fields if their diffs fits within threshold.
16-
pub struct_field_align_threshold: usize,
17-
/// Align storage fields if their diffs fit within the threshold.
18-
pub storage_field_align_threshold: usize,
19-
/// Put small struct literals on a single line.
20-
pub struct_lit_single_line: bool,
10+
/// Align fields of user-defined structures if their diffs fit within threshold.
11+
pub field_alignment: FieldAlignment,
12+
/// Put small user-defined structure literals on a single line.
13+
pub small_structures_single_line: bool,
2114
}
2215

2316
impl Default for Structures {
2417
fn default() -> Self {
2518
Self {
26-
enum_variant_align_threshold: DEFAULT_ENUM_VARIANT_ALIGN_THRESHOLD,
27-
struct_field_align_threshold: DEFAULT_STRUCT_FIELD_ALIGN_THRESHOLD,
28-
storage_field_align_threshold: DEFAULT_STORAGE_FIELD_ALIGN_THRESHOLD,
29-
struct_lit_single_line: true,
19+
field_alignment: FieldAlignment::Off,
20+
small_structures_single_line: true,
3021
}
3122
}
3223
}
@@ -35,18 +26,17 @@ impl Structures {
3526
pub fn from_opts(opts: &StructuresOptions) -> Self {
3627
let default = Self::default();
3728
Self {
38-
enum_variant_align_threshold: opts
39-
.enum_variant_align_threshold
40-
.unwrap_or(default.enum_variant_align_threshold),
41-
struct_field_align_threshold: opts
42-
.struct_field_align_threshold
43-
.unwrap_or(default.struct_field_align_threshold),
44-
storage_field_align_threshold: opts
45-
.storage_field_align_threshold
46-
.unwrap_or(default.storage_field_align_threshold),
47-
struct_lit_single_line: opts
29+
field_alignment: opts.field_alignment.unwrap_or(default.field_alignment),
30+
small_structures_single_line: opts
4831
.struct_lit_single_line
49-
.unwrap_or(default.struct_lit_single_line),
32+
.unwrap_or(default.small_structures_single_line),
5033
}
5134
}
5235
}
36+
37+
/// Align fields if they fit within a provided threshold.
38+
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
39+
pub enum FieldAlignment {
40+
AlignFields(usize),
41+
Off,
42+
}

sway-fmt-v2/src/config/user_opts.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use super::{
88
items::{ItemBraceStyle, ItemsLayout},
99
lists::{ListTactic, SeparatorTactic},
1010
literals::HexLiteralCase,
11+
user_def::FieldAlignment,
1112
whitespace::{IndentStyle, NewlineStyle},
1213
};
1314
/// See parent struct [Whitespace].
@@ -77,10 +78,8 @@ pub struct HeuristicsOptions {
7778
/// See parent struct [Structures].
7879
#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
7980
pub struct StructuresOptions {
80-
pub enum_variant_align_threshold: Option<usize>,
81-
pub struct_field_align_threshold: Option<usize>,
81+
pub field_alignment: Option<FieldAlignment>,
8282
pub struct_lit_single_line: Option<bool>,
83-
pub storage_field_align_threshold: Option<usize>,
8483
}
8584
/// See parent struct [Comments].
8685
#[derive(Serialize, Deserialize, Debug, Copy, Clone)]

sway-fmt-v2/src/constants.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,6 @@ pub const DEFAULT_BLANK_LINES_LOWER_BOUND: usize = 0;
3636
/// Write an items and its attribute on the same line if their combined width is below a threshold.
3737
pub const DEFAULT_INLINE_ATTR_WIDTH: usize = 0;
3838

39-
/////USER_DEFINED_STRUCTURES/////
40-
41-
/// Default max threshold for aligning struct fields.
42-
pub const DEFAULT_STRUCT_FIELD_ALIGN_THRESHOLD: usize = 0;
43-
/// Default max threshold for aligning enum variants.
44-
pub const DEFAULT_ENUM_VARIANT_ALIGN_THRESHOLD: usize = 0;
45-
/// Default max threshold for aligning storage fields.
46-
pub const DEFAULT_STORAGE_FIELD_ALIGN_THRESHOLD: usize = 0;
47-
4839
/////COMMENTS/////
4940

5041
/// Default max length of comments.

sway-fmt-v2/src/fmt.rs

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl Formatter {
8181
#[cfg(test)]
8282
mod tests {
8383
use super::{Config, Formatter};
84-
use crate::utils::indent_style::Shape;
84+
use crate::{config::user_def::FieldAlignment, utils::indent_style::Shape};
8585
use std::sync::Arc;
8686

8787
fn get_formatter(config: Config, shape: Shape) -> Formatter {
@@ -102,72 +102,70 @@ pub const TEST: u16 = 10;"#;
102102
}
103103

104104
#[test]
105-
fn test_struct_single_line_alignment() {
105+
fn test_struct_multiline_line_alignment() {
106106
let sway_code_to_format = r#"contract;
107-
pub struct Foo {
108-
bar: u64,
109-
baz: bool,
107+
pub struct Foo<T, P> {
108+
barbazfoo: u64,
109+
baz : bool,
110110
}
111111
"#;
112112
let correct_sway_code = r#"contract;
113113
114-
pub struct Foo { bar: u64, baz: bool }"#;
114+
pub struct Foo<T, P> {
115+
barbazfoo : u64,
116+
baz : bool,
117+
}"#;
115118
let mut config = Config::default();
116-
config.structures.struct_lit_single_line = true;
117-
config.structures.struct_field_align_threshold = 40;
118-
config.whitespace.max_width = 300;
119+
config.structures.field_alignment = FieldAlignment::AlignFields(40);
119120
let mut formatter = get_formatter(config, Shape::default());
120121
let formatted_sway_code =
121122
Formatter::format(&mut formatter, Arc::from(sway_code_to_format), None).unwrap();
122123
assert!(correct_sway_code == formatted_sway_code)
123124
}
124125
#[test]
125-
fn test_struct_multiline_line_alignment() {
126+
fn test_struct_single_line() {
126127
let sway_code_to_format = r#"contract;
127-
pub struct Foo<T, P> {
128-
barbazfoo: u64,
129-
baz : bool,
128+
pub struct Foo {
129+
bar: u64,
130+
baz: bool,
130131
}
131132
"#;
132133
let correct_sway_code = r#"contract;
133134
134-
pub struct Foo<T, P> {
135-
barbazfoo: u64,
136-
baz : bool,
137-
}"#;
135+
pub struct Foo { bar: u64, baz: bool }"#;
138136
let mut config = Config::default();
139-
config.structures.struct_field_align_threshold = 40;
137+
config.structures.small_structures_single_line = true;
138+
config.whitespace.max_width = 300;
140139
let mut formatter = get_formatter(config, Shape::default());
141140
let formatted_sway_code =
142141
Formatter::format(&mut formatter, Arc::from(sway_code_to_format), None).unwrap();
143142
assert!(correct_sway_code == formatted_sway_code)
144143
}
145144
#[test]
146-
fn test_struct_single_line() {
145+
fn test_enum_single_line() {
147146
let sway_code_to_format = r#"contract;
148-
pub struct Foo {
147+
pub enum Foo {
149148
bar: u64,
150149
baz: bool,
151150
}
152151
"#;
153152
let correct_sway_code = r#"contract;
154153
155-
pub struct Foo { bar: u64, baz: bool }"#;
154+
pub enum Foo { bar: u64, baz: bool }"#;
156155
let mut config = Config::default();
157-
config.structures.struct_lit_single_line = true;
156+
config.structures.small_structures_single_line = true;
158157
config.whitespace.max_width = 300;
159158
let mut formatter = get_formatter(config, Shape::default());
160159
let formatted_sway_code =
161160
Formatter::format(&mut formatter, Arc::from(sway_code_to_format), None).unwrap();
162161
assert!(correct_sway_code == formatted_sway_code)
163162
}
164-
165163
#[test]
166164
fn test_struct_multi_line() {
167165
let sway_code_to_format = r#"contract;
168166
pub struct Foo {
169167
bar: u64,
170-
baz: bool,
168+
baz: bool
171169
}
172170
"#;
173171
let correct_sway_code = r#"contract;
@@ -190,16 +188,16 @@ enum Color {
190188
Blue: (), Green: (),
191189
Red: (),
192190
Silver: (),
193-
Grey: (), }
191+
Grey: () }
194192
"#;
195193
let correct_sway_code = r#"contract;
196194
197195
enum Color {
198-
Blue : (),
199-
Green : (),
200-
Red : (),
201-
Silver : (),
202-
Grey : (),
196+
Blue: (),
197+
Green: (),
198+
Red: (),
199+
Silver: (),
200+
Grey: (),
203201
}"#;
204202
let mut formatter = Formatter::default();
205203
let formatted_sway_code =
@@ -228,14 +226,14 @@ enum Color {
228226

229227
// Creating a config with enum_variant_align_threshold that exceeds longest variant length
230228
let mut formatter = Formatter::default();
231-
formatter.config.structures.enum_variant_align_threshold = 20;
229+
formatter.config.structures.field_alignment = FieldAlignment::AlignFields(20);
232230

233231
let formatted_sway_code =
234232
Formatter::format(&mut formatter, Arc::from(sway_code_to_format), None).unwrap();
235233
assert!(correct_sway_code == formatted_sway_code)
236234
}
237235
#[test]
238-
fn test_item_abi() {
236+
fn test_item_abi_with_generics_and_attributes() {
239237
let sway_code_to_format = r#"contract;
240238
241239
abi StorageMapExample {

0 commit comments

Comments
 (0)