Skip to content

Commit

Permalink
fix: empty fn comments (FuelLabs#4093)
Browse files Browse the repository at this point in the history
## Description

closes FuelLabs#4092

Note that this PR also moves the block indentation logic up one level
into the `ItemFn` level instead of at the `CurlyBrace` impl.

## 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.

---------

Co-authored-by: Joshua Batty <[email protected]>
  • Loading branch information
eightfilms and JoshuaBatty authored Feb 17, 2023
1 parent 18f90e4 commit 5c23176
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
17 changes: 11 additions & 6 deletions swayfmt/src/items/item_fn/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
comments::{has_comments, maybe_write_comments_from_map},
config::items::ItemBraceStyle,
formatter::{
shape::{ExprKind, LineStyle},
Expand All @@ -9,7 +10,7 @@ use crate::{
{CurlyBrace, Parenthesis},
},
};
use std::fmt::Write;
use std::{fmt::Write, ops::Range};
use sway_ast::{
keywords::{MutToken, RefToken, SelfToken, Token},
token::Delimiter,
Expand All @@ -35,10 +36,18 @@ impl Format for ItemFn {
let body = self.body.get();
if !body.statements.is_empty() || body.final_expr_opt.is_some() {
Self::open_curly_brace(formatted_code, formatter)?;
formatter.shape.block_indent(&formatter.config);
body.format(formatted_code, formatter)?;
Self::close_curly_brace(formatted_code, formatter)?;
} else {
write!(formatted_code, " {{}}")?;
Self::open_curly_brace(formatted_code, formatter)?;
let range: Range<usize> = self.span().into();
let comments = formatter.comment_map.comments_between(&range);
if has_comments(comments) {
formatter.shape.block_indent(&formatter.config);
maybe_write_comments_from_map(formatted_code, range, formatter)?;
}
Self::close_curly_brace(formatted_code, formatter)?;
}

Ok(())
Expand All @@ -60,23 +69,19 @@ impl CurlyBrace for ItemFn {
ItemBraceStyle::AlwaysNextLine => {
// Add openning brace to the next line.
writeln!(line, "\n{open_brace}")?;
formatter.shape.block_indent(&formatter.config);
}
ItemBraceStyle::SameLineWhere => match formatter.shape.code_line.has_where_clause {
true => {
write!(line, "{open_brace}")?;
formatter.shape.code_line.update_where_clause(false);
formatter.shape.block_indent(&formatter.config);
}
false => {
write!(line, " {open_brace}")?;
formatter.shape.block_indent(&formatter.config);
}
},
_ => {
// TODO: implement PreferSameLine
writeln!(line, " {open_brace}")?;
formatter.shape.block_indent(&formatter.config);
}
}

Expand Down
35 changes: 35 additions & 0 deletions swayfmt/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,41 @@ trait AlignMyComments {
);
}

#[test]
fn comments_empty_fns() {
check(
r#"contract;
fn single_comment_same_line() { /* a comment */ }
fn single_comment() -> bool {
// TODO: This is a TODO
}
fn multiline_comments() {
// Multi
// line
// comment
}"#,
r#"contract;
fn single_comment_same_line() {
/* a comment */
}
fn single_comment() -> bool {
// TODO: This is a TODO
}
fn multiline_comments() {
// Multi
// line
// comment
}
"#,
);
}

#[test]
fn enum_comments() {
check(
Expand Down

0 comments on commit 5c23176

Please sign in to comment.