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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

- Air no longer emits colored output when stderr is not a terminal, such as when stderr is redirected to a file.

- `#line` directives are no longer indented, since R only reads them as directives at the start of a line (#425).

# 0.12.0

- New `--no-configuration` flag to format with Air's built-in defaults without loading a project or user configuration file (#479, @LeonidasZhak).
Expand Down
30 changes: 30 additions & 0 deletions crates/air_r_formatter/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use biome_formatter::comments::CommentTextPosition;
use biome_formatter::comments::Comments;
use biome_formatter::comments::DecoratedComment;
use biome_formatter::comments::SourceComment;
use biome_formatter::format_args;
use biome_formatter::write;
use biome_rowan::AstNode;
use biome_rowan::Direction;
Expand All @@ -41,12 +42,41 @@ impl FormatRule<SourceComment<RLanguage>> for FormatRLeadingComment {
comment: &SourceComment<RLanguage>,
f: &mut Formatter<Self::Context>,
) -> FormatResult<()> {
// A `#line` directive is only read as one when it is the first thing on
// its line, so it is written at the start of the line whatever the
// indentation around it. The line break inside the dedent doesn't add a
// line, it only resets the indentation of the line the comment starts.
if is_line_directive(comment) {
return write!(
f,
[dedent_to_root(&format_args![
hard_line_break(),
comment.piece().as_piece()
])]
);
}

// It seems like this is only worth customizing for multi-line comments
// that need to be aligned together
write!(f, [comment.piece().as_piece()])
}
}

/// Whether the comment is a `#line` directive, which maps source references in
/// generated code back to another file. R reads `#line` followed by a line
/// number as one, but only at the start of a line.
fn is_line_directive(comment: &SourceComment<RLanguage>) -> bool {
comment.lines_before() > 0
&& comment
.piece()
.text()
.strip_prefix("#line")
.is_some_and(|rest| {
rest.trim_start_matches([' ', '\t'])
.starts_with(|c: char| c.is_ascii_digit())
})
}

#[derive(Eq, PartialEq, Copy, Clone, Debug, Default)]
pub struct RCommentStyle;

Expand Down
28 changes: 28 additions & 0 deletions crates/air_r_formatter/tests/specs/r/comment_line_directive.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#line 1 "top.R"
x <- 1

{
#line 1 "file1.R"
withAutoprint({ show.srcref() })
}

f <- function() {
x
#line 10 "gen.R"
y
}

fn(
a,
#line 20 "gen.R"
b
)

{
# Comments that only look like one stay indented
#lines of text
#line up the columns
z
}

x #line 30 "gen.R"
87 changes: 87 additions & 0 deletions crates/air_r_formatter/tests/specs/r/comment_line_directive.R.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
source: crates/air_formatter_test/src/snapshot_builder.rs
info: r/comment_line_directive.R
---
# Input

```R
#line 1 "top.R"
x <- 1

{
#line 1 "file1.R"
withAutoprint({ show.srcref() })
}

f <- function() {
x
#line 10 "gen.R"
y
}

fn(
a,
#line 20 "gen.R"
b
)

{
# Comments that only look like one stay indented
#lines of text
#line up the columns
z
}

x #line 30 "gen.R"

```


=============================

# Outputs

## Output 1

-----
Indent style: Space
Indent width: 2
Line ending: LF
Line width: 80
Persistent line breaks: Respect
Assignment style: Arrow
Table: fcase, rowwiseDT, tribble
-----

```R
#line 1 "top.R"
x <- 1

{
#line 1 "file1.R"
withAutoprint({
show.srcref()
})
}

f <- function() {
x
#line 10 "gen.R"
y
}

fn(
a,
#line 20 "gen.R"
b
)

{
# Comments that only look like one stay indented
#lines of text
#line up the columns
z
}

x #line 30 "gen.R"
```