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
26 changes: 24 additions & 2 deletions sway-fmt-v2/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,15 +606,17 @@ fn hello_world(baz: /* this is a comment */ u64) { // This is a comment inside t
abi StorageMapExample {
// insert_into_map is blah blah
#[storage(write)] // this is some other comment
fn insert_into_map(key: u64, value: u64); // this is the last comment inside the StorageMapExample
fn insert_into_map(key: u64, value: u64);
// this is the last comment inside the StorageMapExample
}"#;
let correct_sway_code = r#"contract;

// This is an abi
abi StorageMapExample {
// insert_into_map is blah blah
#[storage(write)] // this is some other comment
fn insert_into_map(key: u64, value: u64); // this is the last comment inside the StorageMapExample
fn insert_into_map(key: u64, value: u64);
// this is the last comment inside the StorageMapExample
}"#;
let mut formatter = Formatter::default();
let formatted_sway_code =
Expand Down Expand Up @@ -680,4 +682,24 @@ trait Programmer {
Formatter::format(&mut formatter, Arc::from(sway_code_to_format), None).unwrap();
assert_eq!(correct_sway_code, formatted_sway_code)
}

#[test]
fn test_where_comment() {
let sway_code_to_format = r#"contract;

pub fn hello( person: String ) -> String where /* This is next to where */ T: Eq, /*Here is a comment*/{let greeting = 42;greeting.to_string()}"#;
let correct_sway_code = r#"contract;

pub fn hello(person: String) -> String
where /* This is next to where */
T: Eq, /*Here is a comment*/
{
let greeting = 42;
greeting.to_string()
}"#;
let mut formatter = Formatter::default();
let formatted_sway_code =
Formatter::format(&mut formatter, Arc::from(sway_code_to_format), None).unwrap();
assert_eq!(correct_sway_code, formatted_sway_code)
}
}
48 changes: 5 additions & 43 deletions sway-fmt-v2/src/items/item_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::{
config::items::ItemBraceStyle,
fmt::{Format, FormattedCode, Formatter},
utils::{
attribute::FormatDecl,
bracket::CurlyBrace,
comments::{ByteSpan, LeafSpans},
},
Expand All @@ -29,32 +28,14 @@ impl Format for ItemAbi {

// abi_items
let mut abi_items_iter = self.abi_items.get().iter().peekable();
while let Some(item) = abi_items_iter.next() {
let attribute_list = item.0.attribute_list.clone();
// add indent + format attribute if it exists
if !attribute_list.is_empty() {
write!(
formatted_code,
"{}",
&formatter.shape.indent.to_string(formatter),
)?;
for attr in attribute_list {
attr.format(formatted_code, formatter)?;
}
}
while let Some((fn_signature, semicolon)) = abi_items_iter.next() {
// add indent + format item
write!(
formatted_code,
"{}",
&formatter.shape.indent.to_string(formatter),
)?;
fn_signature.format(formatted_code, formatter)?;
writeln!(
formatted_code,
"{}{}",
item.0.value.span().as_str(), // TODO(PR #2173): FnSignature formatting
item.1.span().as_str() // SemicolonToken
"{}",
semicolon.ident().as_str() // SemicolonToken
)?;

if abi_items_iter.peek().is_some() {
writeln!(formatted_code)?;
}
Expand All @@ -64,27 +45,8 @@ impl Format for ItemAbi {
if let Some(abi_defs) = self.abi_defs_opt.clone() {
let mut iter = abi_defs.get().iter().peekable();
while let Some(item) = iter.next() {
let attribute_list = item.attribute_list.clone();
// add indent + format attribute if it exists
if !attribute_list.is_empty() {
write!(
formatted_code,
"{}",
&formatter.shape.indent.to_string(formatter),
)?;
for attr in attribute_list {
attr.format(formatted_code, formatter)?;
}
}

// add indent + format item
write!(
formatted_code,
"{}",
&formatter.shape.indent.to_string(formatter),
)?;
item.value.format(formatted_code, formatter)?; // TODO(PR #2173): ItemFn formatting

item.format(formatted_code, formatter)?;
if iter.peek().is_some() {
writeln!(formatted_code)?;
}
Expand Down
16 changes: 5 additions & 11 deletions sway-fmt-v2/src/items/item_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,17 @@ impl Format for ItemConst {
write!(formatted_code, "{}", self.name.as_str())?;

// Check if ty exists
if let Some(ty) = &self.ty_opt {
if let Some((colon_token, ty)) = &self.ty_opt {
// Add colon
write!(formatted_code, "{} ", ty.0.span().as_str())?;
ty.1.format(formatted_code, formatter)?;
write!(formatted_code, "{} ", colon_token.ident().as_str())?;
ty.format(formatted_code, formatter)?;
}

// ` = `
write!(formatted_code, " {} ", self.eq_token.ident().as_str())?;

// TODO: We are not applying any custom formatting to expr, probably we will need to in the future.
write!(
formatted_code,
"{}{}",
self.expr.span().as_str(),
self.semicolon_token.ident().as_str()
)?;
self.expr.format(formatted_code, formatter)?;
write!(formatted_code, "{}", self.semicolon_token.ident().as_str())?;

Ok(())
}
Expand All @@ -56,7 +51,6 @@ impl LeafSpans for ItemConst {
collected_spans.push(ByteSpan::from(self.name.span()));
if let Some(ty) = &self.ty_opt {
collected_spans.append(&mut ty.leaf_spans());
// TODO: determine if we allow comments in between `:` and ty
}
collected_spans.push(ByteSpan::from(self.eq_token.span()));
collected_spans.append(&mut self.expr.leaf_spans());
Expand Down
17 changes: 8 additions & 9 deletions sway-fmt-v2/src/items/item_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,9 @@ fn format_enum(
});

let mut value_pairs_iter = value_pairs.iter().enumerate().peekable();
for (var_index, variant) in value_pairs_iter.clone() {
for (var_index, (type_field, comma_token)) in value_pairs_iter.clone() {
formatted_code.push_str(&formatter.shape.indent.to_string(formatter));

let type_field = &variant.0;
// Add name
write!(formatted_code, "{}", type_field.name.as_str())?;
let current_variant_length = variant_length[var_index];
Expand All @@ -133,25 +132,25 @@ fn format_enum(
)?;
type_field.ty.format(formatted_code, formatter)?;
if value_pairs_iter.peek().is_some() {
writeln!(formatted_code, "{}", variant.1.span().as_str())?;
writeln!(formatted_code, "{}", comma_token.ident().as_str())?;
} else if let Some(final_value) = &variants.final_value_opt {
write!(formatted_code, "{}", final_value.span().as_str())?;
}
}
}
FieldAlignment::Off => {
let mut value_pairs_iter = variants.value_separator_pairs.iter().peekable();
for variant in value_pairs_iter.clone() {
for (type_field, comma_token) in value_pairs_iter.clone() {
write!(
formatted_code,
"{}",
&formatter.shape.indent.to_string(formatter)
)?;
// TypeField
variant.0.format(formatted_code, formatter)?;
type_field.format(formatted_code, formatter)?;

if value_pairs_iter.peek().is_some() {
writeln!(formatted_code, "{}", variant.1.span().as_str())?;
writeln!(formatted_code, "{}", comma_token.ident().as_str())?;
}
}
if let Some(final_value) = &variants.final_value_opt {
Expand All @@ -169,11 +168,11 @@ fn format_enum(
// non-multiline formatting
write!(formatted_code, " ")?;
let mut value_pairs_iter = variants.value_separator_pairs.iter().peekable();
for variant in value_pairs_iter.clone() {
variant.0.format(formatted_code, formatter)?;
for (ty, comma_token) in value_pairs_iter.clone() {
ty.format(formatted_code, formatter)?;

if value_pairs_iter.peek().is_some() {
write!(formatted_code, "{} ", variant.1.span().as_str())?;
write!(formatted_code, "{} ", comma_token.ident().as_str())?;
}
}
if let Some(final_value) = &variants.final_value_opt {
Expand Down
16 changes: 9 additions & 7 deletions sway-fmt-v2/src/items/item_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,24 +119,24 @@ impl Format for FnSignature {
// `self`
formatted_code.push_str(self_token.span().as_str());
// `args_opt`
if let Some(args) = args_opt {
if let Some((comma, args)) = args_opt {
// `, `
write!(formatted_code, "{} ", args.0.span().as_str())?;
write!(formatted_code, "{} ", comma.ident().as_str())?;
// `Punctuated<FnArg, CommaToken>`
args.1.format(formatted_code, formatter)?;
args.format(formatted_code, formatter)?;
}
}
}
// `)`
Self::close_parenthesis(formatted_code, formatter)?;
// `return_type_opt`
if let Some(return_type) = &self.return_type_opt {
if let Some((right_arrow, ty)) = &self.return_type_opt {
write!(
formatted_code,
" {} ",
return_type.0.span().as_str() // `->`
right_arrow.ident().as_str() // `->`
)?;
return_type.1.format(formatted_code, formatter)?; // `Ty`
ty.format(formatted_code, formatter)?; // `Ty`
}
// `WhereClause`
if let Some(where_clause) = &self.where_clause_opt {
Expand Down Expand Up @@ -244,7 +244,9 @@ impl LeafSpans for FnSignature {
if let Some(return_type) = &self.return_type_opt {
collected_spans.append(&mut return_type.leaf_spans());
}
// TODO add where, I will add where for all items at once.
if let Some(where_clause) = &self.where_clause_opt {
collected_spans.append(&mut where_clause.leaf_spans());
}
collected_spans
}
}
Expand Down
4 changes: 3 additions & 1 deletion sway-fmt-v2/src/items/item_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ impl LeafSpans for ItemImpl {
collected_spans.append(&mut trait_tuple.leaf_spans());
}
collected_spans.append(&mut self.ty.leaf_spans());
// TODO add where
if let Some(where_clause) = &self.where_clause_opt {
collected_spans.append(&mut where_clause.leaf_spans());
}
collected_spans.append(&mut self.contents.leaf_spans());
collected_spans
}
Expand Down
31 changes: 18 additions & 13 deletions sway-fmt-v2/src/items/item_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,13 @@ fn format_storage(
});

let mut value_pairs_iter = value_pairs.iter().enumerate().peekable();
for (field_index, field) in value_pairs_iter.clone() {
for (field_index, (storage_field, comma_token)) in value_pairs_iter.clone() {
write!(
formatted_code,
"{}",
&formatter.shape.indent.to_string(formatter)
)?;

let storage_field = &field.0;
// Add name
write!(formatted_code, "{}", storage_field.name.as_str())?;

Expand Down Expand Up @@ -124,25 +123,25 @@ fn format_storage(
.initializer
.format(formatted_code, formatter)?;
if value_pairs_iter.peek().is_some() {
writeln!(formatted_code, "{}", field.1.span().as_str())?;
writeln!(formatted_code, "{}", comma_token.ident().as_str())?;
} else if let Some(final_value) = &fields.final_value_opt {
final_value.format(formatted_code, formatter)?;
}
}
}
FieldAlignment::Off => {
let mut value_pairs_iter = fields.value_separator_pairs.iter().peekable();
for field in value_pairs_iter.clone() {
for (storage_field, comma_token) in value_pairs_iter.clone() {
write!(
formatted_code,
"{}",
&formatter.shape.indent.to_string(formatter)
)?;
// storage_field
field.0.format(formatted_code, formatter)?;
storage_field.format(formatted_code, formatter)?;

if value_pairs_iter.peek().is_some() {
writeln!(formatted_code, "{}", field.1.span().as_str())?;
writeln!(formatted_code, "{}", comma_token.ident().as_str())?;
}
}
if let Some(final_value) = &fields.final_value_opt {
Expand All @@ -160,19 +159,25 @@ fn format_storage(
// non-multiline formatting
write!(formatted_code, " ")?;
let mut value_pairs_iter = fields.value_separator_pairs.iter().peekable();
for field in value_pairs_iter.clone() {
for (storage_field, comma_token) in value_pairs_iter.clone() {
// storage_field
write!(
formatted_code,
"{}{} ",
field.0.name.span().as_str(),
field.0.colon_token.span().as_str(),
storage_field.name.span().as_str(),
storage_field.colon_token.span().as_str(),
)?;
field.0.ty.format(formatted_code, formatter)?;
write!(formatted_code, " {} ", field.0.eq_token.ident().as_str())?;
field.0.initializer.format(formatted_code, formatter)?;
storage_field.ty.format(formatted_code, formatter)?;
write!(
formatted_code,
" {} ",
storage_field.eq_token.ident().as_str()
)?;
storage_field
.initializer
.format(formatted_code, formatter)?;
if value_pairs_iter.peek().is_some() {
write!(formatted_code, "{} ", field.1.span().as_str())?;
write!(formatted_code, "{} ", comma_token.span().as_str())?;
}
}
if let Some(final_value) = &fields.final_value_opt {
Expand Down
11 changes: 5 additions & 6 deletions sway-fmt-v2/src/items/item_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,13 @@ fn format_struct(
});

let mut value_pairs_iter = value_pairs.iter().enumerate().peekable();
for (field_index, field) in value_pairs_iter.clone() {
for (field_index, (type_field, comma_token)) in value_pairs_iter.clone() {
write!(
formatted_code,
"{}",
&formatter.shape.indent.to_string(formatter)
)?;

let type_field = &field.0;
// Add name
write!(formatted_code, "{}", type_field.name.as_str())?;

Expand All @@ -137,25 +136,25 @@ fn format_struct(
)?;
type_field.ty.format(formatted_code, formatter)?;
if value_pairs_iter.peek().is_some() {
writeln!(formatted_code, "{}", field.1.span().as_str())?;
writeln!(formatted_code, "{}", comma_token.ident().as_str())?;
} else if let Some(final_value) = &fields.final_value_opt {
write!(formatted_code, "{}", final_value.span().as_str())?;
}
}
}
FieldAlignment::Off => {
let mut value_pairs_iter = fields.value_separator_pairs.iter().peekable();
for field in value_pairs_iter.clone() {
for (type_field, comma_token) in value_pairs_iter.clone() {
write!(
formatted_code,
"{}",
&formatter.shape.indent.to_string(formatter)
)?;
// TypeField
field.0.format(formatted_code, formatter)?;
type_field.format(formatted_code, formatter)?;

if value_pairs_iter.peek().is_some() {
writeln!(formatted_code, "{}", field.1.span().as_str())?;
writeln!(formatted_code, "{}", comma_token.ident().as_str())?;
}
}
if let Some(final_value) = &fields.final_value_opt {
Expand Down
Loading