Skip to content

Commit d421812

Browse files
Merge pull request #532 from lorenzwalthert/fix-write-back
- Only write back when contents changed (#532).
2 parents 6f606f4 + a4eddfe commit d421812

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: styler
22
Type: Package
33
Title: Non-Invasive Pretty Printing of R Code
4-
Version: 1.1.1.9000
4+
Version: 1.1.1.9002
55
Authors@R:
66
c(person(given = "Kirill",
77
family = "Müller",

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
directories. We no longer display the file name of the styled file, but
77
the absolute path. This is also reflected in the invisible return value of the
88
function (#522).
9+
* `style_file()` and friends do not write content back to a file when
10+
styling does not cause any changes in the file. This means the modification
11+
date of files styled is only changed when the content is changed (#532).
912

1013
## New features
1114

R/io.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ transform_utf8 <- function(path, fun, write_back = TRUE) {
1414
}
1515

1616
#' @importFrom rlang with_handlers warn
17-
transform_utf8_one <- function(path, fun, write_back = write_back) {
17+
transform_utf8_one <- function(path, fun, write_back) {
1818
old <- xfun::read_utf8(path)
1919
with_handlers({
2020
new <- fun(old)
21-
if (write_back) {
21+
identical <- identical(unclass(old), unclass(new))
22+
if (!identical && write_back) {
2223
xfun::write_utf8(new, path)
2324
}
24-
!identical(unclass(old), unclass(new))
25+
!identical
2526
}, error = function(e) {
2627
warn(paste0("When processing ", path, ": ", conditionMessage(e)))
2728
NA

README.Rmd

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ styler can be customized to format code according to other style guides too.
3535
You can install the package from CRAN:
3636

3737
```{r, eval = FALSE}
38-
install.packages("styler")
38+
install.packages("styler")
3939
```
4040

4141
Or get the development version from GitHub:
4242

4343
```{r, eval = FALSE}
44-
# install.packages("remotes")
45-
remotes::install_github("r-lib/styler")
44+
# install.packages("remotes")
45+
remotes::install_github("r-lib/styler")
4646
```
4747

4848
## API
@@ -98,11 +98,11 @@ If you wish to keep alignment as is, you can use `strict = FALSE`:
9898
```{r}
9999
style_text(
100100
c(
101-
"first <- 4",
101+
"first <- 4",
102102
"second <- 1+1"
103103
),
104-
strict = FALSE
105-
)
104+
strict = FALSE
105+
)
106106
```
107107

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

0 commit comments

Comments
 (0)