Skip to content

Commit 59e3586

Browse files
authored
Merge pull request #727 from moodymudskipper/fix/long-strings-and-symbols
2 parents adb882a + 3349537 commit 59e3586

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# cli (development version)
22

3+
* `code_highlight()` supports long strings and symbols (#727 @moodymudskipper)
4+
35
# cli 3.6.4
46

57
* Pluralization now handles edge cases (`NA`, `NaN`, `Inf` and `-Inf`)

R/prettycode.R

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ code_highlight <- function(code, code_theme = NULL, envir = NULL) {
5555
}
5656

5757
theme <- code_theme_make(code_theme)
58-
data <- getParseData(parsed, includeText = NA)
58+
data <- get_parse_data(parsed)
5959

6060
hitext <- data$text
6161

@@ -125,6 +125,46 @@ code_highlight <- function(code, code_theme = NULL, envir = NULL) {
125125
do_subst(code, data, hitext)
126126
}
127127

128+
get_parse_data <- function(x) {
129+
# getParseData(x, includeText = NA) would trim long strings and symbols
130+
data <- getParseData(x, includeText = FALSE)
131+
data$text <- character(nrow(data))
132+
133+
substr_with_tabs <- function (x, start, stop, tabsize = 8) {
134+
widths <- rep_len(1, nchar(x))
135+
tabs <- which(strsplit(x, "")[[1]] == "\t")
136+
for (i in tabs) {
137+
cols <- cumsum(widths)
138+
widths[i] <- tabsize - (cols[i] - 1) %% tabsize
139+
}
140+
cols <- cumsum(widths)
141+
start <- which(cols >= start)
142+
if (!length(start)) {
143+
return("")
144+
}
145+
start <- start[1]
146+
stop <- which(cols <= stop)
147+
if (length(stop)) {
148+
stop <- stop[length(stop)]
149+
substr(x, start, stop)
150+
} else {
151+
""
152+
}
153+
}
154+
155+
srcfile <- attr(data, "srcfile")
156+
terminal <- which(data$terminal)
157+
for (i in terminal) {
158+
lines <- getSrcLines(srcfile, data$line1[i], data$line2[i])
159+
n <- length(lines)
160+
lines[n] <- substr_with_tabs(lines[n], 1, data$col2[i])
161+
lines[1] <- substr_with_tabs(lines[1], data$col1[i], Inf)
162+
data$text[i] <- paste(lines, collapse = "\n")
163+
}
164+
165+
data
166+
}
167+
128168
do_subst <- function(code, pdata, hitext) {
129169

130170
pdata$hitext <- hitext

tests/testthat/test-prettycode.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,32 @@ test_that_cli(configs = "ansi", "new language features, lambda functions", {
240240
cat(code_highlight('\\(x) x * 2', list(reserved = "bold")))
241241
)
242242
})
243+
244+
test_that("code_highlight() works on long strings and symbols", {
245+
expect_true(
246+
grepl(
247+
strrep("-", 1000),
248+
code_highlight(paste0("foo('", strrep("-", 1000), "')"))
249+
)
250+
)
251+
252+
expect_true(
253+
grepl(
254+
strrep("-", 1000),
255+
code_highlight(paste0("foo(`", strrep("-", 1000), "`)"))
256+
)
257+
)
258+
expect_true(
259+
grepl(
260+
strrep("-", 1000),
261+
code_highlight(paste0("a$`", strrep("-", 1000), "`"))
262+
)
263+
)
264+
265+
expect_true(
266+
grepl(
267+
strrep("-", 1000),
268+
code_highlight(paste0("`", strrep("-", 1000), "`$a"))
269+
)
270+
)
271+
})

0 commit comments

Comments
 (0)