diff --git a/sway-ast/src/expr/mod.rs b/sway-ast/src/expr/mod.rs index 4db4cc32323..27d37382e72 100644 --- a/sway-ast/src/expr/mod.rs +++ b/sway-ast/src/expr/mod.rs @@ -406,6 +406,13 @@ impl Spanned for MatchBranchKind { pub struct CodeBlockContents { pub statements: Vec, pub final_expr_opt: Option>, + pub span: Span, +} + +impl Spanned for CodeBlockContents { + fn span(&self) -> Span { + self.span.clone() + } } #[derive(Clone, Debug, Serialize)] diff --git a/sway-parse/src/attribute.rs b/sway-parse/src/attribute.rs index 448bc9857a8..c1f5f6ccd3e 100644 --- a/sway-parse/src/attribute.rs +++ b/sway-parse/src/attribute.rs @@ -237,6 +237,7 @@ mod tests { inner: Nil, span: (178, 180), ))), + span: (161, 193), ), span: (160, 194), ), diff --git a/sway-parse/src/expr/mod.rs b/sway-parse/src/expr/mod.rs index fd84dbe2211..fa8e211fd05 100644 --- a/sway-parse/src/expr/mod.rs +++ b/sway-parse/src/expr/mod.rs @@ -139,6 +139,7 @@ impl ParseToEnd for CodeBlockContents { let code_block_contents = CodeBlockContents { statements, final_expr_opt, + span: parser.full_span().clone(), }; Ok((code_block_contents, consumed)) } diff --git a/sway-parse/src/module.rs b/sway-parse/src/module.rs index 9319c7f11bf..edd33c8bb9c 100644 --- a/sway-parse/src/module.rs +++ b/sway-parse/src/module.rs @@ -132,6 +132,7 @@ mod tests { inner: Nil, span: (70, 72), ))), + span: (53, 85), ), span: (52, 86), ), diff --git a/sway-parse/src/parser.rs b/sway-parse/src/parser.rs index 46c1544517b..0d9e61a65a4 100644 --- a/sway-parse/src/parser.rs +++ b/sway-parse/src/parser.rs @@ -149,6 +149,10 @@ impl<'a, 'e> Parser<'a, 'e> { } Ok(()) } + + pub fn full_span(&self) -> &Span { + &self.full_span + } } pub struct Peeker<'a> { diff --git a/sway-parse/tests/noop_script.rs b/sway-parse/tests/noop_script.rs index dac5fd13e21..fd3fe83cf0e 100644 --- a/sway-parse/tests/noop_script.rs +++ b/sway-parse/tests/noop_script.rs @@ -55,6 +55,7 @@ fn noop_script_file() { inner: Nil, span: (48, 50), ))), + span: (39, 57), ), span: (38, 58), ), diff --git a/swayfmt/src/items/item_fn/tests.rs b/swayfmt/src/items/item_fn/tests.rs index 6ea8bde1556..e250e4f735e 100644 --- a/swayfmt/src/items/item_fn/tests.rs +++ b/swayfmt/src/items/item_fn/tests.rs @@ -88,3 +88,35 @@ fmt_test_item!( fn_nested_if_lets }; }" ); + +fmt_test_item!( fn_conditional_with_comment +"fn conditional_with_comment() { + if true { + // comment here + } +}", +intermediate_whitespace +"fn conditional_with_comment() { + if true { + // comment here + } +}" +); + +fmt_test_item!( fn_conditional_with_comment_and_else +"fn conditional_with_comment() { + if true { + // if + } else { + // else + } +}", +intermediate_whitespace +"fn conditional_with_comment() { + if true { + // if + } else { + // else + } +}" +); diff --git a/swayfmt/src/utils/language/expr/code_block.rs b/swayfmt/src/utils/language/expr/code_block.rs index 5d44d485283..0b0c7af5980 100644 --- a/swayfmt/src/utils/language/expr/code_block.rs +++ b/swayfmt/src/utils/language/expr/code_block.rs @@ -1,4 +1,5 @@ use crate::{ + comments::write_comments, config::items::ItemBraceStyle, formatter::{shape::LineStyle, *}, utils::{ @@ -8,7 +9,7 @@ use crate::{ }; use std::fmt::Write; use sway_ast::CodeBlockContents; -use sway_types::ast::Delimiter; +use sway_types::{ast::Delimiter, Spanned}; impl Format for CodeBlockContents { fn format( @@ -52,6 +53,15 @@ impl Format for CodeBlockContents { } } } + } else { + let comments: bool = write_comments( + formatted_code, + self.span().start()..self.span().end(), + formatter, + )?; + if !comments { + formatter.shape.block_unindent(&formatter.config); + } } Ok(()) diff --git a/swayfmt/src/utils/language/expr/conditional.rs b/swayfmt/src/utils/language/expr/conditional.rs index de6c06f4923..751c8a55ea1 100644 --- a/swayfmt/src/utils/language/expr/conditional.rs +++ b/swayfmt/src/utils/language/expr/conditional.rs @@ -142,16 +142,24 @@ fn format_then_block( formatted_code: &mut FormattedCode, formatter: &mut Formatter, ) -> Result<(), FormatterError> { + IfExpr::open_curly_brace(formatted_code, formatter)?; + if !if_expr.then_block.get().statements.is_empty() || if_expr.then_block.get().final_expr_opt.is_some() { - IfExpr::open_curly_brace(formatted_code, formatter)?; if_expr.then_block.get().format(formatted_code, formatter)?; - if if_expr.else_opt.is_none() { - IfExpr::close_curly_brace(formatted_code, formatter)?; - } } else { - write!(formatted_code, " {{}}")?; + let comments = write_comments( + formatted_code, + if_expr.then_block.span().start()..if_expr.then_block.span().end(), + formatter, + )?; + if !comments { + formatter.shape.block_unindent(&formatter.config); + } + } + if if_expr.else_opt.is_none() { + IfExpr::close_curly_brace(formatted_code, formatter)?; } Ok(())