diff --git a/crates/air_r_formatter/src/comments_ext.rs b/crates/air_r_formatter/src/comments_ext.rs index 2b275f11..b5b7fc3f 100644 --- a/crates/air_r_formatter/src/comments_ext.rs +++ b/crates/air_r_formatter/src/comments_ext.rs @@ -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 { - // We intentionally only consider directives in leading comments. This is a - // departure from Biome (and Ruff?). + let matches = |c: &biome_formatter::comments::SourceComment| { + 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 { @@ -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 ) } diff --git a/crates/air_r_formatter/tests/quick_test.rs b/crates/air_r_formatter/tests/quick_test.rs index 3c1c0b89..6376e0cd 100644 --- a/crates/air_r_formatter/tests/quick_test.rs +++ b/crates/air_r_formatter/tests/quick_test.rs @@ -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 ) "#; 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..505b7897 100644 --- a/crates/air_r_formatter/tests/specs/r/call_table.R +++ b/crates/air_r_formatter/tests/specs/r/call_table.R @@ -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 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..6c7e8d87 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 @@ -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 @@ -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 @@ -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) ,