@@ -55,7 +55,7 @@ code_highlight <- function(code, code_theme = NULL, envir = NULL) {
55
55
}
56
56
57
57
theme <- code_theme_make(code_theme )
58
- data <- getParseData (parsed , includeText = NA )
58
+ data <- get_parse_data (parsed )
59
59
60
60
hitext <- data $ text
61
61
@@ -125,6 +125,46 @@ code_highlight <- function(code, code_theme = NULL, envir = NULL) {
125
125
do_subst(code , data , hitext )
126
126
}
127
127
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
+
128
168
do_subst <- function (code , pdata , hitext ) {
129
169
130
170
pdata $ hitext <- hitext
0 commit comments