Skip to content

Commit 8e0feda

Browse files
fix(fmt): only indent wrapped trailing block cmnts (#12319)
* fix(fmt): only indent wrapped trailing cmnts which are line cmnts * style: flip --------- Co-authored-by: grandizzy <[email protected]>
1 parent 5fe5510 commit 8e0feda

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

crates/fmt/src/state/mod.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -870,15 +870,14 @@ impl<'sess> State<'sess, '_> {
870870
if !self.config.wrap_comments && cmnt.lines.len() == 1 {
871871
self.word(cmnt.lines.pop().unwrap());
872872
} else if self.config.wrap_comments {
873-
config.offset = self.ind;
873+
if cmnt.is_doc || matches!(cmnt.kind, ast::CommentKind::Line) {
874+
config.offset = 0;
875+
} else {
876+
config.offset = self.ind;
877+
}
874878
for (lpos, line) in cmnt.lines.into_iter().delimited() {
875879
if !line.is_empty() {
876-
self.print_wrapped_line(
877-
&line,
878-
prefix,
879-
if cmnt.is_doc { 0 } else { config.offset },
880-
cmnt.is_doc,
881-
);
880+
self.print_wrapped_line(&line, prefix, config.offset, cmnt.is_doc);
882881
}
883882
if !lpos.is_last {
884883
config.hardbreak(&mut self.s);

crates/fmt/testdata/SimpleComments/fmt.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
contract SimpleComments {
2+
uint40 constant PERIOD = uint40(12345); // ~578 days
3+
// Represents the depletion timestamp
4+
uint40 constant WARP_PERIOD = FEB_1_2025 + PERIOD;
5+
26
//´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:
37
// VARIABLES
48
//.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•

crates/fmt/testdata/SimpleComments/original.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
contract SimpleComments {
2+
uint40 constant PERIOD = uint40(12345); // ~578 days
3+
// Represents the depletion timestamp
4+
uint40 constant WARP_PERIOD = FEB_1_2025 + PERIOD;
5+
26
//´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:
37
// VARIABLES
48
//.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•

crates/fmt/testdata/SimpleComments/wrap-comments.fmt.sol

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// config: line_length = 60
22
// config: wrap_comments = true
33
contract SimpleComments {
4+
uint40 constant PERIOD = uint40(12345); // ~578 days
5+
// Represents the depletion timestamp
6+
uint40 constant WARP_PERIOD = FEB_1_2025 + PERIOD;
7+
48
//´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:
59
// VARIABLES
610
//.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•
@@ -46,8 +50,8 @@ contract SimpleComments {
4650

4751
function test4() public view returns (uint256) {
4852
uint256 abc; // long postfix comment that exceeds
49-
// line width. the comment should be split and
50-
// carried over to the next line
53+
// line width. the comment should be split and
54+
// carried over to the next line
5155
uint256 abc2; // reallylongsinglewordcommentthatexceedslinewidththecommentshouldbesplitandcarriedovertothenextline
5256

5357
// long prefix comment that exceeds line width. the

0 commit comments

Comments
 (0)