Skip to content

Commit 9f6b57a

Browse files
Merge pull request #442 from lorenzwalthert/remove-enc
- replace enc with xfun (#432).
2 parents 7f61328 + 8002a9d commit 9f6b57a

23 files changed

+101
-61
lines changed

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ BugReports: https://github.com/r-lib/styler/issues
1212
Imports:
1313
backports,
1414
cli,
15-
enc (>= 0.2),
1615
magrittr,
1716
purrr (>= 0.2.3),
1817
rematch2,
1918
rlang,
2019
rprojroot,
2120
tibble (>= 1.4.2),
2221
tools,
23-
withr
22+
withr,
23+
xfun
2424
Suggests:
2525
data.tree,
2626
dplyr,
@@ -44,6 +44,7 @@ Collate:
4444
'expr-is.R'
4545
'indent.R'
4646
'initialize.R'
47+
'io.R'
4748
'nest.R'
4849
'nested-to-tree.R'
4950
'parse.R'

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export(tidyverse_reindention)
1414
export(tidyverse_style)
1515
import(tibble)
1616
importFrom(magrittr,"%>%")
17+
importFrom(magrittr,set_names)
1718
importFrom(purrr,as_mapper)
1819
importFrom(purrr,compact)
1920
importFrom(purrr,flatten)

R/io.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#' Apply a function to the contents of a file
2+
#'
3+
#' Transforms a file with a function.
4+
#' @param path A vector with file paths to transform.
5+
#' @param fun A function that returns a character vector.
6+
#' @param write_back Whether or not the results of the transformation should
7+
#' be written back to the file.
8+
#' @importFrom magrittr set_names
9+
#' @keywords internal
10+
transform_utf8 <- function(path, fun, write_back = TRUE) {
11+
map_lgl(path, transform_utf8_one, fun = fun, write_back = write_back) %>%
12+
set_names(path)
13+
}
14+
15+
transform_utf8_one <- function(path, fun, write_back = write_back) {
16+
old <- xfun::read_utf8(path)
17+
tryCatch({
18+
new <- fun(old)
19+
if (write_back) {
20+
xfun::write_utf8(new, path)
21+
}
22+
!identical(unclass(old), unclass(new))
23+
}, error = function(e) {
24+
warning("When processing ", path, ": ", conditionMessage(e), call. = FALSE)
25+
NA
26+
})
27+
}

R/roxygen-examples-find.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ identify_start_to_stop_of_roxygen_examples_from_text <- function(text) {
1414
}
1515

1616
identify_start_to_stop_of_roxygen_examples <- function(path) {
17-
content <- enc::read_lines_enc(path)
17+
content <- xfun::read_utf8(path)
1818
identify_start_to_stop_of_roxygen_examples_from_text(content)
1919
}
2020

R/testing.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,18 @@ transform_and_check <- function(in_item, out_item,
102102
write_tree = NA,
103103
out_tree = "_tree", ...) {
104104
write_tree <- set_arg_write_tree(write_tree)
105-
read_in <- enc::read_lines_enc(in_item)
105+
read_in <- xfun::read_utf8(in_item)
106106
if (write_tree) {
107107
create_tree(read_in) %>%
108108
write.table(out_tree, col.names = FALSE, row.names = FALSE, quote = FALSE)
109109
}
110110
transformed_text <- read_in %>%
111111
transformer(...) %>%
112112
unclass()
113-
transformed <- enc::transform_lines_enc(
113+
transformed <- transform_utf8(
114114
out_item,
115115
function(x) transformed_text,
116-
write_back = write_back,
117-
verbose = FALSE
116+
write_back = write_back
118117
)
119118

120119
if (transformed) {

R/transform-code.R

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
#' Transform code from R, Rmd or Rnw files
22
#'
3-
#' A wrapper for [enc::transform_lines_enc()] which initiates the styling of
3+
#' A wrapper which initiates the styling of
44
#' either R, Rmd or Rnw files by passing the relevant transformer function for each
55
#' case.
66
#'
7-
#' @inheritParams enc::transform_lines_enc
8-
#' @param ... Further arguments passed to `enc::transform_lines_enc()`.
7+
#' @inheritParams transform_utf8
8+
#' @param ... Further arguments passed to [transform_utf8()].
99
#' @keywords internal
10-
transform_code <- function(path, fun, verbose = FALSE, ...) {
10+
transform_code <- function(path, fun, ...) {
1111
if (is_plain_r_file(path)) {
12-
enc::transform_lines_enc(path, fun = fun, ..., verbose = verbose)
12+
transform_utf8(path, fun = fun, ...)
1313
} else if (is_rmd_file(path)) {
14-
enc::transform_lines_enc(path,
15-
fun = partial(transform_mixed, transformer_fun = fun, filetype = "Rmd"), ...,
16-
verbose = verbose
14+
transform_utf8(path,
15+
fun = partial(transform_mixed, transformer_fun = fun, filetype = "Rmd"),
16+
...
1717
)
1818
} else if (is_rnw_file(path)) {
19-
enc::transform_lines_enc(path,
20-
fun = partial(transform_mixed, transformer_fun = fun, filetype = "Rnw"), ...,
21-
verbose = verbose
19+
transform_utf8(path,
20+
fun = partial(transform_mixed, transformer_fun = fun, filetype = "Rnw"),
21+
...
2222
)
2323
} else {
2424
stop(path, " is not an R, Rmd or Rnw file")

R/transform-files.R

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,18 @@ transform_files <- function(files, transformers, include_roxygen_examples) {
2626

2727
#' Transform a file and output a customized message
2828
#'
29-
#' Wraps `enc::transform_lines_enc()` and outputs customized messages.
29+
#' Transforms file contents and outputs customized messages.
3030
#' @param max_char_path The number of characters of the longest path. Determines
3131
#' the indention level of `message_after`.
3232
#' @param message_before The message to print before the path.
3333
#' @param message_after The message to print after the path.
3434
#' @param message_after_if_changed The message to print after `message_after` if
3535
#' any file was transformed.
36-
#' @inheritParams enc::transform_lines_enc
37-
#' @param ... Further arguments passed to `enc::transform_lines_enc()`.
36+
#' @inheritParams transform_utf8
37+
#' @param ... Further arguments passed to [transform_utf8()].
3838
#' @keywords internal
3939
transform_file <- function(path,
4040
fun,
41-
verbose = FALSE,
4241
max_char_path,
4342
message_before = "",
4443
message_after = " [DONE]",
@@ -53,7 +52,7 @@ transform_file <- function(path,
5352
rep_char(" ", max(0L, n_spaces_before_message_after)),
5453
append = FALSE
5554
)
56-
changed <- transform_code(path, fun = fun, verbose = verbose, ...)
55+
changed <- transform_code(path, fun = fun, ...)
5756

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

R/ui.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,10 @@ prettify_any <- function(transformers,
215215
#' @examples
216216
#' # the following is identical but the former is more convenient:
217217
#' file <- tempfile("styler", fileext = ".R")
218-
#' enc::write_lines_enc("1++1", file)
218+
#' xfun::write_utf8("1++1", file)
219219
#' style_file(file, style = tidyverse_style, strict = TRUE)
220220
#' style_file(file, transformers = tidyverse_style(strict = TRUE))
221-
#' enc::read_lines_enc(file)
221+
#' xfun::read_utf8(file)
222222
#' unlink(file)
223223
#' @family stylers
224224
#' @export

man/style_file.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/transform_code.Rd

Lines changed: 4 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)