diff --git a/CHANGELOG.md b/CHANGELOG.md index d4b4fa80..bfe0c929 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,35 @@ # Development version +- We now preserve the placement of comments after `=`. E.g. this: + + ```r + foo( + name = # comment + value + ) + ``` + + now gets formatted to: + + ```r + foo( + name = + # comment + value + ) + ``` + + instead of: + + ```r + foo( + # comment + name = + value + ) + ``` + - Added support for formatting notebook cells (progress towards #405, @kv9898). diff --git a/crates/air_r_formatter/src/comments.rs b/crates/air_r_formatter/src/comments.rs index f3908be6..55a2e578 100644 --- a/crates/air_r_formatter/src/comments.rs +++ b/crates/air_r_formatter/src/comments.rs @@ -779,7 +779,11 @@ fn handle_argument_comment(comment: DecoratedComment) -> CommentPlace // ) // ``` if name_clause.syntax() == preceding { - return CommentPlacement::leading(preceding.clone(), comment); + if let Some(following) = comment.following_node() { + return CommentPlacement::leading(following.clone(), comment); + } else { + return CommentPlacement::leading(preceding.clone(), comment); + } } } diff --git a/crates/air_r_formatter/src/r/auxiliary/argument.rs b/crates/air_r_formatter/src/r/auxiliary/argument.rs index d74d2718..c563262c 100644 --- a/crates/air_r_formatter/src/r/auxiliary/argument.rs +++ b/crates/air_r_formatter/src/r/auxiliary/argument.rs @@ -36,7 +36,27 @@ pub(crate) fn fmt_argument_fields(node: &RArgument, f: &mut RFormatter) -> Forma // Named argument with a value // `foo(name = value)` (Some(name_clause), Some(value)) => { - write!(f, [name_clause.format(), space(), value.format()]) + // If `value` has leading comments (which might happen due to our + // comment placement rules in arguments), we consistently break + // after the name clause and indent the value + if f.comments().has_leading_comments(value.syntax()) { + write!( + f, + [ + name_clause.format(), + space(), + // Note that this _will_ break, since the comment causes a hard line break + soft_line_indent_or_space(&value.format()) + ] + ) + } else { + // Never break before the value, even if `name = value` doesn't + // fit on one line. This allows more consistent formatting, but + // the main reason is for behaviour within a group. A soft line + // break would always break within expanded calls, even if the + // name-value pair comfortably fits on a line. + write!(f, [name_clause.format(), space(), value.format()]) + } } } } diff --git a/crates/air_r_formatter/tests/specs/r/call.R b/crates/air_r_formatter/tests/specs/r/call.R index b9009d19..272fe01e 100644 --- a/crates/air_r_formatter/tests/specs/r/call.R +++ b/crates/air_r_formatter/tests/specs/r/call.R @@ -527,6 +527,18 @@ with( # own-line ) +list( + # comment0 + foo # comment1 + =1, bar # comment2 + = # comment3 + # comment4 + 2 #comment5 + #comment6 + , #comment7 + #comment8 +) + # ------------------------------------------------------------------------ # Comments: Trailing inline function diff --git a/crates/air_r_formatter/tests/specs/r/call.R.snap b/crates/air_r_formatter/tests/specs/r/call.R.snap index 295dceaa..a27cdda5 100644 --- a/crates/air_r_formatter/tests/specs/r/call.R.snap +++ b/crates/air_r_formatter/tests/specs/r/call.R.snap @@ -534,6 +534,18 @@ with( # own-line ) +list( + # comment0 + foo # comment1 + =1, bar # comment2 + = # comment3 + # comment4 + 2 #comment5 + #comment6 + , #comment7 + #comment8 +) + # ------------------------------------------------------------------------ # Comments: Trailing inline function @@ -1454,18 +1466,20 @@ with( with( xs, - # end-of-line - expr = { - x + 1 - } + expr = + # end-of-line + { + x + 1 + } ) with( xs, - # own-line - expr = { - x + 1 - } + expr = + # own-line + { + x + 1 + } ) with( @@ -1483,6 +1497,19 @@ with( # own-line ) +list( + # comment0 + # comment1 + foo = 1, + # comment2 + bar = + # comment3 + # comment4 + 2, #comment5 #comment7 + #comment8 + #comment6 +) + # ------------------------------------------------------------------------ # Comments: Trailing inline function @@ -1527,18 +1554,20 @@ fn( fn( xs, - # end-of-line - f = function(x) { - x + 1 - } + f = + # end-of-line + function(x) { + x + 1 + } ) fn( xs, - # own-line - f = function(x) { - x + 1 - } + f = + # own-line + function(x) { + x + 1 + } ) fn( @@ -1751,8 +1780,9 @@ c(list( )) c(list( - #foo - x = 1 + x = + #foo + 1 )) c(list( @@ -1849,8 +1879,8 @@ foo(bar[ # Lines exceeding max width of 80 characters ``` 316: my_long_list_my_long_list_my_long_list_my_long_list_long_long_long_long_long_list, - 789: foobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbazzzzzzzzzfoobarbaz - 793: c(list(foobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbazzzzzzzzzfoobarbaz())) - 796: c(list(foobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbazzzzzzzzzfoobarbaz( - 809: name = foobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbazzzzzzzzzfoobarbaz( + 806: foobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbazzzzzzzzzfoobarbaz + 810: c(list(foobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbazzzzzzzzzfoobarbaz())) + 813: c(list(foobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbazzzzzzzzzfoobarbaz( + 826: name = foobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbafoobarbazzzzzzzzzfoobarbaz( ``` diff --git a/crates/air_r_formatter/tests/specs/r/call_table.R b/crates/air_r_formatter/tests/specs/r/call_table.R index 7da8ac13..8519dcb3 100644 --- a/crates/air_r_formatter/tests/specs/r/call_table.R +++ b/crates/air_r_formatter/tests/specs/r/call_table.R @@ -122,7 +122,6 @@ list( 1,2 # comment ) -# Unfortunate: comment3 gets pulled up by Biome's Comments builder # fmt: table list( foo # comment1 diff --git a/crates/air_r_formatter/tests/specs/r/call_table.R.snap b/crates/air_r_formatter/tests/specs/r/call_table.R.snap index 6dbd26b7..61820403 100644 --- a/crates/air_r_formatter/tests/specs/r/call_table.R.snap +++ b/crates/air_r_formatter/tests/specs/r/call_table.R.snap @@ -129,7 +129,6 @@ list( 1,2 # comment ) -# Unfortunate: comment3 gets pulled up by Biome's Comments builder # fmt: table list( foo # comment1 @@ -561,14 +560,14 @@ list( 1 , 2 # comment ) -# Unfortunate: comment3 gets pulled up by Biome's Comments builder # fmt: table list( # comment1 foo = 1, - # comment3 # comment2 - bar = 2, + bar = + # comment3 + 2, ) # ------------------------------------------------------------------------ @@ -854,8 +853,8 @@ list( ## Unimplemented nodes/tokens -"(\n\"foo\n\", 2)" => 5299..5312 -"(\n{ foo }, 2\n)" => 5331..5346 +"(\n\"foo\n\", 2)" => 5238..5251 +"(\n{ foo }, 2\n)" => 5270..5285 # Lines exceeding max width of 80 characters ``` 91: foooooooooo , baaaaaaaaar , foooooooooo , baaaaaaaaar , foooooooooo , baaaaaaaaar , foooooooooo , baaaaaaaaar , foooooooooo(baaaaaaaaar, foooooooooo, baaaaaaaaar) ,