Skip to content
Open
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
29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).


Expand Down
6 changes: 5 additions & 1 deletion crates/air_r_formatter/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,11 @@ fn handle_argument_comment(comment: DecoratedComment<RLanguage>) -> 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);
}
}
}

Expand Down
22 changes: 21 additions & 1 deletion crates/air_r_formatter/src/r/auxiliary/argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()])
}
}
}
}
12 changes: 12 additions & 0 deletions crates/air_r_formatter/tests/specs/r/call.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
74 changes: 52 additions & 22 deletions crates/air_r_formatter/tests/specs/r/call.R.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -1751,8 +1780,9 @@ c(list(
))

c(list(
#foo
x = 1
x =
#foo
1
))

c(list(
Expand Down Expand Up @@ -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(
```
1 change: 0 additions & 1 deletion crates/air_r_formatter/tests/specs/r/call_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ list(
1,2 # comment
)

# Unfortunate: comment3 gets pulled up by Biome's Comments builder
# fmt: table
list(
foo # comment1
Expand Down
11 changes: 5 additions & 6 deletions crates/air_r_formatter/tests/specs/r/call_table.R.snap
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ list(
1,2 # comment
)

# Unfortunate: comment3 gets pulled up by Biome's Comments builder
# fmt: table
list(
foo # comment1
Expand Down Expand Up @@ -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,
)

# ------------------------------------------------------------------------
Expand Down Expand Up @@ -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) ,
Expand Down