Skip to content

Commit a91a971

Browse files
add messages about current file under styling.
1 parent 31179ff commit a91a971

File tree

5 files changed

+65
-4
lines changed

5 files changed

+65
-4
lines changed

R/rules-other.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ add_brackets_in_pipe <- function(pd) {
77
lag_newlines = rep(0, 2),
88
terminal = rep(TRUE, 2),
99
spaces = rep(0, 2),
10+
multi_line = rep(FALSE, 2),
11+
indention_ref_id = rep(NA, 2),
1012
line1 = pd$line1[has_no_brackets],
1113
line2 = line1,
12-
col1 = pd$col1[has_no_brackets],
14+
col1 = pd$col1[has_no_brackets] + c(0.3, 0.6),
1315
col2 = col1 + 1:2,
1416
indent = rep(0, 2),
1517
child = rep(list(NULL), 2)

R/transform.R

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,43 @@
1010
#' carefully inspect the changes via a message sent to the console.
1111
transform_files <- function(files, transformers) {
1212
transformer <- make_transformer(transformers)
13-
changed <- utf8::transform_lines_enc(files, transformer)
14-
if (any(changed, na.rm = TRUE)) {
13+
max_char <- min(max(nchar(files), 0), 80)
14+
changed <- map_lgl(
15+
files, transform_file, fun = transformer, max_char_path = max_char
16+
)
17+
18+
if (!any(changed, na.rm = TRUE)) {
19+
message("No files changed.")
20+
} else {
21+
message("Files changed: ", utf8:::ellipsis(files[which(changed)]))
1522
message("Please review the changes carefully!")
1623
}
1724
invisible(changed)
1825
}
26+
27+
#' Transform a file an give customized message
28+
#'
29+
#' Wraps `utf8::transform_lines_enc()` and gives customized messages.
30+
#' @inheritParams utf8::transform_lines_enc
31+
#' @param ... Further arguments passed to `utf8::transform_lines_enc()`.
32+
transform_file <- function(path,
33+
fun,
34+
verbose = FALSE,
35+
max_char_path,
36+
message_before = "styling file ",
37+
message_after = " [DONE]",
38+
...) {
39+
char_after_path <- nchar(message_before) + nchar(path) + 1
40+
max_char_after_message_path <- nchar(message_before) + max_char_path + 1
41+
n_spaces_before_message_after <-
42+
max_char_after_message_path - char_after_path
43+
message(message_before, path, ".", appendLF = FALSE)
44+
changed <- utf8::transform_lines_enc(path, fun = fun, verbose = verbose, ...)
45+
46+
message(rep(" ", max(0, n_spaces_before_message_after)), message_after)
47+
invisible(changed)
48+
}
49+
1950
#' Closure to return a transformer function
2051
#'
2152
#' This function takes a list of transformer functions as input and

man/transform_file.Rd

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# empty

tests/testthat/test-public_api.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ test_that("styler can style file", {
1616
)
1717
})
1818

19-
# style_active_file() must be tested manually.
19+
test_that("styler does not return error when there is no file to style", {
20+
expect_false(
21+
style_dir(paste0(base, "/xyzemptydir"), strict = FALSE)
22+
)
23+
})
24+
25+

0 commit comments

Comments
 (0)