Skip to content

Commit

Permalink
Fixes the new storage namespaces in swayfmt.
Browse files Browse the repository at this point in the history
  • Loading branch information
esdrubal committed Jun 6, 2024
1 parent bb86372 commit 3941df8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
39 changes: 34 additions & 5 deletions swayfmt/src/items/item_storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ impl Format for ItemStorage {
value_pairs.iter().for_each(|(storage_entry, _)| {
collect_field_lengths(storage_entry, ident_size, 0, &mut field_lengths)
});
if let Some(final_value) = &entries.final_value_opt {
collect_field_lengths(
&final_value.value,
ident_size,
0,
&mut field_lengths,
);
}

// Find the maximum length in the `field_length` vector that is still
// smaller than `storage_field_align_threshold`. `max_valid_field_length`:
Expand All @@ -100,20 +108,35 @@ impl Format for ItemStorage {
field_lengths: &HashMap<IdentUnique, usize>,
max_valid_field_length: usize,
) -> Result<(), FormatterError> {
write!(formatted_code, "{}", formatter.indent_to_str()?)?;
if let Some(namespace) = &entry.namespace {
entry.name.format(formatted_code, formatter)?;
ItemStorage::open_curly_brace(formatted_code, formatter)?;
writeln!(formatted_code)?;

formatter.shape.code_line.update_expr_new_line(true);

for e in namespace.clone().into_inner().into_iter() {
for (e, comma_token) in
namespace.clone().into_inner().value_separator_pairs
{
format_entry(
formatted_code,
formatter,
&e.value,
field_lengths,
max_valid_field_length,
)?;
writeln!(formatted_code, "{}", comma_token.ident().as_str())?;
}
if let Some(final_value) =
&namespace.clone().into_inner().final_value_opt
{
format_entry(
formatted_code,
formatter,
&final_value.value,
field_lengths,
max_valid_field_length,
)?;
writeln!(formatted_code)?;
}

ItemStorage::close_curly_brace(formatted_code, formatter)?;
Expand Down Expand Up @@ -155,7 +178,6 @@ impl Format for ItemStorage {
Ok(())
}
for (storage_entry, comma_token) in value_pairs.iter().clone() {
write!(formatted_code, "{}", formatter.indent_to_str()?)?;
format_entry(
formatted_code,
formatter,
Expand All @@ -166,7 +188,14 @@ impl Format for ItemStorage {
writeln!(formatted_code, "{}", comma_token.ident().as_str())?;
}
if let Some(final_value) = &entries.final_value_opt {
final_value.format(formatted_code, formatter)?;
format_entry(
formatted_code,
formatter,
&final_value.value,
&field_lengths,
max_valid_field_length,
)?;
writeln!(formatted_code)?;
}
}
FieldAlignment::Off => entries.format(formatted_code, formatter)?,
Expand Down
18 changes: 14 additions & 4 deletions swayfmt/src/utils/language/punctuated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use sway_ast::{
};
use sway_types::{ast::PunctKind, Ident, Spanned};

use self::shape::ExprKind;

use super::expr::should_write_multiline;

impl<T, P> Format for Punctuated<T, P>
Expand Down Expand Up @@ -287,10 +289,18 @@ impl Format for StorageEntry {
self.name.format(formatted_code, formatter)?;
ItemStorage::open_curly_brace(formatted_code, formatter)?;
formatter.shape.code_line.update_expr_new_line(true);
namespace
.clone()
.into_inner()
.format(formatted_code, formatter)?;
formatter.with_shape(
formatter
.shape
.with_code_line_from(LineStyle::Multiline, ExprKind::Struct),
|formatter| -> Result<(), FormatterError> {
namespace
.clone()
.into_inner()
.format(formatted_code, formatter)?;
Ok(())
},
)?;
ItemStorage::close_curly_brace(formatted_code, formatter)?;
}

Expand Down
13 changes: 10 additions & 3 deletions swayfmt/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ fn storage_without_alignment() {
storage {
var1: Type1=Type1{ foo: 8 },
var2: Type2=Type2{ bar: 9 },
ns1 { var3: u64 = 1 },
ns1 { var3: u64 = 1, ns2 { var4: u64 = 1 } },
}
"#,
r#"contract;
Expand All @@ -314,7 +314,10 @@ storage {
var2: Type2 = Type2 { bar: 9 },
ns1 {
var3: u64 = 1,
}
ns2 {
var4: u64 = 1,
},
},
}
"#,
);
Expand All @@ -337,7 +340,7 @@ struct Type2 {
storage {
long_var_name: Type1=Type1{ foo: 8 },
var2: Type2=Type2{ bar: 9 },
ns1 { var3: u64 = 1 },
ns1 { var3: u64 = 1, ns2 { var4: u64 = 1, }, }, var5: u64 = 1
}
"#,
r#"contract;
Expand All @@ -355,7 +358,11 @@ storage {
var2 : Type2 = Type2 { bar: 9 },
ns1 {
var3 : u64 = 1,
ns2 {
var4 : u64 = 1,
},
},
var5 : u64 = 1
}
"#,
&mut formatter,
Expand Down

0 comments on commit 3941df8

Please sign in to comment.