Skip to content

Commit

Permalink
Merge pull request #532 from lorenzwalthert/fix-write-back
Browse files Browse the repository at this point in the history
- Only write back when contents changed (#532).
  • Loading branch information
lorenzwalthert committed Jul 9, 2019
2 parents 6f606f4 + a4eddfe commit d421812
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: styler
Type: Package
Title: Non-Invasive Pretty Printing of R Code
Version: 1.1.1.9000
Version: 1.1.1.9002
Authors@R:
c(person(given = "Kirill",
family = "Müller",
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
directories. We no longer display the file name of the styled file, but
the absolute path. This is also reflected in the invisible return value of the
function (#522).
* `style_file()` and friends do not write content back to a file when
styling does not cause any changes in the file. This means the modification
date of files styled is only changed when the content is changed (#532).

## New features

Expand Down
7 changes: 4 additions & 3 deletions R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ transform_utf8 <- function(path, fun, write_back = TRUE) {
}

#' @importFrom rlang with_handlers warn
transform_utf8_one <- function(path, fun, write_back = write_back) {
transform_utf8_one <- function(path, fun, write_back) {
old <- xfun::read_utf8(path)
with_handlers({
new <- fun(old)
if (write_back) {
identical <- identical(unclass(old), unclass(new))
if (!identical && write_back) {
xfun::write_utf8(new, path)
}
!identical(unclass(old), unclass(new))
!identical
}, error = function(e) {
warn(paste0("When processing ", path, ": ", conditionMessage(e)))
NA
Expand Down
12 changes: 6 additions & 6 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ styler can be customized to format code according to other style guides too.
You can install the package from CRAN:

```{r, eval = FALSE}
install.packages("styler")
install.packages("styler")
```

Or get the development version from GitHub:

```{r, eval = FALSE}
# install.packages("remotes")
remotes::install_github("r-lib/styler")
# install.packages("remotes")
remotes::install_github("r-lib/styler")
```

## API
Expand Down Expand Up @@ -98,11 +98,11 @@ If you wish to keep alignment as is, you can use `strict = FALSE`:
```{r}
style_text(
c(
"first <- 4",
"first <- 4",
"second <- 1+1"
),
strict = FALSE
)
strict = FALSE
)
```

This was just the tip of the iceberg. Learn more about customization with the
Expand Down

0 comments on commit d421812

Please sign in to comment.