Skip to content

Keep #line directives at the start of the line - #552

Open
wleeaf wants to merge 1 commit into
posit-dev:mainfrom
wleeaf:line-directive-column-zero
Open

wleeaf wants to merge 1 commit into
posit-dev:mainfrom
wleeaf:line-directive-column-zero

Conversation

@wleeaf

@wleeaf wleeaf commented Oct 26, 2026

Copy link
Copy Markdown

Fixes #425.

R only reads #line as a directive when it's the first thing on a line, so Air indenting it turns it into an ordinary comment, and the source references it was there to set are lost. R's own tests/reg-tests-2.R has one inside a braced block (the PR#18572 regression test):

show.srcref <- function() str(sys.call())
{
#line 1 "file1.R"
    withAutoprint({ show.srcref() })
}

Air formats the directive as #line 1 "file1.R", and getParseData() on the output has no LINE_DIRECTIVE token anymore.

FormatRLeadingComment now writes a directive inside dedent_to_root, starting with a hard_line_break(). The comment already starts a line, so the line break doesn't add one. It only makes the printer take the line's indentation from inside the dedent, which puts the directive at column 0. The lines after it keep their indentation.

A comment counts as a directive the way R's lexer decides it: it's on its own line, and #line is followed by a line number, with optional spaces or tabs in between. R 4.6.1 reads #line 5 and #line5 as directives, and #line up the columns and #lines of text as comments. Other comments, and #line after code on the same line, are formatted as before. A directive that an earlier Air version indented is moved back to column 0 too, so R reads it as a directive again.

The new comment_line_directive.R spec covers a directive at the top level, in a braced block, in a function body and between arguments, plus three look-alikes. Without the change the snapshot test fails because the directives are indented. With it:

  • cargo test --workspace --exclude lsp, cargo clippy --workspace --all-targets -- -D warnings and cargo fmt -- --check pass.
  • All 26 lsp tests pass when each runs in its own process, the way nextest runs them. Under plain cargo test, the handlers_format tests hang on main as well.
  • R's parser finds the same four LINE_DIRECTIVEs in the spec's input and its formatted output.

I found this by formatting tidyverse, r-lib and data.table sources plus R's own src/library/*/R and tests (2,264 files), and comparing R's getParseData() token streams before and after. The comparison treated =/<-, ; and {} as equal. reg-tests-2.R was the only file with a difference. With this change, all 2,261 files that parse have the same tokens after formatting, and reg-tests-2.R is the only file whose output differs from main.

Timing, release builds of main and this branch. The machine was shared, so I counted instructions with callgrind:

  • 20,000 own-line comments and 20,000 trailing comments in a function body: 1,492.0M vs 1,494.0M instructions (+0.1%), with the same output.
  • 20,000 #line directives in a function body: 1,212.7M vs 1,222.7M instructions (+0.8%).
  • The 2,264 corpus files took 6.2 s vs 5.2 s wall clock (best of three), which is within the noise.

R only reads `#line` as a directive when it starts a line, so indenting
one turned it into a plain comment and dropped the source references it
sets. A comment on its own line where `#line` is followed by a line
number is now written at column 0, which is how R's lexer recognizes
a directive.

Fixes posit-dev#425.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

#line directives should not be indented

1 participant