Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix formatting conditionals with comments #4936

Merged
merged 4 commits into from
Aug 10, 2023
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
7 changes: 7 additions & 0 deletions sway-ast/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,13 @@ impl Spanned for MatchBranchKind {
pub struct CodeBlockContents {
pub statements: Vec<Statement>,
pub final_expr_opt: Option<Box<Expr>>,
pub span: Span,
}

impl Spanned for CodeBlockContents {
fn span(&self) -> Span {
self.span.clone()
}
}

#[derive(Clone, Debug, Serialize)]
Expand Down
1 change: 1 addition & 0 deletions sway-parse/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ mod tests {
inner: Nil,
span: (178, 180),
))),
span: (161, 193),
),
span: (160, 194),
),
Expand Down
1 change: 1 addition & 0 deletions sway-parse/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
1 change: 1 addition & 0 deletions sway-parse/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ mod tests {
inner: Nil,
span: (70, 72),
))),
span: (53, 85),
),
span: (52, 86),
),
Expand Down
4 changes: 4 additions & 0 deletions sway-parse/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ impl<'a, 'e> Parser<'a, 'e> {
}
Ok(())
}

pub fn full_span(&self) -> &Span {
&self.full_span
}
}

pub struct Peeker<'a> {
Expand Down
1 change: 1 addition & 0 deletions sway-parse/tests/noop_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ fn noop_script_file() {
inner: Nil,
span: (48, 50),
))),
span: (39, 57),
),
span: (38, 58),
),
Expand Down
32 changes: 32 additions & 0 deletions swayfmt/src/items/item_fn/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
JoshuaBatty marked this conversation as resolved.
Show resolved Hide resolved
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
}
}"
);
12 changes: 11 additions & 1 deletion swayfmt/src/utils/language/expr/code_block.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
comments::write_comments,
config::items::ItemBraceStyle,
formatter::{shape::LineStyle, *},
utils::{
Expand All @@ -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(
Expand Down Expand Up @@ -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(())
Expand Down
18 changes: 13 additions & 5 deletions swayfmt/src/utils/language/expr/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down