Skip to content

Commit

Permalink
Adds impl formatting to sfv2 (FuelLabs#2403)
Browse files Browse the repository at this point in the history
* add impl formatting and test

* update test and where clause

* make test messier

* fix comment

* tests passing
  • Loading branch information
eureka-cpu authored Aug 1, 2022
1 parent 50f4fae commit b56f4dc
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 12 deletions.
44 changes: 44 additions & 0 deletions sway-fmt-v2/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,50 @@ where /* This is next to where */
{
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)
}
#[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 =
Expand Down
8 changes: 7 additions & 1 deletion sway-fmt-v2/src/items/item_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,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.block_unindent(&formatter.config);
writeln!(
line,
"{}{}",
formatter.shape.indent.to_string(&formatter.config)?,
Delimiter::Brace.as_close_char()
)?;
Ok(())
}
}
Expand Down Expand Up @@ -134,6 +139,7 @@ impl Format for FnSignature {
}
// `WhereClause`
if let Some(where_clause) = &self.where_clause_opt {
writeln!(formatted_code)?;
where_clause.format(formatted_code, formatter)?;
formatter.shape.update_where_clause();
}
Expand Down
90 changes: 83 additions & 7 deletions sway-fmt-v2/src/items/item_impl.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,96 @@
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.config)?,
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)?;
formatter.shape.update_where_clause();
}
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 open_brace = Delimiter::Brace.as_open_char();
match brace_style {
ItemBraceStyle::AlwaysNextLine => {
// Add opening brace to the next line.
writeln!(line, "\n{}", open_brace)?;
formatter.shape.block_indent(&formatter.config);
}
ItemBraceStyle::SameLineWhere => match formatter.shape.has_where_clause {
true => {
writeln!(line, "{}", open_brace)?;
formatter.shape.update_where_clause();
formatter.shape.block_indent(&formatter.config);
}
false => {
writeln!(line, " {}", open_brace)?;
formatter.shape.block_indent(&formatter.config);
}
},
_ => {
// TODO: implement PreferSameLine
writeln!(line, " {}", open_brace)?;
formatter.shape.block_indent(&formatter.config);
}
}

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

impl LeafSpans for ItemImpl {
fn leaf_spans(&self) -> Vec<ByteSpan> {
let mut collected_spans = vec![ByteSpan::from(self.impl_token.span())];
Expand Down
6 changes: 3 additions & 3 deletions sway-fmt-v2/src/items/item_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ impl Format for Traits {
// additional `PathType`s
//
// ` + PathType`
for paths in self.suffixes.iter() {
write!(formatted_code, " {} ", paths.0.span().as_str())?;
paths.1.format(formatted_code, formatter)?;
for (add_token, path_type) in self.suffixes.iter() {
write!(formatted_code, " {} ", add_token.span().as_str())?;
path_type.format(formatted_code, formatter)?;
}

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 @@ -13,7 +13,7 @@ impl Format for WhereClause {
) -> Result<(), FormatterError> {
writeln!(
formatted_code,
"\n{}{}",
"{}{}",
&formatter.shape.indent.to_string(&formatter.config)?,
self.where_token.span().as_str(),
)?;
Expand Down

0 comments on commit b56f4dc

Please sign in to comment.