Skip to content

Commit 8434f36

Browse files
dmurdochhadley
andauthored
Fixes truncation of long tokens by using utils::getParseText() (#136)
Fixes #128 Co-authored-by: Hadley Wickham <[email protected]>
1 parent a113582 commit 8434f36

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# downlit (development version)
22

3+
* Very long strings or other tokens are no longer truncated
4+
by `downlit::highlight()` (@dmurdoch, #128).
5+
36
* Auto-linking works when used with double colon infix operator and `utils::help()`
47
(@IndrajeetPatil, #131).
58

R/highlight.R

+18-1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,23 @@ line_col <- function(x) {
121121
data.frame(line, col)
122122
}
123123

124+
# utils::getParseData will truncate very long strings or tokens;
125+
# this function checks for that and uses the slow
126+
# utils::getParseText function when necessary.
127+
128+
getFullParseData <- function(x) {
129+
res <- utils::getParseData(x)
130+
131+
truncated <- res$terminal &
132+
substr(res$text, 1, 1) == "[" &
133+
nchar(res$text) > 5 # 5 is arbitrary, 2 would probably be enough
134+
135+
if (any(truncated))
136+
res$text[truncated] <- utils::getParseText(res, res$id[truncated])
137+
138+
res
139+
}
140+
124141
parse_data <- function(text) {
125142
text <- standardise_text(text)
126143
stopifnot(is.character(text), length(text) == 1)
@@ -130,7 +147,7 @@ parse_data <- function(text) {
130147
return(NULL)
131148
}
132149

133-
list(text = text, expr = expr, data = utils::getParseData(expr))
150+
list(text = text, expr = expr, data = getFullParseData(expr))
134151
}
135152

136153
# Highlighting ------------------------------------------------------------

tests/testthat/test-highlight.R

+6
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,9 @@ test_that("ansi escapes are converted to html", {
109109
expect_snapshot_output(highlight("# \033[31mhello\033[m"))
110110
expect_snapshot_output(highlight("# \u2029[31mhello\u2029[m"))
111111
})
112+
113+
test_that("can highlight vers long strings", {
114+
val <- paste0(rep('very', 200), collapse = " ")
115+
out <- downlit::highlight(sprintf("'%s'", val))
116+
expect_equal(out, paste0("<span class='s'>'", val, "'</span>"))
117+
})

0 commit comments

Comments
 (0)