Skip to content

Commit

Permalink
Fix struct indentation
Browse files Browse the repository at this point in the history
Fix struct properties indentation
  • Loading branch information
cr-fuel committed Oct 22, 2023
1 parent 3427dbc commit 39cccee
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 20 deletions.
4 changes: 3 additions & 1 deletion examples/enums/src/enum_of_structs.sw
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ enum MyEnum {

fn main() {
let my_enum = MyEnum::Item(Item {
price: 5, amount: 2, id: 42,
price: 5,
amount: 2,
id: 42,
});
}
6 changes: 4 additions & 2 deletions examples/structs/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use data_structures::{Foo, Line, Point, TupleInStruct};
fn hardcoded_instantiation() -> Foo {
// Instantiate `foo` as `Foo`
let mut foo = Foo {
bar: 42, baz: false,
bar: 42,
baz: false,
};

// Access and write to "baz"
Expand Down Expand Up @@ -59,7 +60,8 @@ fn struct_destructuring() {
let Point { x, .. } = point2;

let line = Line {
p1: point1, p2: point2,
p1: point1,
p2: point2,
};
// Destructure the values from the nested structs into variables
let Line {
Expand Down
17 changes: 17 additions & 0 deletions swayfmt/src/utils/language/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ impl Format for Expr {
let (field_width, body_width) =
get_field_width(fields.get(), &mut formatter.clone())?;

formatter.shape.code_line.update_expr_new_line(true);

// changes to the actual formatter
let expr_width = buf.chars().count();
formatter.shape.code_line.update_width(expr_width);
Expand Down Expand Up @@ -650,6 +652,21 @@ fn format_expr_struct(
Ok(())
}

pub fn should_write_multiline(code: &str, formatter: &Formatter) -> bool {
if formatter.shape.code_line.expr_new_line {
true
} else {
let max_per_line = formatter.shape.width_heuristics.collection_width;
for (i, c) in code.chars().rev().enumerate() {
if c == '\n' {
return i > max_per_line;
}
}

false
}
}

fn format_method_call(
target: &Expr,
dot_token: &DotToken,
Expand Down
33 changes: 30 additions & 3 deletions swayfmt/src/utils/language/expr/struct_field.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use crate::{
config::items::ItemBraceStyle,
formatter::{shape::LineStyle, *},
formatter::{
shape::{ExprKind, LineStyle},
*,
},
utils::{
language::expr::should_write_multiline,
map::byte_span::{ByteSpan, LeafSpans},
CurlyBrace,
},
Expand All @@ -18,8 +22,31 @@ impl Format for ExprStructField {
) -> Result<(), FormatterError> {
write!(formatted_code, "{}", self.field_name.span().as_str())?;
if let Some((colon_token, expr)) = &self.expr_opt {
write!(formatted_code, "{} ", colon_token.span().as_str())?;
expr.format(formatted_code, formatter)?;
formatter.with_shape(
formatter
.shape
.with_code_line_from(LineStyle::Inline, ExprKind::Struct),
|formatter| -> Result<(), FormatterError> {
let mut expr_str = FormattedCode::new();
expr.format(&mut expr_str, formatter)?;

let expr_str = if should_write_multiline(&expr_str, formatter) {
let mut expr_str = FormattedCode::new();
formatter.shape.code_line.update_expr_new_line(true);
expr.format(&mut expr_str, formatter)?;
expr_str
} else {
expr_str
};
write!(
formatted_code,
"{} {}",
colon_token.span().as_str(),
expr_str
)?;
Ok(())
},
)?;
}

Ok(())
Expand Down
15 changes: 1 addition & 14 deletions swayfmt/src/utils/language/punctuated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,7 @@ use sway_ast::{
};
use sway_types::{ast::PunctKind, Ident, Spanned};

fn should_write_multiline(code: &str, formatter: &Formatter) -> bool {
if formatter.shape.code_line.expr_new_line {
true
} else {
let max_per_line = formatter.shape.width_heuristics.collection_width;
for (i, c) in code.chars().rev().enumerate() {
if c == '\n' {
return i > max_per_line;
}
}

false
}
}
use super::expr::should_write_multiline;

impl<T, P> Format for Punctuated<T, P>
where
Expand Down
67 changes: 67 additions & 0 deletions swayfmt/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1890,3 +1890,70 @@ fn main() {
"#,
);
}

#[test]
fn struct_new_line() {
check(
r#"
library;
struct Item {
price: u64, amount: u64,
id: u64,
}
enum MyEnum {
Item: Item,
}
fn main() {
let my_enum = MyEnum::Item(Item {
price: 5, amount: 2, id: 42,
});
}
"#,
r#"library;
struct Item {
price: u64,
amount: u64,
id: u64,
}
enum MyEnum {
Item: Item,
}
fn main() {
let my_enum = MyEnum::Item(Item {
price: 5,
amount: 2,
id: 42,
});
}
"#,
)
}

#[test]
fn struct_new_line_2() {
check(
r#"
library;
impl Convert<Square> for Rectangle { fn from(t: Square) -> Self {
Self { width: t
.width,length: t.width,
}}}
"#,
r#"library;
impl Convert<Square> for Rectangle {
fn from(t: Square) -> Self {
Self {
width: t.width,
length: t.width,
}
}
}
"#,
)
}

0 comments on commit 39cccee

Please sign in to comment.