Skip to content

Commit 1889abd

Browse files
authored
fix call breaks (#229)
1 parent 777e087 commit 1889abd

8 files changed

+18
-29
lines changed

R/qenv-eval_code.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ setMethod("eval_code", signature = c("qenv", "character"), function(object, code
4040
return(object)
4141
}
4242
code_split <- split_code(paste(code, collapse = "\n"))
43-
4443
for (i in seq_along(code_split)) {
4544
current_code <- code_split[[i]]
4645
current_call <- parse(text = current_code, keep.source = TRUE)

R/qenv-get_code.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ setMethod("get_code", signature = "qenv", function(object, deparse = TRUE, names
141141
}
142142

143143
if (deparse) {
144-
gsub(";\n", ";", paste(gsub("\n$", "", unlist(code)), collapse = "\n"))
144+
paste(unlist(code), collapse = "\n")
145145
} else {
146146
parse(text = paste(c("{", unlist(code), "}"), collapse = "\n"), keep.source = TRUE)
147147
}

R/utils-get_code_dependency.R

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,16 +467,15 @@ split_code <- function(code) {
467467

468468
idx_start <- c(
469469
0, # first call starts in the beginning of src
470-
char_count_lines[call_breaks[, "line"]] + call_breaks[, "col"] + 2
470+
char_count_lines[call_breaks[, "line"]] + call_breaks[, "col"] + 1
471471
)
472472
idx_end <- c(
473-
char_count_lines[call_breaks[, "line"]] + call_breaks[, "col"] + 1,
473+
char_count_lines[call_breaks[, "line"]] + call_breaks[, "col"],
474474
nchar(code) # last call end in the end of src
475475
)
476476
new_code <- substring(code, idx_start, idx_end)
477477

478-
# we need to remove leading semicolons from the calls and move them to the previous call
479-
# this is a reasult of a wrong split, which ends on the end of call and not on the ;
480-
# semicolon is treated by R parser as a separate call.
481-
gsub("^([[:space:]])*;(.+)$", "\\1\\2", new_code, perl = TRUE)
478+
# line split happens before call terminator (it could be `;` or `\n`) and the terminator goes to the next line
479+
# we need to move remove leading and add \n instead when combining calls
480+
c(new_code[1], gsub("^[\t ]*(\n|;)", "", new_code[-1]))
482481
}

tests/testthat/test-qenv_eval_code.R

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,20 +152,13 @@ testthat::test_that("comments at the end of src are added to the previous call e
152152
testthat::test_that("comments from the same line are associated with it's call", {
153153
code <- c("x <- 5", " y <- 4 # comment", "z <- 5")
154154
q <- eval_code(qenv(), code)
155-
testthat::expect_identical(
156-
as.character(q@code)[2],
157-
paste0(code[2], "\n")
158-
)
155+
testthat::expect_identical(as.character(q@code)[2], code[2])
159156
})
160157

161158
testthat::test_that("alone comments at the end of the source are considered as continuation of the last call", {
162-
# todo: should be associated to the last call or be separted?
163-
code <- c("x <- 5\ny <- 10\n# comment")
159+
code <- c("x <- 5\n", "y <- 10\n# comment")
164160
q <- eval_code(eval_code(qenv(), code[1]), code[2])
165-
testthat::expect_identical(
166-
as.character(q@code)[2],
167-
"y <- 10\n# comment"
168-
)
161+
testthat::expect_identical(as.character(q@code)[2], code[2])
169162
})
170163

171164
testthat::test_that("comments passed alone to eval_code that contain @linksto tag have detected dependency", {

tests/testthat/test-qenv_extract.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ testthat::test_that("`[.` extracts the code only needed to recreate objects pass
8181
q <- eval_code(q, code)
8282
object_names <- c("x", "a")
8383
qs <- q[object_names]
84-
testthat::expect_identical(get_code(qs), c("x<-1\na<-1;"))
84+
testthat::expect_identical(get_code(qs), c("x<-1\na<-1"))
8585
})
8686

8787
testthat::test_that("`[.` comments are preserved in the code and associated with the following call", {
8888
q <- qenv()
8989
code <- c("x<-1 #comment", "a<-1;b<-2")
9090
q <- eval_code(q, code)
9191
qs <- q[c("x", "a")]
92-
testthat::expect_identical(get_code(qs), c("x<-1 #comment\na<-1;"))
92+
testthat::expect_identical(get_code(qs), c("x<-1 #comment\na<-1"))
9393
})

tests/testthat/test-qenv_get_code.R

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ testthat::test_that("get_code called with qenv.error returns error with trace in
4747
)
4848
})
4949

50-
testthat::test_that("get_code returns code with comments and empty spaces", {
50+
testthat::test_that("get_code formatted returns code asis but replaces `;` with `\n`", {
5151
code <- "
5252
# header comment after white space
5353
@@ -58,7 +58,7 @@ testthat::test_that("get_code returns code with comments and empty spaces", {
5858
# closing comment
5959
"
6060
q <- eval_code(qenv(), code)
61-
testthat::expect_equal(get_code(q), code)
61+
testthat::expect_equal(get_code(q), gsub(";", "\n", code))
6262
})
6363

6464
# names parameter -------------------------------------------------------------------------------------------------
@@ -240,14 +240,12 @@ testthat::describe("get_code for specific names", {
240240
testthat::expect_length(get_code(q1, deparse = FALSE), 1)
241241
})
242242

243-
testthat::it("does not break if code is separated by ;", {
244-
code <- c(
245-
"a <- 1;a <- a + 1"
246-
)
243+
testthat::it("detects calls associated with object if calls are separated by ;", {
244+
code <- c("a <- 1;b <- 2;a <- a + 1")
247245
q <- eval_code(qenv(), code)
248246
testthat::expect_identical(
249247
get_code(q, names = "a"),
250-
code
248+
"a <- 1\na <- a + 1"
251249
)
252250
})
253251

tests/testthat/test-qenv_get_messages.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ testthat::test_that("get_messages accepts a qenv object with a single eval_code
4949
"~~~ Messages ~~~\n",
5050
"> This is a message 1!",
5151
"when running code:",
52-
"message(\"This is a message 1!\")\n\n",
52+
"message(\"This is a message 1!\")\n",
5353
"> This is a message 2!",
5454
"when running code:",
5555
"message(\"This is a message 2!\")\n",

tests/testthat/test-qenv_get_warnings.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ testthat::test_that("get_warnings accepts a qenv object with a single eval_code
4949
"~~~ Warnings ~~~\n",
5050
"> This is a warning 1!",
5151
"when running code:",
52-
"warning(\"This is a warning 1!\")\n\n",
52+
"warning(\"This is a warning 1!\")\n",
5353
"> This is a warning 2!",
5454
"when running code:",
5555
"warning(\"This is a warning 2!\")\n",

0 commit comments

Comments
 (0)