Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is special support for line directives needed? #160

Open
EagleoutIce opened this issue Nov 21, 2024 · 1 comment
Open

Is special support for line directives needed? #160

EagleoutIce opened this issue Nov 21, 2024 · 1 comment

Comments

@EagleoutIce
Copy link

EagleoutIce commented Nov 21, 2024

Similar to #68, highlighting line directives such as #line 42 example.R instead of parsing them simply as (program (comment)) could be beneficial.
The default R parser handles it as a separate LINE_DIRECTIVE token:

> getParseData(parse(text="#line 42 example.R"))
  line1 col1 line2 col2 id parent          token terminal               text
1     1    1     1   18  1      0 LINE_DIRECTIVE     TRUE #line 42 example.R
@DavisVaughan
Copy link
Member

Mayyyyybe. They aren't used that frequently from what I've seen.

We would probably want to nest them within comment somehow if we did this, like (program (comment (line_directive <fields>))) so that people can iterate over comments and have line directives be included by default, which seems like what you'd want. And you can just skip them (by recognizing the node kind of the child) if you care about handling them separately.

I'm more convinced about including this than roxygen2 support, since this is literally built into the language, but I'm not sure the very small benefit is worth the effort / headache.

You could also use a query to locate them today, but you'd have to do some additional work to make sure they are at the 1st column of the line, and match the correct form of a line directive

query <- '
(
  (comment) @comment
  (#match? @comment "^#line")
)
'

language <- treesitter.r::language()
query <- treesitter::query(language, query)

text <- "
1 + 1
# comment
#line 43 example.R
2 + 2
# comment2
#line 45 example2.R
"

node <- treesitter::text_parse(text, language)

captures <- treesitter::query_captures(query, node)

captures$node
#> [[1]]
#> <tree_sitter_node>
#> 
#> ── Text ────────────────────────────────────────────────────────────────────────
#> #line 43 example.R
#> 
#> ── S-Expression ────────────────────────────────────────────────────────────────
#> (comment [(3, 0), (3, 18)])
#> 
#> [[2]]
#> <tree_sitter_node>
#> 
#> ── Text ────────────────────────────────────────────────────────────────────────
#> #line 45 example2.R
#> 
#> ── S-Expression ────────────────────────────────────────────────────────────────
#> (comment [(6, 0), (6, 19)])

https://github.com/wch/r-source/blob/0a55f06ce3b46c4fd8d3c85b50c4ccb94d7653ef/src/main/gram.y#L2528-L2546
https://github.com/wch/r-source/blob/0a55f06ce3b46c4fd8d3c85b50c4ccb94d7653ef/src/main/gram.y#L3467-L3487

A few notes

  • It has to start at column 0 in the row (which may be hard with tree-sitter, although the external scanner does have get_column() but its not free)
  • It leads with #line and has to have a digit, but seems like the file name is optional
  • The file name could be quoted in "

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

No branches or pull requests

2 participants