Skip to content
Merged
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
53 changes: 46 additions & 7 deletions crates/air_r_formatter/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,55 @@ fn handle_empty_statement_comment(

match comment.text_position() {
CommentTextPosition::EndOfLine => match (preceding, following) {
(Some(preceding), Some(following)) => {
// Is the comment on the same line as the closest non-empty `preceding` node?
let is_on_preceding_line = comment.preceding_node().is_some_and(|preceding| {
preceding
.siblings(Direction::Prev)
.take_while(|sibling| REmptyStatement::can_cast(sibling.kind()))
.all(|sibling| !sibling.has_leading_newline())
});

if is_on_preceding_line {
// ```r
// {
// 1; ; # Trails preceding
// 2
// }
// ```
CommentPlacement::trailing(preceding, comment)
} else {
// Leading keeps the relative ordering here:
//
// ```r
// {
// 1
// # Leads following 1
// ; # Leads following 2
// 2
// }
// ```
//
// And generally also makes sense here where the `;` is
// simply removed and the comment doesn't move
//
// ```r
// {
// 1
// ; # Leads following
// 2
// }
//
// ```
CommentPlacement::leading(following, comment)
}
}

// ```r
// {
// 1
// ; # Trails preceding
// 2
// 1; ; # Trails preceding
// }
// ```
(Some(preceding), Some(_)) => CommentPlacement::trailing(preceding, comment),

// ```r
//
// {
// 1
// ; # Trails preceding
Expand Down
148 changes: 145 additions & 3 deletions crates/air_r_formatter/src/r/auxiliary/empty_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,51 @@ mod test {
Ok(())
}

#[test]
fn test_comments_around_lone_semicolon_keep_order() -> anyhow::Result<()> {
let source = "\
{
1
# a
; # b
2
}";
let expected = "\
{
1
# a
# b
2
}
";
let tree = root([statement(braces(
token(L_CURLY, &[], "{", &[]),
[
statement(double(token(
R_DOUBLE_LITERAL,
&[Newline, INDENT],
"1",
&[],
))),
empty(token(
SEMICOLON,
&[Newline, INDENT, Comment("# a"), Newline, INDENT],
";",
&[SPACE, Comment("# b")],
)),
statement(double(token(
R_DOUBLE_LITERAL,
&[Newline, INDENT],
"2",
&[],
))),
],
token(R_CURLY, &[Newline], "}", &[]),
))]);
assert_eq!(format(tree, source)?, expected);
Ok(())
}

/// Handles every variant of [crate::comments::handle_empty_statement_comment]
#[test]
fn test_handle_empty_statement_comment() -> anyhow::Result<()> {
Expand All @@ -538,15 +583,77 @@ mod test {

let source = "\
{
1
; # Trails preceding
1; ; # Trails preceding
2
}";
let expected = "\
{
1 # Trails preceding
2
}
";
let tree = root([statement(braces(
token(L_CURLY, &[], "{", &[]),
[
statement_with_semicolon(
double(token(R_DOUBLE_LITERAL, &[Newline, INDENT], "1", &[])),
token(SEMICOLON, &[], ";", &[]),
),
empty(token(SEMICOLON, &[SPACE], ";", &[SPACE, TRAILS])),
statement(double(token(
R_DOUBLE_LITERAL,
&[Newline, INDENT],
"2",
&[],
))),
],
token(R_CURLY, &[Newline], "}", &[]),
))]);
assert_eq!(format(tree, source)?, expected);

let source = "\
{
1; ; ; # Trails preceding
2
}";
let expected = "\
{
1 # Trails preceding
2
}
";
let tree = root([statement(braces(
token(L_CURLY, &[], "{", &[]),
[
statement_with_semicolon(
double(token(R_DOUBLE_LITERAL, &[Newline, INDENT], "1", &[])),
token(SEMICOLON, &[], ";", &[]),
),
empty(token(SEMICOLON, &[SPACE], ";", &[])),
empty(token(SEMICOLON, &[SPACE], ";", &[SPACE, TRAILS])),
statement(double(token(
R_DOUBLE_LITERAL,
&[Newline, INDENT],
"2",
&[],
))),
],
token(R_CURLY, &[Newline], "}", &[]),
))]);
assert_eq!(format(tree, source)?, expected);

let source = "\
{
1
; ; # Leads following
2
}";
let expected = "\
{
1
# Leads following
2
}
";
let tree = root([statement(braces(
token(L_CURLY, &[], "{", &[]),
Expand All @@ -557,7 +664,42 @@ mod test {
"1",
&[],
))),
empty(token(SEMICOLON, &[Newline, INDENT], ";", &[SPACE, TRAILS])),
empty(token(SEMICOLON, &[Newline, INDENT], ";", &[])),
empty(token(SEMICOLON, &[SPACE], ";", &[SPACE, LEADS])),
statement(double(token(
R_DOUBLE_LITERAL,
&[Newline, INDENT],
"2",
&[],
))),
],
token(R_CURLY, &[Newline], "}", &[]),
))]);
assert_eq!(format(tree, source)?, expected);

let source = "\
{
1
; # Leads following
2
}";
let expected = "\
{
1
# Leads following
2
}
";
let tree = root([statement(braces(
token(L_CURLY, &[], "{", &[]),
[
statement(double(token(
R_DOUBLE_LITERAL,
&[Newline, INDENT],
"1",
&[],
))),
empty(token(SEMICOLON, &[Newline, INDENT], ";", &[SPACE, LEADS])),
statement(double(token(
R_DOUBLE_LITERAL,
&[Newline, INDENT],
Expand Down
Loading