Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# teal.code 0.7.1.9000

### Bug fixes

* Fixed a problem parsing Chinese characters due to the encoding (#284)

# teal.code 0.7.1

### Bug fixes
Expand Down
7 changes: 5 additions & 2 deletions R/qenv-eval_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ setMethod("eval_code", signature = c(object = "qenv.error"), function(object, co
code <- paste(split_code(code), collapse = "\n")

object@.xData <- rlang::env_clone(object@.xData, parent = parent.env(object@.xData))
parsed_code <- parse(text = code, keep.source = TRUE)
parsed_code <- parse(text = code, keep.source = TRUE, encoding = "UTF-8")

old <- evaluate::inject_funs(
library = function(...) {
Expand All @@ -72,7 +72,10 @@ setMethod("eval_code", signature = c(object = "qenv.error"), function(object, co
for (this in out) {
if (inherits(this, "source")) {
this_code <- gsub("\n$", "", this$src)
attr(this_code, "dependency") <- extract_dependency(parse(text = this_code, keep.source = TRUE))
attr(this_code, "dependency") <- extract_dependency(parse(
text = this_code,
keep.source = TRUE, encoding = "UTF-8"
))
new_code <- c(new_code, stats::setNames(list(this_code), sample.int(.Machine$integer.max, size = 1)))
} else {
last_code <- new_code[[length(new_code)]]
Expand Down
2 changes: 1 addition & 1 deletion R/qenv-get_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ setMethod("get_code", signature = "qenv", function(object, deparse = TRUE, names
if (deparse) {
paste(unlist(code), collapse = "\n")
} else {
parse(text = paste(c("{", unlist(code), "}"), collapse = "\n"), keep.source = TRUE)
parse(text = paste(c("{", unlist(code), "}"), collapse = "\n"), keep.source = TRUE, encoding = "UTF-8")
}
})

Expand Down
7 changes: 4 additions & 3 deletions R/utils-get_code_dependency.R
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ extract_dependency <- function(parsed_code) {
queue <- as.list(parsed_code[[1]][expr_ix])
new_list <- parsed_code[[1]]
new_list[expr_ix] <- NULL
list(parse(text = as.expression(new_list), keep.source = TRUE))
list(parse(text = as.expression(new_list), keep.source = TRUE, encoding = "UTF-8"))
}

while (length(queue) > 0) {
Expand All @@ -416,7 +416,8 @@ extract_dependency <- function(parsed_code) {
if (identical(current[[1L]], as.name("{"))) {
queue <- append(queue, as.list(current)[-1L])
} else {
parsed_code_list[[length(parsed_code_list) + 1]] <- parse(text = as.expression(current), keep.source = TRUE)
parsed_code <- parse(text = as.expression(current), keep.source = TRUE, encoding = "UTF-8")
parsed_code_list[[length(parsed_code_list) + 1]] <- parsed_code
}
}

Expand Down Expand Up @@ -556,7 +557,7 @@ normalize_pd <- function(pd) {
#' @keywords internal
#' @noRd
get_call_breaks <- function(code) {
parsed_code <- parse(text = code, keep.source = TRUE)
parsed_code <- parse(text = code, keep.source = TRUE, encoding = "UTF-8")
pd <- utils::getParseData(parsed_code)
pd <- normalize_pd(pd)
pd <- pd[pd$token != "';'", ]
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-qenv_within.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,15 @@ testthat::test_that("Code executed with integer shorthand (1L) is the same as or
q <- within(qenv(), a <- 1L)
testthat::expect_identical(get_code(q), "a <- 1L")
})


testthat::test_that("Chinese characters are handled properly (issue 284)", {
q <- within(qenv(), {
"无进展生存期 (月)"
"总生存期 (月)"
"缓解持续时间 (月)"
"确认的缓解持续时间 (月)"
})

expect_equal(lengths(strsplit(get_code(q), split = "\n", fixed = TRUE)), 4L)
})
Loading