Skip to content

code_highlight() supports long strings and symbols #727

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

Merged
merged 5 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# cli (development version)

* `code_highlight()` supports long strings and symbols (#727 @moodymudskipper)

# cli 3.6.4

* Pluralization now handles edge cases (`NA`, `NaN`, `Inf` and `-Inf`)
Expand Down
42 changes: 41 additions & 1 deletion R/prettycode.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ code_highlight <- function(code, code_theme = NULL, envir = NULL) {
}

theme <- code_theme_make(code_theme)
data <- getParseData(parsed, includeText = NA)
data <- get_parse_data(parsed)

hitext <- data$text

Expand Down Expand Up @@ -125,6 +125,46 @@ code_highlight <- function(code, code_theme = NULL, envir = NULL) {
do_subst(code, data, hitext)
}

get_parse_data <- function(x) {
# getParseData(x, includeText = NA) would trim long strings and symbols
data <- getParseData(x, includeText = FALSE)
data$text <- character(nrow(data))

substr_with_tabs <- function (x, start, stop, tabsize = 8) {
widths <- rep_len(1, nchar(x))
tabs <- which(strsplit(x, "")[[1]] == "\t")
for (i in tabs) {
cols <- cumsum(widths)
widths[i] <- tabsize - (cols[i] - 1) %% tabsize
}
cols <- cumsum(widths)
start <- which(cols >= start)
if (!length(start)) {
return("")
}
start <- start[1]
stop <- which(cols <= stop)
if (length(stop)) {
stop <- stop[length(stop)]
substr(x, start, stop)
} else {
""
}
}

srcfile <- attr(data, "srcfile")
terminal <- which(data$terminal)
for (i in terminal) {
lines <- getSrcLines(srcfile, data$line1[i], data$line2[i])
n <- length(lines)
lines[n] <- substr_with_tabs(lines[n], 1, data$col2[i])
lines[1] <- substr_with_tabs(lines[1], data$col1[i], Inf)
data$text[i] <- paste(lines, collapse = "\n")
}

data
}

do_subst <- function(code, pdata, hitext) {

pdata$hitext <- hitext
Expand Down
29 changes: 29 additions & 0 deletions tests/testthat/test-prettycode.R
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,32 @@ test_that_cli(configs = "ansi", "new language features, lambda functions", {
cat(code_highlight('\\(x) x * 2', list(reserved = "bold")))
)
})

test_that("code_highlight() works on long strings and symbols", {
expect_true(
grepl(
strrep("-", 1000),
code_highlight(paste0("foo('", strrep("-", 1000), "')"))
)
)

expect_true(
grepl(
strrep("-", 1000),
code_highlight(paste0("foo(`", strrep("-", 1000), "`)"))
)
)
expect_true(
grepl(
strrep("-", 1000),
code_highlight(paste0("a$`", strrep("-", 1000), "`"))
)
)

expect_true(
grepl(
strrep("-", 1000),
code_highlight(paste0("`", strrep("-", 1000), "`$a"))
)
)
})