Skip to content

Commit

Permalink
Fix formatting conditionals with comments (FuelLabs#4936)
Browse files Browse the repository at this point in the history
## Description

Closes FuelLabs#4935

- In order to write comments for the else block when it only contains a
comment, I had to add the span to `CodeBlockContents`

## Checklist

- [x] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.
  • Loading branch information
sdankel authored Aug 10, 2023
1 parent d9d9192 commit d1fdd65
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 6 deletions.
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() {
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

0 comments on commit d1fdd65

Please sign in to comment.