From db888be0ed36c3435d07eac74de4dc62b080af60 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Tue, 1 Feb 2022 11:24:53 +0100 Subject: [PATCH 01/11] Add new R pipe support token types are PIPE and PIPEBIND --- R/highlight.R | 4 +++- tests/testthat/test-highlight.R | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/R/highlight.R b/R/highlight.R index b6afbc4..8573de6 100644 --- a/R/highlight.R +++ b/R/highlight.R @@ -168,7 +168,9 @@ token_type <- function(x, text) { # assignment / equals "LEFT_ASSIGN", "RIGHT_ASSIGN", "EQ_ASSIGN", "EQ_FORMALS", "EQ_SUB", # miscellaneous - "'$'", "'@'","'~'", "'?'", "':'", "SPECIAL" + "'$'", "'@'","'~'", "'?'", "':'", "SPECIAL", + # pipes + "PIPE", "PIPEBIND" ) x[x %in% infix] <- "infix" diff --git a/tests/testthat/test-highlight.R b/tests/testthat/test-highlight.R index 35a4fb3..bc4a3dc 100644 --- a/tests/testthat/test-highlight.R +++ b/tests/testthat/test-highlight.R @@ -109,3 +109,16 @@ test_that("ansi escapes are converted to html", { expect_snapshot_output(highlight("# \033[31mhello\033[m")) expect_snapshot_output(highlight("# \u2029[31mhello\u2029[m")) }) + +test_that("New R pipes get highlighted and not linked", { + skip_if_not(getRversion() > 4.1, message = "Pipes are available from R 4.1") + expect_equal( + highlight("1 |> fun()"), + "1 |> fun()" + ) + withr::local_envvar(list("_R_USE_PIPEBIND_" = TRUE)) + expect_equal( + highlight("x => fun(2, x)"), + "x => fun(2, x)" + ) +}) From c4f245b8f814eb99e372398139039765452c2cd7 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Tue, 1 Feb 2022 11:26:22 +0100 Subject: [PATCH 02/11] Add NEWS bullet --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.md b/NEWS.md index 2f4e0b8..566f644 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # downlit (development version) +* New R pipe `|>` is now highlighted correctly as an infix operator like + magrittr's `%>%` (#126). + # downlit 0.4.0 ## Syntax highlighting From 0fe0a94f30b0e6fab61a2dc78dbd4d0e99fa8c60 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Tue, 1 Feb 2022 11:39:20 +0100 Subject: [PATCH 03/11] withr is not in suggest yet. --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index 85e66ea..2f13cc8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -34,6 +34,7 @@ Suggests: pkgload, rmarkdown, testthat (>= 3.0.0), + withr (>= 2.4.3), xml2 Config/testthat/edition: 3 Encoding: UTF-8 From b4148504341719f5044d822b981f78c836c55eab Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Tue, 1 Feb 2022 13:28:38 +0100 Subject: [PATCH 04/11] check highlight of 2 pipes in one step --- tests/testthat/test-highlight.R | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/testthat/test-highlight.R b/tests/testthat/test-highlight.R index bc4a3dc..01c5137 100644 --- a/tests/testthat/test-highlight.R +++ b/tests/testthat/test-highlight.R @@ -112,13 +112,9 @@ test_that("ansi escapes are converted to html", { test_that("New R pipes get highlighted and not linked", { skip_if_not(getRversion() > 4.1, message = "Pipes are available from R 4.1") - expect_equal( - highlight("1 |> fun()"), - "1 |> fun()" - ) withr::local_envvar(list("_R_USE_PIPEBIND_" = TRUE)) expect_equal( - highlight("x => fun(2, x)"), - "x => fun(2, x)" + highlight("1 |> x => fun(2, x)"), + "1 |> x => fun(2, x)" ) }) From 6234c10a3a188c4dbc871d45f7b74cce80a41dc2 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Wed, 9 Mar 2022 16:58:16 +0100 Subject: [PATCH 05/11] Add support for placeholder in R pipe syntax --- NEWS.md | 3 +-- R/highlight.R | 2 +- tests/testthat/test-highlight.R | 10 +++++++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/NEWS.md b/NEWS.md index 566f644..d43108a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,6 @@ # downlit (development version) -* New R pipe `|>` is now highlighted correctly as an infix operator like - magrittr's `%>%` (#126). +* Adds support for new R pipe `|>` syntax (#126). # downlit 0.4.0 diff --git a/R/highlight.R b/R/highlight.R index 8573de6..008536a 100644 --- a/R/highlight.R +++ b/R/highlight.R @@ -170,7 +170,7 @@ token_type <- function(x, text) { # miscellaneous "'$'", "'@'","'~'", "'?'", "':'", "SPECIAL", # pipes - "PIPE", "PIPEBIND" + "PIPE", "PIPEBIND", "PLACEHOLDER" ) x[x %in% infix] <- "infix" diff --git a/tests/testthat/test-highlight.R b/tests/testthat/test-highlight.R index 01c5137..f4b4c87 100644 --- a/tests/testthat/test-highlight.R +++ b/tests/testthat/test-highlight.R @@ -111,10 +111,18 @@ test_that("ansi escapes are converted to html", { }) test_that("New R pipes get highlighted and not linked", { - skip_if_not(getRversion() > 4.1, message = "Pipes are available from R 4.1") + skip_if_not(getRversion() >= 4.1, message = "Pipes are available from R 4.1") withr::local_envvar(list("_R_USE_PIPEBIND_" = TRUE)) expect_equal( highlight("1 |> x => fun(2, x)"), "1 |> x => fun(2, x)" ) }) + +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_equal( + highlight("1:10 |> mean(x = _)"), + "1:10 |> mean(x = _)" + ) +}) From bd6b346e8bc530be24426e8b8c36bf39a69806a4 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Wed, 9 Mar 2022 17:00:13 +0100 Subject: [PATCH 06/11] use snapshot test instead of writing long HTML line in R file --- tests/testthat/_snaps/highlight.md | 14 ++++++++++++++ tests/testthat/test-highlight.R | 10 ++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/testthat/_snaps/highlight.md b/tests/testthat/_snaps/highlight.md index c09023d..cd94121 100644 --- a/tests/testthat/_snaps/highlight.md +++ b/tests/testthat/_snaps/highlight.md @@ -14,3 +14,17 @@ [1] "# hello" +# New R pipes get highlighted and not linked + + Code + highlight("1 |> x => fun(2, x)") + Output + [1] "1 |> x => fun(2, x)" + +# placeholder in R pipe gets highlighted and not linked + + Code + highlight("1:10 |> mean(x = _)") + Output + [1] "1:10 |> mean(x = _)" + diff --git a/tests/testthat/test-highlight.R b/tests/testthat/test-highlight.R index f4b4c87..fa394c4 100644 --- a/tests/testthat/test-highlight.R +++ b/tests/testthat/test-highlight.R @@ -113,16 +113,10 @@ test_that("ansi escapes are converted to html", { test_that("New R pipes get highlighted and not linked", { skip_if_not(getRversion() >= 4.1, message = "Pipes are available from R 4.1") withr::local_envvar(list("_R_USE_PIPEBIND_" = TRUE)) - expect_equal( - highlight("1 |> x => fun(2, x)"), - "1 |> x => fun(2, x)" - ) + expect_snapshot(highlight("1 |> x => fun(2, x)")) }) 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_equal( - highlight("1:10 |> mean(x = _)"), - "1:10 |> mean(x = _)" - ) + expect_snapshot(highlight("1:10 |> mean(x = _)")) }) From 094bae388be34bc14118f0f44931e09f6ef91bdb Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Fri, 10 Jun 2022 10:29:12 +0200 Subject: [PATCH 07/11] Leave out PIPEBIND test --- tests/testthat/_snaps/highlight.md | 7 ------- tests/testthat/test-highlight.R | 6 ------ 2 files changed, 13 deletions(-) diff --git a/tests/testthat/_snaps/highlight.md b/tests/testthat/_snaps/highlight.md index cd94121..77bc69f 100644 --- a/tests/testthat/_snaps/highlight.md +++ b/tests/testthat/_snaps/highlight.md @@ -14,13 +14,6 @@ [1] "# hello" -# New R pipes get highlighted and not linked - - Code - highlight("1 |> x => fun(2, x)") - Output - [1] "1 |> x => fun(2, x)" - # placeholder in R pipe gets highlighted and not linked Code diff --git a/tests/testthat/test-highlight.R b/tests/testthat/test-highlight.R index fa394c4..c154a25 100644 --- a/tests/testthat/test-highlight.R +++ b/tests/testthat/test-highlight.R @@ -110,12 +110,6 @@ test_that("ansi escapes are converted to html", { expect_snapshot_output(highlight("# \u2029[31mhello\u2029[m")) }) -test_that("New R pipes get highlighted and not linked", { - skip_if_not(getRversion() >= 4.1, message = "Pipes are available from R 4.1") - withr::local_envvar(list("_R_USE_PIPEBIND_" = TRUE)) - expect_snapshot(highlight("1 |> x => fun(2, x)")) -}) - 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 = _)")) From 2259b07b3f849c1dc46a8027d0a1a4cb3454317b Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Fri, 10 Jun 2022 10:46:15 +0200 Subject: [PATCH 08/11] associate PIPE with special instead of infix --- R/highlight.R | 5 +++-- tests/testthat/_snaps/highlight.md | 4 ++-- tests/testthat/test-highlight.R | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/R/highlight.R b/R/highlight.R index 008536a..b94c323 100644 --- a/R/highlight.R +++ b/R/highlight.R @@ -147,7 +147,7 @@ token_type <- function(x, text) { special <- c( "FUNCTION", "FOR", "IN", "BREAK", "NEXT", "REPEAT", "WHILE", - "IF", "ELSE" + "IF", "ELSE", "PLACEHOLDER" ) rstudio_special <- c( "return", "switch", "try", "tryCatch", "stop", @@ -170,7 +170,7 @@ token_type <- function(x, text) { # miscellaneous "'$'", "'@'","'~'", "'?'", "':'", "SPECIAL", # pipes - "PIPE", "PIPEBIND", "PLACEHOLDER" + "PIPE", "PIPEBIND" ) x[x %in% infix] <- "infix" @@ -185,6 +185,7 @@ 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" x } diff --git a/tests/testthat/_snaps/highlight.md b/tests/testthat/_snaps/highlight.md index 77bc69f..76c9806 100644 --- a/tests/testthat/_snaps/highlight.md +++ b/tests/testthat/_snaps/highlight.md @@ -17,7 +17,7 @@ # placeholder in R pipe gets highlighted and not linked Code - highlight("1:10 |> mean(x = _)") + highlight("1:10 |> mean(x = _)", classes = classes_pandoc()) Output - [1] "1:10 |> mean(x = _)" + [1] "1:10 |> mean(x = _)" diff --git a/tests/testthat/test-highlight.R b/tests/testthat/test-highlight.R index c154a25..22f9a4f 100644 --- a/tests/testthat/test-highlight.R +++ b/tests/testthat/test-highlight.R @@ -112,5 +112,5 @@ test_that("ansi escapes are converted to html", { 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 = _)")) + expect_snapshot(highlight("1:10 |> mean(x = _)", classes = classes_pandoc())) }) From a43735d56368d1b769e961120813dd55226b99fd Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Fri, 10 Jun 2022 11:31:51 +0200 Subject: [PATCH 09/11] withr is not needed anymore and already in import now anyway --- DESCRIPTION | 1 - 1 file changed, 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 588a9a4..4acbef8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -34,7 +34,6 @@ Suggests: pkgload, rmarkdown, testthat (>= 3.0.0), - withr (>= 2.4.3), xml2 Config/testthat/edition: 3 Encoding: UTF-8 From c4d48945653912f3b99b62adeb0301780b588b82 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Fri, 10 Jun 2022 16:22:50 +0200 Subject: [PATCH 10/11] Treat PLACEHOLDER as SYMBOL and set dsVariable class --- R/highlight.R | 4 +++- tests/testthat/_snaps/highlight.md | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/R/highlight.R b/R/highlight.R index 92366c2..9d758d1 100644 --- a/R/highlight.R +++ b/R/highlight.R @@ -164,7 +164,7 @@ token_type <- function(x, text) { special <- c( "FUNCTION", "FOR", "IN", "BREAK", "NEXT", "REPEAT", "WHILE", - "IF", "ELSE", "PLACEHOLDER" + "IF", "ELSE" ) rstudio_special <- c( "return", "switch", "try", "tryCatch", "stop", @@ -231,6 +231,7 @@ classes_pandoc <- function() { "SLOT" = "va", "SYMBOL" = "va", "SYMBOL_FORMALS" = "va", + "PLACEHOLDER" = "va", "NS_GET" = "fu", "NS_GET_INT" = "fu", @@ -257,6 +258,7 @@ classes_chroma <- function() { "SLOT" = "nv", "SYMBOL" = "nv", "SYMBOL_FORMALS" = "nv", + "PLACEHOLDER" = "nv", "NS_GET" = "nf", "NS_GET_INT" = "nf", diff --git a/tests/testthat/_snaps/highlight.md b/tests/testthat/_snaps/highlight.md index 76c9806..873b73c 100644 --- a/tests/testthat/_snaps/highlight.md +++ b/tests/testthat/_snaps/highlight.md @@ -19,5 +19,5 @@ Code highlight("1:10 |> mean(x = _)", classes = classes_pandoc()) Output - [1] "1:10 |> mean(x = _)" + [1] "1:10 |> mean(x = _)" From c37e45a711d61b76acd5517fe8e87a29a0502ac1 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Fri, 10 Jun 2022 21:07:45 +0200 Subject: [PATCH 11/11] Do the transformatin in `token_type()` directly --- R/highlight.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/highlight.R b/R/highlight.R index 9d758d1..56d899f 100644 --- a/R/highlight.R +++ b/R/highlight.R @@ -204,6 +204,9 @@ token_type <- function(x, text) { x[x == "NULL_CONST"] <- "constant" x[x == "NULL_CONST"] <- "constant" + # Treats pipe's placeholder '_' as a SYMBOL + x[x == "PLACEHOLDER"] <- "SYMBOL" + x } @@ -231,7 +234,6 @@ classes_pandoc <- function() { "SLOT" = "va", "SYMBOL" = "va", "SYMBOL_FORMALS" = "va", - "PLACEHOLDER" = "va", "NS_GET" = "fu", "NS_GET_INT" = "fu", @@ -258,7 +260,6 @@ classes_chroma <- function() { "SLOT" = "nv", "SYMBOL" = "nv", "SYMBOL_FORMALS" = "nv", - "PLACEHOLDER" = "nv", "NS_GET" = "nf", "NS_GET_INT" = "nf",