Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only write back when contents changed #532

Merged
merged 5 commits into from
Jul 9, 2019
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
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