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
24 changes: 17 additions & 7 deletions crates/air_r_formatter/src/comments_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,25 @@ impl CommentsExt for RComments {
}
}

// We intentionally only consider directives in leading comments. This is a
// departure from Biome (and Ruff?).
fn directives(comments: &RComments, node: &RSyntaxNode) -> impl Iterator<Item = Directive> {
// We intentionally only consider directives in leading comments. This is a
// departure from Biome (and Ruff?).
let matches = |c: &biome_formatter::comments::SourceComment<RLanguage>| {
comments::parse_comment_directive(c.piece().text())
};

// In argument lists, comments get attached to `R_ARGUMENT` nodes. So check
// there for directives.
let parent = node.parent();
let target_node = parent
.as_ref()
.filter(|p| p.kind() == RSyntaxKind::R_ARGUMENT)
.unwrap_or(node);

comments
.leading_comments(node)
.leading_comments(target_node)
.iter()
.filter_map(|c| comments::parse_comment_directive(c.piece().text()))
.filter_map(matches)
}

fn can_have_directive(node: &RSyntaxNode) -> bool {
Expand All @@ -66,9 +78,7 @@ fn can_have_directive(node: &RSyntaxNode) -> bool {
// - Binary expression sides (like the RHS of a pipe chain)
matches!(
parent.kind(),
RSyntaxKind::R_EXPRESSION_LIST
| RSyntaxKind::R_ARGUMENT_LIST
| RSyntaxKind::R_BINARY_EXPRESSION
RSyntaxKind::R_EXPRESSION_LIST | RSyntaxKind::R_ARGUMENT | RSyntaxKind::R_BINARY_EXPRESSION
)
}

Expand Down
17 changes: 8 additions & 9 deletions crates/air_r_formatter/tests/quick_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ mod language {
#[test]
fn quick_test() {
let src = r#"
# fmt: tabular
tribble(
~quarter, ~region, ~product, ~price, ~units_sold,
"Q1", "North", "Laptop", 1499.99, 250,
"Q1", "North", "Tablet", 85.5, 340,

# fmt: tabular off
"Q2", "South", "Laptop", 989.0, 196,
"Q2", "South", "Tablet", 76.95, 304,
matrix(
data =
# fmt: table
c(
1, 2,
10, 200
),
ncol = 2
)
"#;

Expand Down
23 changes: 23 additions & 0 deletions crates/air_r_formatter/tests/specs/r/call_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,29 @@ foooooooooo,baaaaaaaaar,foooooooooo,baaaaaaaaar,foooooooooo,baaaaaaaaar,fooooooo
1+100, 1+1000,
)

# ------------------------------------------------------------------------
# Comment directives

# In argument
x <- matrix(
# fmt: table
c(
1, 2,
10, 200
),
ncol = 2
)

# In argument with name
matrix(
# fmt: table
data = c(
1, 2,
10, 200
),
ncol = 2
)

# ------------------------------------------------------------------------
# Comments

Expand Down
50 changes: 48 additions & 2 deletions crates/air_r_formatter/tests/specs/r/call_table.R.snap
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,29 @@ foooooooooo,baaaaaaaaar,foooooooooo,baaaaaaaaar,foooooooooo,baaaaaaaaar,fooooooo
1+100, 1+1000,
)

# ------------------------------------------------------------------------
# Comment directives

# In argument
x <- matrix(
# fmt: table
c(
1, 2,
10, 200
),
ncol = 2
)

# In argument with name
matrix(
# fmt: table
data = c(
1, 2,
10, 200
),
ncol = 2
)

# ------------------------------------------------------------------------
# Comments

Expand Down Expand Up @@ -528,6 +551,29 @@ list(
1 + 100 , 1 + 1000 ,
)

# ------------------------------------------------------------------------
# Comment directives

# In argument
x <- matrix(
# fmt: table
c(
1 , 2 ,
10 , 200
),
ncol = 2
)

# In argument with name
matrix(
# fmt: table
data = c(
1 , 2 ,
10 , 200
),
ncol = 2
)

# ------------------------------------------------------------------------
# Comments

Expand Down Expand Up @@ -854,8 +900,8 @@ list(

## Unimplemented nodes/tokens

"(\n\"foo\n\", 2)" => 5299..5312
"(\n{ foo }, 2\n)" => 5331..5346
"(\n\"foo\n\", 2)" => 5596..5609
"(\n{ foo }, 2\n)" => 5628..5643
# Lines exceeding max width of 80 characters
```
91: foooooooooo , baaaaaaaaar , foooooooooo , baaaaaaaaar , foooooooooo , baaaaaaaaar , foooooooooo , baaaaaaaaar , foooooooooo(baaaaaaaaar, foooooooooo, baaaaaaaaar) ,
Expand Down