Skip to content

Commit 35de66b

Browse files
committed
incorporate split_code into codebase
1 parent 811092b commit 35de66b

File tree

3 files changed

+20
-27
lines changed

3 files changed

+20
-27
lines changed

R/extract_code_as_is_prototype.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ get_line_ids <- function(pd) {
4545
Filter(Negate(is.null), c(list(first_comment_ids), call_ids, list(last_comment_ids)))
4646
}
4747

48-
split_code <- function(code, lines_ids) {
48+
split_code <- function(code, parsed_code) {
49+
50+
pd <- utils::getParseData(parsed_code)
51+
pd <- pd[pd$token != "';'", ]
52+
lines_ids <- get_line_ids(pd)
53+
4954
code_split <- strsplit(code, split = "\n", fixed = TRUE)[[1]]
5055
code_split_calls <- list()
5156

R/qenv-eval_code.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ setGeneric("eval_code", function(object, code) standardGeneric("eval_code"))
2929

3030
setMethod("eval_code", signature = c("qenv", "character"), function(object, code) {
3131
parsed_code <- parse(text = code, keep.source = TRUE)
32-
comments <- extract_comments(parsed_code)
3332
id <- sample.int(.Machine$integer.max, size = length(parsed_code))
3433

3534
object@id <- c(object@id, id)
3635
object@env <- rlang::env_clone(object@env, parent = parent.env(.GlobalEnv))
37-
object@code <- c(object@code, trimws(paste(as.character(parsed_code), comments)))
36+
37+
code_split <- split_code(code, parsed_code)
38+
object@code <- c(object@code, unlist(code_split))
3839

3940
current_warnings <- rep("", length(parsed_code))
4041
current_messages <- rep("", length(parsed_code))
4142

42-
43-
for (i in seq_along(parsed_code)) {
44-
single_call <- parsed_code[i]
43+
for (i in seq_along(code_split)) {
44+
single_call <- parse(text = code_split[[i]], keep.source = FALSE)
4545
# Using withCallingHandlers to capture warnings and messages.
4646
# Using tryCatch to capture the error and abort further evaluation.
4747
x <- withCallingHandlers(

R/utils-get_code_dependency.R

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ get_code_dependency <- function(code, names, check_names = TRUE) {
3939
code <- sub("^\\{(.*)\\}$", "\\1", tcode)
4040
}
4141

42-
43-
code <- parse(text = code, keep.source = TRUE)
44-
pd <- utils::getParseData(code)
42+
code <- split_code(code, parsed_code)
43+
parsed_code <- parse(text = code, keep.source = TRUE)
44+
pd <- utils::getParseData(parsed_code)
4545
pd <- normalize_pd(pd)
4646
calls_pd <- extract_calls(pd)
47-
comments <- extract_comments(code)
47+
comments <- extract_comments(parsed_code)
4848

4949
if (check_names) {
5050
# Detect if names are actually in code.
@@ -66,7 +66,8 @@ get_code_dependency <- function(code, names, check_names = TRUE) {
6666
lib_ind <- detect_libraries(calls_pd)
6767

6868
code_ids <- sort(unique(c(lib_ind, ind)))
69-
trimws(paste(as.character(code[code_ids]), comments[code_ids]))
69+
code[code_ids]
70+
#trimws(paste(as.character(code[code_ids]), comments[code_ids]))
7071
}
7172

7273
#' Locate function call token
@@ -141,12 +142,12 @@ get_children <- function(pd, parent) {
141142
#' Fixes edge case of comments being shifted to the next call.
142143
#' @keywords internal
143144
#' @noRd
144-
fix_shifted_comments <- function(calls, pattern = "@linksto") {
145+
fix_shifted_comments <- function(calls) {
145146
# If the first or the second token is a @linksto COMMENT,
146147
# then it belongs to the previous call.
147148
if (length(calls) >= 2) {
148149
for (i in 2:length(calls)) {
149-
comment_idx <- grep(pattern, calls[[i]][, "text"])
150+
comment_idx <- grep("@linksto", calls[[i]][, "text"])
150151
if (isTRUE(comment_idx[1] <= 2)) {
151152
calls[[i - 1]] <- rbind(
152153
calls[[i - 1]],
@@ -156,20 +157,7 @@ fix_shifted_comments <- function(calls, pattern = "@linksto") {
156157
}
157158
}
158159
}
159-
calls <- Filter(nrow, calls)
160-
# If, after shifting, there are two COMMENTs in one call, paste them.
161-
merge_comments <- function(call) {
162-
if (sum(call$token == "COMMENT") >= 2) {
163-
comments <- call[call$token == "COMMENT", "text"]
164-
first_comment_row <- call[which(call$token == "COMMENT")[1], ]
165-
call <- call[call$token != "COMMENT", ]
166-
first_comment_row$text <- paste(comments, collapse = " ")
167-
rbind(call, first_comment_row)
168-
} else {
169-
call
170-
}
171-
}
172-
lapply(calls, merge_comments)
160+
Filter(nrow, calls)
173161
}
174162

175163
#' Fixes edge case of `<-` assignment operator being called as function,

0 commit comments

Comments
 (0)