2828setGeneric ("eval_code ", function(object, code) standardGeneric("eval_code"))
2929
3030setMethod ("eval_code ", signature = c("qenv", "character"), function(object, code) {
31- id <- sample.int(.Machine $ integer.max , size = 1 )
31+ parsed_code <- parse(text = code , keep.source = TRUE )
32+ comments <- extract_comments(parsed_code )
33+ id <- sample.int(.Machine $ integer.max , size = length(parsed_code ))
3234
3335 object @ id <- c(object @ id , id )
3436 object @ env <- rlang :: env_clone(object @ env , parent = parent.env(.GlobalEnv ))
35- code <- paste(code , collapse = " \n " )
36- object @ code <- c(object @ code , code )
37+ object @ code <- c(object @ code , trimws(paste(as.character(parsed_code ), comments )))
3738
38- current_warnings <- " "
39- current_messages <- " "
39+ current_warnings <- rep( " " , length( parsed_code ))
40+ current_messages <- rep( " " , length( parsed_code ))
4041
41- parsed_code <- parse(text = code , keep.source = TRUE )
42- for (single_call in parsed_code ) {
42+
43+ for (i in 1 : length(parsed_code )) {
44+ single_call <- parsed_code [i ]
4345 # Using withCallingHandlers to capture warnings and messages.
4446 # Using tryCatch to capture the error and abort further evaluation.
4547 x <- withCallingHandlers(
@@ -66,11 +68,11 @@ setMethod("eval_code", signature = c("qenv", "character"), function(object, code
6668 }
6769 ),
6870 warning = function (w ) {
69- current_warnings <<- paste0( current_warnings , .ansi_strip(sprintf(" > %s\n " , conditionMessage(w ) )))
71+ current_warnings [ i ] <<- .ansi_strip(sprintf(" > %s\n " , conditionMessage(w )))
7072 invokeRestart(" muffleWarning" )
7173 },
7274 message = function (m ) {
73- current_messages <<- paste0( current_messages , .ansi_strip(sprintf(" > %s" , conditionMessage(m ) )))
75+ current_messages [ i ] <<- .ansi_strip(sprintf(" > %s" , conditionMessage(m )))
7476 invokeRestart(" muffleMessage" )
7577 }
7678 )
0 commit comments