diff --git a/sway-fmt-v2/src/fmt.rs b/sway-fmt-v2/src/fmt.rs
index c16d18976ad..590e67d98a3 100644
--- a/sway-fmt-v2/src/fmt.rs
+++ b/sway-fmt-v2/src/fmt.rs
@@ -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 Qux 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 Qux for Foo where
+ A: Qux,
+ B: Qux,
+{
+ fn is_baz_true(self) -> bool {
+ self.baz
+ }
}"#;
let mut formatter = Formatter::default();
let formatted_sway_code =
diff --git a/sway-fmt-v2/src/items/item_fn.rs b/sway-fmt-v2/src/items/item_fn.rs
index 22acd039fd8..75ea2eec9b7 100644
--- a/sway-fmt-v2/src/items/item_fn.rs
+++ b/sway-fmt-v2/src/items/item_fn.rs
@@ -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(())
}
}
@@ -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();
}
diff --git a/sway-fmt-v2/src/items/item_impl.rs b/sway-fmt-v2/src/items/item_impl.rs
index f1d89770816..10ef0234ba4 100644
--- a/sway-fmt-v2/src/items/item_impl.rs
+++ b/sway-fmt-v2/src/items/item_impl.rs
@@ -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 {
let mut collected_spans = vec![ByteSpan::from(self.impl_token.span())];
diff --git a/sway-fmt-v2/src/items/item_trait.rs b/sway-fmt-v2/src/items/item_trait.rs
index e0331817646..972df8da9c7 100644
--- a/sway-fmt-v2/src/items/item_trait.rs
+++ b/sway-fmt-v2/src/items/item_trait.rs
@@ -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(())
diff --git a/sway-fmt-v2/src/utils/where_clause.rs b/sway-fmt-v2/src/utils/where_clause.rs
index 325bf607211..12fd8ca76e5 100644
--- a/sway-fmt-v2/src/utils/where_clause.rs
+++ b/sway-fmt-v2/src/utils/where_clause.rs
@@ -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(),
)?;