From af324edee2a09e8de804ee87b12d10bf87c15639 Mon Sep 17 00:00:00 2001 From: Luis Verde Arregoitia Date: Thu, 17 Sep 2026 10:40:20 -0600 Subject: [PATCH] Highlight = in args, namespace operators, and package calls Treat EQ_FORMALS and EQ_SUB (the = in function formals and call arguments) and the namespace operators :: and ::: as operators, and style the package name in pkg::fun() with the call style. Fixes #15 --- NEWS.md | 3 +++ R/highlight.R | 10 +++++++--- tests/testthat/test-highlight.R | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index 082a444..4baf551 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,9 @@ * Printing primitive functions excludes their `NULL` environment (#11 @mdequeljoe) * `highlight()` supports long strings and symbols (#21 @moodymudskipper) +* `highlight()` now styles `=` in function formals and call arguments as an + operator, the namespace operators `::` and `:::` as operators, and the + package name in `pkg::fun()` as a call (#15) # prettycode 1.1.0 diff --git a/R/highlight.R b/R/highlight.R index 8c76728..d20d9d3 100644 --- a/R/highlight.R +++ b/R/highlight.R @@ -23,7 +23,11 @@ operator_tokens <- function() { "RIGHT_ASSIGN", "'$'", "'@'", - "EQ_ASSIGN" + "EQ_ASSIGN", + "EQ_FORMALS", + "EQ_SUB", + "NS_GET", + "NS_GET_INT" ) } @@ -97,9 +101,9 @@ highlight <- function(code, style = default_style()) { hitext[operator] <- style$operator(data$text[operator]) } - ## Function calls + ## Function calls, including the package name in `pkg::fun()` if (!is.null(style$call)) { - fun_call <- data$token == "SYMBOL_FUNCTION_CALL" + fun_call <- data$token %in% c("SYMBOL_FUNCTION_CALL", "SYMBOL_PACKAGE") hitext[fun_call] <- style$call(data$text[fun_call]) } diff --git a/tests/testthat/test-highlight.R b/tests/testthat/test-highlight.R index 5ff65d0..12994cd 100644 --- a/tests/testthat/test-highlight.R +++ b/tests/testthat/test-highlight.R @@ -78,10 +78,28 @@ test_that("operator", { ), "a OP 10; 20 OP b; c OP 30; aOPb; aOPb" ) + + expect_equal( + highlight( + "function(var = value) f(x = 1)", + list(operator = function(x) "OP") + ), + "function(var OP value) f(x OP 1)" + ) + + expect_equal( + highlight("a::b + a:::b", list(operator = function(x) "OP")), + "aOPb OP aOPb" + ) }) test_that("call", { expect_equal(highlight("ls(2)", list(call = function(x) "F")), "F(2)") + + expect_equal( + highlight("crayon::make_style(2)", list(call = function(x) "F")), + "F::F(2)" + ) }) test_that("string", {