Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
44 changes: 44 additions & 0 deletions sway-fmt-v2/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,4 +680,48 @@ 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_impl() {
let sway_code_to_format = r#"script;

struct Foo {
bar: u64,
baz: bool,
}

trait Qux {
fn is_baz_true(self) -> bool;
}

impl<A, B> Qux<A, B> for Foo where
A: Qux,
B: Qux,
{
fn is_baz_true(self) -> bool {
self.baz
}
}"#;
let correct_sway_code = r#"script;

struct Foo {
bar: u64,
baz: bool,
}
trait Qux {
fn is_baz_true(self) -> bool;
}

impl<A, B> Qux<A, B> for Foo where
A: Qux,
B: Qux,
{
fn is_baz_true(self) -> bool {
self.baz
}
}"#;
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)
}
}
18 changes: 13 additions & 5 deletions sway-fmt-v2/src/items/item_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ impl CurlyBrace for ItemFn {
line: &mut FormattedCode,
formatter: &mut Formatter,
) -> Result<(), FormatterError> {
writeln!(line, "{}", Delimiter::Brace.as_close_char())?;
// If shape is becoming left-most alligned or - indent just have the defualt shape
formatter.shape = formatter
.shape
.shrink_left(formatter.config.whitespace.tab_spaces)
.unwrap_or_default();
formatter.shape.indent = formatter.shape.indent.block_unindent(formatter);
writeln!(
line,
"{}{}",
formatter.shape.indent.to_string(formatter),
Delimiter::Brace.as_close_char()
)?;
Ok(())
}
}
Expand All @@ -84,6 +86,11 @@ impl Format for FnSignature {
formatted_code: &mut String,
formatter: &mut Formatter,
) -> Result<(), FormatterError> {
write!(
formatted_code,
"{}",
formatter.shape.indent.to_string(formatter)
)?;
// `pub `
if let Some(visibility_token) = &self.visibility {
write!(formatted_code, "{} ", visibility_token.span().as_str())?;
Expand Down Expand Up @@ -140,6 +147,7 @@ impl Format for FnSignature {
}
// `WhereClause`
if let Some(where_clause) = &self.where_clause_opt {
writeln!(formatted_code)?;
where_clause.format(formatted_code, formatter)?;
let mut shape = formatter.shape;
shape = shape.update_where_clause();
Expand Down
95 changes: 88 additions & 7 deletions sway-fmt-v2/src/items/item_impl.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,101 @@
use crate::{
fmt::{Format, FormattedCode, Formatter},
utils::comments::{ByteSpan, LeafSpans},
FormatterError,
config::items::ItemBraceStyle,
fmt::*,
utils::{
bracket::CurlyBrace,
comments::{ByteSpan, LeafSpans},
},
};
use sway_parse::ItemImpl;
use std::fmt::Write;
use sway_parse::{token::Delimiter, ItemImpl};
use sway_types::Spanned;

impl Format for ItemImpl {
fn format(
&self,
_formatted_code: &mut FormattedCode,
_formatter: &mut Formatter,
formatted_code: &mut FormattedCode,
formatter: &mut Formatter,
) -> Result<(), FormatterError> {
todo!()
write!(
formatted_code,
"{}{}",
formatter.shape.indent.to_string(formatter),
self.impl_token.span().as_str()
)?;
if let Some(generic_params) = &self.generic_params_opt {
generic_params.format(formatted_code, formatter)?;
write!(formatted_code, " ")?;
}
if let Some((path_type, for_token)) = &self.trait_opt {
path_type.format(formatted_code, formatter)?;
write!(formatted_code, " {}", for_token.span().as_str())?;
}
write!(formatted_code, " ")?;
self.ty.format(formatted_code, formatter)?;
if let Some(where_clause) = &self.where_clause_opt {
write!(formatted_code, " ")?;
where_clause.format(formatted_code, formatter)?;
let mut shape = formatter.shape;
shape = shape.update_where_clause();
formatter.shape = shape;
}
Self::open_curly_brace(formatted_code, formatter)?;
let contents = self.contents.clone().into_inner();
for item in contents.iter() {
item.format(formatted_code, formatter)?;
}
Self::close_curly_brace(formatted_code, formatter)?;

Ok(())
}
}

impl CurlyBrace for ItemImpl {
fn open_curly_brace(
line: &mut FormattedCode,
formatter: &mut Formatter,
) -> Result<(), FormatterError> {
let brace_style = formatter.config.items.item_brace_style;
let extra_width = formatter.config.whitespace.tab_spaces;
let mut shape = formatter.shape;
let open_brace = Delimiter::Brace.as_open_char();
match brace_style {
ItemBraceStyle::AlwaysNextLine => {
// Add openning brace to the next line.
writeln!(line, "\n{}", open_brace)?;
shape = shape.block_indent(extra_width);
}
ItemBraceStyle::SameLineWhere => match shape.has_where_clause {
true => {
writeln!(line, "{}", open_brace)?;
shape = shape.update_where_clause();
shape = shape.block_indent(extra_width);
}
false => {
writeln!(line, " {}", open_brace)?;
shape = shape.block_indent(extra_width);
}
},
_ => {
// TODO: implement PreferSameLine
writeln!(line, " {}", open_brace)?;
shape = shape.block_indent(extra_width);
}
}

formatter.shape = shape;
Ok(())
}
fn close_curly_brace(
line: &mut FormattedCode,
formatter: &mut Formatter,
) -> Result<(), FormatterError> {
writeln!(line, "{}", Delimiter::Brace.as_close_char())?;
formatter.shape.indent = formatter.shape.indent.block_unindent(formatter);
Ok(())
}
}

impl LeafSpans for ItemImpl {
fn leaf_spans(&self) -> Vec<ByteSpan> {
let mut collected_spans = vec![ByteSpan::from(self.impl_token.span())];
Expand Down
18 changes: 8 additions & 10 deletions sway-fmt-v2/src/items/item_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ impl Format for ItemTrait {
write!(formatted_code, " ")?;
Self::open_curly_brace(formatted_code, formatter)?;
for trait_items in self.trait_items.clone().into_inner() {
write!(
formatted_code,
"{}",
formatter.shape.indent.to_string(formatter)
)?;
// format `Annotated<FnSignature>`
trait_items.0.format(formatted_code, formatter)?;
writeln!(formatted_code, "{}\n", trait_items.1.span().as_str())?;
Expand Down Expand Up @@ -92,11 +87,14 @@ impl CurlyBrace for ItemTrait {
line: &mut FormattedCode,
formatter: &mut Formatter,
) -> Result<(), FormatterError> {
writeln!(line, "{}", Delimiter::Brace.as_close_char())?;
formatter.shape = formatter
.shape
.shrink_left(formatter.config.whitespace.tab_spaces)
.unwrap_or_default();
formatter.shape.indent = formatter.shape.indent.block_unindent(formatter);
writeln!(
line,
"{}{}",
formatter.shape.indent.to_string(formatter),
Delimiter::Brace.as_close_char()
)?;

Ok(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion sway-fmt-v2/src/utils/where_clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl Format for WhereClause {
) -> Result<(), FormatterError> {
writeln!(
formatted_code,
"\n{}{}",
"{}{}",
&formatter.shape.indent.to_string(formatter),
self.where_token.span().as_str(),
)?;
Expand Down