Skip to content

Commit

Permalink
replace enc with xfun
Browse files Browse the repository at this point in the history
Substituting enc's read and write functionality with xfun and re-implement enc::transform_lines_enc() in R/io.R
  • Loading branch information
lorenzwalthert committed Nov 18, 2018
1 parent 7f61328 commit 8458b93
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 37 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ BugReports: https://github.com/r-lib/styler/issues
Imports:
backports,
cli,
enc (>= 0.2),
magrittr,
purrr (>= 0.2.3),
rematch2,
rlang,
rprojroot,
tibble (>= 1.4.2),
tools,
withr
withr,
xfun
Suggests:
data.tree,
dplyr,
Expand All @@ -44,6 +44,7 @@ Collate:
'expr-is.R'
'indent.R'
'initialize.R'
'io.R'
'nest.R'
'nested-to-tree.R'
'parse.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export(tidyverse_reindention)
export(tidyverse_style)
import(tibble)
importFrom(magrittr,"%>%")
importFrom(magrittr,set_names)
importFrom(purrr,as_mapper)
importFrom(purrr,compact)
importFrom(purrr,flatten)
Expand Down
27 changes: 27 additions & 0 deletions R/io.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#' Apply a function to the contents of a file
#'
#' Transforms a file with a function.
#' @param path A vector with file paths to transform.
#' @param fun A function that returns a character vector.
#' @param write_back Whether or not the results of the transformation should
#' be written back to the file.
#' @importFrom magrittr set_names
#' @keywords internal
transform_lines_utf8 <- function(path, fun, write_back = TRUE) {
map_lgl(path, transform_lines_utf8_one, fun = fun, write_back = write_back) %>%
set_names(path)
}

transform_lines_utf8_one <- function(path, fun, write_back = write_back) {
old <- xfun::read_utf8(path)
tryCatch({
new <- fun(old)
if (write_back) {
xfun::write_utf8(new, path)
}
!identical(unclass(old), unclass(new))
}, error = function(e) {
warning("When processing ", path, ": ", conditionMessage(e), call. = FALSE)
NA
})
}
2 changes: 1 addition & 1 deletion R/roxygen-examples-find.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ identify_start_to_stop_of_roxygen_examples_from_text <- function(text) {
}

identify_start_to_stop_of_roxygen_examples <- function(path) {
content <- enc::read_lines_enc(path)
content <- xfun::read_utf8(path)
identify_start_to_stop_of_roxygen_examples_from_text(content)
}

Expand Down
7 changes: 3 additions & 4 deletions R/testing.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,18 @@ transform_and_check <- function(in_item, out_item,
write_tree = NA,
out_tree = "_tree", ...) {
write_tree <- set_arg_write_tree(write_tree)
read_in <- enc::read_lines_enc(in_item)
read_in <- xfun::read_utf8(in_item)
if (write_tree) {
create_tree(read_in) %>%
write.table(out_tree, col.names = FALSE, row.names = FALSE, quote = FALSE)
}
transformed_text <- read_in %>%
transformer(...) %>%
unclass()
transformed <- enc::transform_lines_enc(
transformed <- transform_lines_utf8(
out_item,
function(x) transformed_text,
write_back = write_back,
verbose = FALSE
write_back = write_back
)

if (transformed) {
Expand Down
18 changes: 9 additions & 9 deletions R/transform-code.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#' Transform code from R, Rmd or Rnw files
#'
#' A wrapper for [enc::transform_lines_enc()] which initiates the styling of
#' A wrapper which initiates the styling of
#' either R, Rmd or Rnw files by passing the relevant transformer function for each
#' case.
#'
#' @inheritParams enc::transform_lines_enc
#' @param ... Further arguments passed to `enc::transform_lines_enc()`.
#' @keywords internal
transform_code <- function(path, fun, verbose = FALSE, ...) {
transform_code <- function(path, fun, ...) {
if (is_plain_r_file(path)) {
enc::transform_lines_enc(path, fun = fun, ..., verbose = verbose)
transform_lines_utf8(path, fun = fun, ...)
} else if (is_rmd_file(path)) {
enc::transform_lines_enc(path,
fun = partial(transform_mixed, transformer_fun = fun, filetype = "Rmd"), ...,
verbose = verbose
transform_lines_utf8(path,
fun = partial(transform_mixed, transformer_fun = fun, filetype = "Rmd"),
...
)
} else if (is_rnw_file(path)) {
enc::transform_lines_enc(path,
fun = partial(transform_mixed, transformer_fun = fun, filetype = "Rnw"), ...,
verbose = verbose
transform_lines_utf8(path,
fun = partial(transform_mixed, transformer_fun = fun, filetype = "Rnw"),
...
)
} else {
stop(path, " is not an R, Rmd or Rnw file")
Expand Down
7 changes: 3 additions & 4 deletions R/transform-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,18 @@ transform_files <- function(files, transformers, include_roxygen_examples) {

#' Transform a file and output a customized message
#'
#' Wraps `enc::transform_lines_enc()` and outputs customized messages.
#' Transforms file contents and outputs customized messages.
#' @param max_char_path The number of characters of the longest path. Determines
#' the indention level of `message_after`.
#' @param message_before The message to print before the path.
#' @param message_after The message to print after the path.
#' @param message_after_if_changed The message to print after `message_after` if
#' any file was transformed.
#' @inheritParams enc::transform_lines_enc
#' @inheritParams transform_lines_utf8
#' @param ... Further arguments passed to `enc::transform_lines_enc()`.
#' @keywords internal
transform_file <- function(path,
fun,
verbose = FALSE,
max_char_path,
message_before = "",
message_after = " [DONE]",
Expand All @@ -53,7 +52,7 @@ transform_file <- function(path,
rep_char(" ", max(0L, n_spaces_before_message_after)),
append = FALSE
)
changed <- transform_code(path, fun = fun, verbose = verbose, ...)
changed <- transform_code(path, fun = fun, ...)

bullet <- ifelse(is.na(changed), "warning", ifelse(changed, "info", "tick"))

Expand Down
4 changes: 2 additions & 2 deletions R/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ prettify_any <- function(transformers,
#' @examples
#' # the following is identical but the former is more convenient:
#' file <- tempfile("styler", fileext = ".R")
#' enc::write_lines_enc("1++1", file)
#' xfun::write_utf8("1++1", file)
#' style_file(file, style = tidyverse_style, strict = TRUE)
#' style_file(file, transformers = tidyverse_style(strict = TRUE))
#' enc::read_lines_enc(file)
#' xfun::write_utf8(file)
#' unlink(file)
#' @family stylers
#' @export
Expand Down
4 changes: 2 additions & 2 deletions man/style_file.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions man/transform_code.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions man/transform_file.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions man/transform_lines_utf8.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8458b93

Please sign in to comment.