Skip to content

Commit 88639c1

Browse files
Merge pull request #165 from lorenzwalthert/communication
- add informative messages while styling files (#165).
2 parents 38af3b1 + 5f7f3bf commit 88639c1

File tree

5 files changed

+86
-4
lines changed

5 files changed

+86
-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: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,54 @@
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+
if (length(files) > 0) message("Styling files:")
15+
changed <- map_lgl(
16+
files, transform_file, fun = transformer, max_char_path = max_char
17+
)
18+
if (!any(changed, na.rm = TRUE)) {
19+
message("No files changed.")
20+
} else {
21+
message("* File 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+
#' @param max_char_path The number of characters of the longest path. Determines
31+
#' the indention level of `message_after`.
32+
#' @param message_before The message to print before the path.
33+
#' @param message_after The message to print after the path.
34+
#' @param message_after_if_changed The message to print after `message_after` if
35+
#' any file was transformed.
36+
#' @inheritParams utf8::transform_lines_enc
37+
#' @param ... Further arguments passed to `utf8::transform_lines_enc()`.
38+
transform_file <- function(path,
39+
fun,
40+
verbose = FALSE,
41+
max_char_path,
42+
message_before = "",
43+
message_after = " [DONE]",
44+
message_after_if_changed = " *",
45+
...) {
46+
char_after_path <- nchar(message_before) + nchar(path) + 1
47+
max_char_after_message_path <- nchar(message_before) + max_char_path + 1
48+
n_spaces_before_message_after <-
49+
max_char_after_message_path - char_after_path
50+
message(message_before, path, ".", appendLF = FALSE)
51+
changed <- utf8::transform_lines_enc(path, fun = fun, verbose = verbose, ...)
52+
53+
message(
54+
rep(" ", max(0, n_spaces_before_message_after)),
55+
message_after,
56+
if (any(changed, na.rm = TRUE)) message_after_if_changed
57+
)
58+
invisible(changed)
59+
}
60+
1961
#' Closure to return a transformer function
2062
#'
2163
#' This function takes a list of transformer functions as input and

man/transform_file.Rd

Lines changed: 33 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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ 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_error(style_dir(paste0(base, "/xyzemptydir"), strict = FALSE), NA)
21+
})
22+
23+

0 commit comments

Comments
 (0)