Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions sway-fmt-v2/src/config/heuristics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ pub struct WidthHeuristics {
// Maximum width of the args of a function-like attributes before falling
// back to vertical formatting.
pub(crate) attr_fn_like_width: usize,
// Maximum width in the body of a struct lit before falling back to
// Maximum width in the body of a user-defined structure literal before falling back to
// vertical formatting.
pub(crate) struct_lit_width: usize,
// Maximum width in the body of a struct variant before falling back
pub(crate) structure_lit_width: usize,
// Maximum width in the body of a user-defined structure field before falling back
// to vertical formatting.
pub(crate) struct_variant_width: usize,
pub(crate) structure_field_width: usize,
// Maximum width of an array literal before falling back to vertical
// formatting.
pub(crate) array_width: usize,
Expand All @@ -97,8 +97,8 @@ impl WidthHeuristics {
WidthHeuristics {
fn_call_width: usize::max_value(),
attr_fn_like_width: usize::max_value(),
struct_lit_width: 0,
struct_variant_width: 0,
structure_lit_width: 0,
structure_field_width: 0,
array_width: usize::max_value(),
chain_width: usize::max_value(),
single_line_if_else_max_width: 0,
Expand All @@ -109,8 +109,8 @@ impl WidthHeuristics {
WidthHeuristics {
fn_call_width: max_width,
attr_fn_like_width: max_width,
struct_lit_width: max_width,
struct_variant_width: max_width,
structure_lit_width: max_width,
structure_field_width: max_width,
array_width: max_width,
chain_width: max_width,
single_line_if_else_max_width: max_width,
Expand All @@ -131,8 +131,9 @@ impl WidthHeuristics {
fn_call_width: (DEFAULT_FN_CALL_WIDTH as f32 * max_width_ratio).round() as usize,
attr_fn_like_width: (DEFAULT_ATTR_FN_LIKE_WIDTH as f32 * max_width_ratio).round()
as usize,
struct_lit_width: (DEFAULT_STRUCT_LIT_WIDTH as f32 * max_width_ratio).round() as usize,
struct_variant_width: (DEFAULT_STRUCT_VAR_WIDTH as f32 * max_width_ratio).round()
structure_lit_width: (DEFAULT_STRUCT_LIT_WIDTH as f32 * max_width_ratio).round()
as usize,
structure_field_width: (DEFAULT_STRUCT_VAR_WIDTH as f32 * max_width_ratio).round()
as usize,
array_width: (DEFAULT_ARRAY_WIDTH as f32 * max_width_ratio).round() as usize,
chain_width: (DEFAULT_CHAIN_WIDTH as f32 * max_width_ratio).round() as usize,
Expand Down
44 changes: 17 additions & 27 deletions sway-fmt-v2/src/config/user_def.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
//! Configuration options related to formatting user-defined structures.

use crate::constants::{
DEFAULT_ENUM_VARIANT_ALIGN_THRESHOLD, DEFAULT_STORAGE_FIELD_ALIGN_THRESHOLD,
DEFAULT_STRUCT_FIELD_ALIGN_THRESHOLD,
};
use serde::{Deserialize, Serialize};

use super::user_opts::StructuresOptions;

/// Styling preferences for user-defined structures like `struct`s or `enum`s.
#[derive(Debug)]
pub struct Structures {
/// Align enum variants discrims, if their diffs fit within threshold.
pub enum_variant_align_threshold: usize,
/// Align struct fields if their diffs fits within threshold.
pub struct_field_align_threshold: usize,
/// Align storage fields if their diffs fit within the threshold.
pub storage_field_align_threshold: usize,
/// Put small struct literals on a single line.
pub struct_lit_single_line: bool,
/// Align fields of user-defined structures if their diffs fit within threshold.
pub field_alignment: FieldAlignment,
/// Put small user-defined structure literals on a single line.
pub small_structures_single_line: bool,
}

impl Default for Structures {
fn default() -> Self {
Self {
enum_variant_align_threshold: DEFAULT_ENUM_VARIANT_ALIGN_THRESHOLD,
struct_field_align_threshold: DEFAULT_STRUCT_FIELD_ALIGN_THRESHOLD,
storage_field_align_threshold: DEFAULT_STORAGE_FIELD_ALIGN_THRESHOLD,
struct_lit_single_line: true,
field_alignment: FieldAlignment::Off,
small_structures_single_line: true,
}
}
}
Expand All @@ -35,18 +26,17 @@ impl Structures {
pub fn from_opts(opts: &StructuresOptions) -> Self {
let default = Self::default();
Self {
enum_variant_align_threshold: opts
.enum_variant_align_threshold
.unwrap_or(default.enum_variant_align_threshold),
struct_field_align_threshold: opts
.struct_field_align_threshold
.unwrap_or(default.struct_field_align_threshold),
storage_field_align_threshold: opts
.storage_field_align_threshold
.unwrap_or(default.storage_field_align_threshold),
struct_lit_single_line: opts
field_alignment: opts.field_alignment.unwrap_or(default.field_alignment),
small_structures_single_line: opts
.struct_lit_single_line
.unwrap_or(default.struct_lit_single_line),
.unwrap_or(default.small_structures_single_line),
}
}
}

/// Align fields if they fit within a provided threshold.
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub enum FieldAlignment {
AlignFields(usize),
Off,
}
5 changes: 2 additions & 3 deletions sway-fmt-v2/src/config/user_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use super::{
items::{ItemBraceStyle, ItemsLayout},
lists::{ListTactic, SeparatorTactic},
literals::HexLiteralCase,
user_def::FieldAlignment,
whitespace::{IndentStyle, NewlineStyle},
};
/// See parent struct [Whitespace].
Expand Down Expand Up @@ -77,10 +78,8 @@ pub struct HeuristicsOptions {
/// See parent struct [Structures].
#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
pub struct StructuresOptions {
pub enum_variant_align_threshold: Option<usize>,
pub struct_field_align_threshold: Option<usize>,
pub field_alignment: Option<FieldAlignment>,
pub struct_lit_single_line: Option<bool>,
pub storage_field_align_threshold: Option<usize>,
}
/// See parent struct [Comments].
#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
Expand Down
9 changes: 0 additions & 9 deletions sway-fmt-v2/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ pub const DEFAULT_BLANK_LINES_LOWER_BOUND: usize = 0;
/// Write an items and its attribute on the same line if their combined width is below a threshold.
pub const DEFAULT_INLINE_ATTR_WIDTH: usize = 0;

/////USER_DEFINED_STRUCTURES/////

/// Default max threshold for aligning struct fields.
pub const DEFAULT_STRUCT_FIELD_ALIGN_THRESHOLD: usize = 0;
/// Default max threshold for aligning enum variants.
pub const DEFAULT_ENUM_VARIANT_ALIGN_THRESHOLD: usize = 0;
/// Default max threshold for aligning storage fields.
pub const DEFAULT_STORAGE_FIELD_ALIGN_THRESHOLD: usize = 0;

/////COMMENTS/////

/// Default max length of comments.
Expand Down
62 changes: 30 additions & 32 deletions sway-fmt-v2/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Formatter {
#[cfg(test)]
mod tests {
use super::{Config, Formatter};
use crate::utils::indent_style::Shape;
use crate::{config::user_def::FieldAlignment, utils::indent_style::Shape};
use std::sync::Arc;

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

#[test]
fn test_struct_single_line_alignment() {
fn test_struct_multiline_line_alignment() {
let sway_code_to_format = r#"contract;
pub struct Foo {
bar: u64,
baz: bool,
pub struct Foo<T, P> {
barbazfoo: u64,
baz : bool,
}
"#;
let correct_sway_code = r#"contract;

pub struct Foo { bar: u64, baz: bool }"#;
pub struct Foo<T, P> {
barbazfoo : u64,
baz : bool,
}"#;
let mut config = Config::default();
config.structures.struct_lit_single_line = true;
config.structures.struct_field_align_threshold = 40;
config.whitespace.max_width = 300;
config.structures.field_alignment = FieldAlignment::AlignFields(40);
let mut formatter = get_formatter(config, Shape::default());
let formatted_sway_code =
Formatter::format(&mut formatter, Arc::from(sway_code_to_format), None).unwrap();
assert!(correct_sway_code == formatted_sway_code)
}
#[test]
fn test_struct_multiline_line_alignment() {
fn test_struct_single_line() {
let sway_code_to_format = r#"contract;
pub struct Foo<T, P> {
barbazfoo: u64,
baz : bool,
pub struct Foo {
bar: u64,
baz: bool,
}
"#;
let correct_sway_code = r#"contract;

pub struct Foo<T, P> {
barbazfoo: u64,
baz : bool,
}"#;
pub struct Foo { bar: u64, baz: bool }"#;
let mut config = Config::default();
config.structures.struct_field_align_threshold = 40;
config.structures.small_structures_single_line = true;
config.whitespace.max_width = 300;
let mut formatter = get_formatter(config, Shape::default());
let formatted_sway_code =
Formatter::format(&mut formatter, Arc::from(sway_code_to_format), None).unwrap();
assert!(correct_sway_code == formatted_sway_code)
}
#[test]
fn test_struct_single_line() {
fn test_enum_single_line() {
let sway_code_to_format = r#"contract;
pub struct Foo {
pub enum Foo {
bar: u64,
baz: bool,
}
"#;
let correct_sway_code = r#"contract;

pub struct Foo { bar: u64, baz: bool }"#;
pub enum Foo { bar: u64, baz: bool }"#;
let mut config = Config::default();
config.structures.struct_lit_single_line = true;
config.structures.small_structures_single_line = true;
config.whitespace.max_width = 300;
let mut formatter = get_formatter(config, Shape::default());
let formatted_sway_code =
Formatter::format(&mut formatter, Arc::from(sway_code_to_format), None).unwrap();
assert!(correct_sway_code == formatted_sway_code)
}

#[test]
fn test_struct_multi_line() {
let sway_code_to_format = r#"contract;
pub struct Foo {
bar: u64,
baz: bool,
baz: bool
}
"#;
let correct_sway_code = r#"contract;
Expand All @@ -190,16 +188,16 @@ enum Color {
Blue: (), Green: (),
Red: (),
Silver: (),
Grey: (), }
Grey: () }
"#;
let correct_sway_code = r#"contract;

enum Color {
Blue : (),
Green : (),
Red : (),
Silver : (),
Grey : (),
Blue: (),
Green: (),
Red: (),
Silver: (),
Grey: (),
}"#;
let mut formatter = Formatter::default();
let formatted_sway_code =
Expand Down Expand Up @@ -228,14 +226,14 @@ enum Color {

// Creating a config with enum_variant_align_threshold that exceeds longest variant length
let mut formatter = Formatter::default();
formatter.config.structures.enum_variant_align_threshold = 20;
formatter.config.structures.field_alignment = FieldAlignment::AlignFields(20);

let formatted_sway_code =
Formatter::format(&mut formatter, Arc::from(sway_code_to_format), None).unwrap();
assert!(correct_sway_code == formatted_sway_code)
}
#[test]
fn test_item_abi() {
fn test_item_abi_with_generics_and_attributes() {
let sway_code_to_format = r#"contract;

abi StorageMapExample {
Expand Down
Loading