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

Add new R pipe support #127

Merged
merged 12 commits into from
Jun 11, 2022
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# downlit (development version)

* Adds support for new R pipe `|>` syntax (#126).

* Very long strings or other tokens are no longer truncated
by `downlit::highlight()` (@dmurdoch, #128).

Expand Down
8 changes: 7 additions & 1 deletion R/highlight.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ token_type <- function(x, text) {
# assignment / equals
"LEFT_ASSIGN", "RIGHT_ASSIGN", "EQ_ASSIGN", "EQ_FORMALS", "EQ_SUB",
# miscellaneous
"'$'", "'@'","'~'", "'?'", "':'", "SPECIAL"
"'$'", "'@'","'~'", "'?'", "':'", "SPECIAL",
# pipes
"PIPE", "PIPEBIND"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant you should do the transformation in this function, so we only need to do it in one place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok. I was under the impression you did associate some token classes from R parsed data to some chroma or pandoc classes directly. So I went with that for this one too as token_type() seemed to handle special grouping of some token type in infix, special, constant and parens groups.

I'll adapt

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did the change, hopefully I got it right. I did not went with that originally because you went with separate attributions for other types by duplicating

downlit/R/highlight.R

Lines 228 to 230 in 8434f36

"SLOT" = "va",
"SYMBOL" = "va",
"SYMBOL_FORMALS" = "va",

downlit/R/highlight.R

Lines 254 to 256 in 8434f36

"SLOT" = "nv",
"SYMBOL" = "nv",
"SYMBOL_FORMALS" = "nv",

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, that's reasonable read — I think this is different because PLACEHOLDER is only a single token so it makes more sense to lump it into one of the other classes.

)
x[x %in% infix] <- "infix"

Expand All @@ -200,6 +202,10 @@ token_type <- function(x, text) {
x[x == "NUM_CONST" & text %in% constant] <- "constant"
x[x == "SYMBOL" & text %in% c("T", "F")] <- "constant"
x[x == "NULL_CONST"] <- "constant"
x[x == "NULL_CONST"] <- "constant"

# Treats pipe's placeholder '_' as a SYMBOL
x[x == "PLACEHOLDER"] <- "SYMBOL"

x
}
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/_snaps/highlight.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@

[1] "<span class='c'># <span style='color: #BB0000;'>hello</span></span>"

# placeholder in R pipe gets highlighted and not linked

Code
highlight("1:10 |> mean(x = _)", classes = classes_pandoc())
Output
[1] "<span class='fl'>1</span><span class='op'>:</span><span class='fl'>10</span> <span class='op'>|&gt;</span> <span class='fu'><a href='https://rdrr.io/r/base/mean.html'>mean</a></span><span class='op'>(</span>x <span class='op'>=</span> <span class='va'>_</span><span class='op'>)</span>"

5 changes: 5 additions & 0 deletions tests/testthat/test-highlight.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,8 @@ test_that("can highlight vers long strings", {
out <- downlit::highlight(sprintf("'%s'", val))
expect_equal(out, paste0("<span class='s'>'", val, "'</span>"))
})

test_that("placeholder in R pipe gets highlighted and not linked", {
skip_if_not(getRversion() >= 4.2, message = "Pipes are available from R 4.1")
expect_snapshot(highlight("1:10 |> mean(x = _)", classes = classes_pandoc()))
})