Repository navigation
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #425.
R only reads
#lineas 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 owntests/reg-tests-2.Rhas one inside a braced block (the PR#18572 regression test):Air formats the directive as
#line 1 "file1.R", andgetParseData()on the output has noLINE_DIRECTIVEtoken anymore.FormatRLeadingCommentnow writes a directive insidededent_to_root, starting with ahard_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
#lineis followed by a line number, with optional spaces or tabs in between. R 4.6.1 reads#line 5and#line5as directives, and#line up the columnsand#lines of textas comments. Other comments, and#lineafter 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.Rspec 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 warningsandcargo fmt -- --checkpass.lsptests pass when each runs in its own process, the way nextest runs them. Under plaincargo test, thehandlers_formattests hang onmainas well.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/*/Randtests(2,264 files), and comparing R'sgetParseData()token streams before and after. The comparison treated=/<-,;and{}as equal.reg-tests-2.Rwas the only file with a difference. With this change, all 2,261 files that parse have the same tokens after formatting, andreg-tests-2.Ris the only file whose output differs frommain.Timing, release builds of
mainand this branch. The machine was shared, so I counted instructions with callgrind:#linedirectives in a function body: 1,212.7M vs 1,222.7M instructions (+0.8%).