Skip to content

Communication #165

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

Merged
merged 2 commits into from
Aug 28, 2017
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
4 changes: 3 additions & 1 deletion R/rules-other.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ add_brackets_in_pipe <- function(pd) {
lag_newlines = rep(0, 2),
terminal = rep(TRUE, 2),
spaces = rep(0, 2),
multi_line = rep(FALSE, 2),
indention_ref_id = rep(NA, 2),
line1 = pd$line1[has_no_brackets],
line2 = line1,
col1 = pd$col1[has_no_brackets],
col1 = pd$col1[has_no_brackets] + c(0.3, 0.6),
col2 = col1 + 1:2,
indent = rep(0, 2),
child = rep(list(NULL), 2)
Expand Down
46 changes: 44 additions & 2 deletions R/transform.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,54 @@
#' carefully inspect the changes via a message sent to the console.
transform_files <- function(files, transformers) {
transformer <- make_transformer(transformers)
changed <- utf8::transform_lines_enc(files, transformer)
if (any(changed, na.rm = TRUE)) {
max_char <- min(max(nchar(files), 0), 80)
if (length(files) > 0) message("Styling files:")
changed <- map_lgl(
files, transform_file, fun = transformer, max_char_path = max_char
)
if (!any(changed, na.rm = TRUE)) {
message("No files changed.")
} else {
message("* File changed.")
message("Please review the changes carefully!")
}
invisible(changed)
}

#' Transform a file an give customized message
#'
#' Wraps `utf8::transform_lines_enc()` and gives customized messages.
#' @param max_char_path The number of characters of the longest path. Determines
#' the indention level of `message_after`.
#' @param message_before The message to print before the path.
#' @param message_after The message to print after the path.
#' @param message_after_if_changed The message to print after `message_after` if
#' any file was transformed.
#' @inheritParams utf8::transform_lines_enc
#' @param ... Further arguments passed to `utf8::transform_lines_enc()`.
transform_file <- function(path,
fun,
verbose = FALSE,
max_char_path,
message_before = "",
message_after = " [DONE]",
message_after_if_changed = " *",
...) {
char_after_path <- nchar(message_before) + nchar(path) + 1
max_char_after_message_path <- nchar(message_before) + max_char_path + 1
n_spaces_before_message_after <-
max_char_after_message_path - char_after_path
message(message_before, path, ".", appendLF = FALSE)
changed <- utf8::transform_lines_enc(path, fun = fun, verbose = verbose, ...)

message(
rep(" ", max(0, n_spaces_before_message_after)),
message_after,
if (any(changed, na.rm = TRUE)) message_after_if_changed
)
invisible(changed)
}

#' Closure to return a transformer function
#'
#' This function takes a list of transformer functions as input and
Expand Down
33 changes: 33 additions & 0 deletions man/transform_file.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/testthat/public-api/xyzemptydir/non_r_file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# empty
6 changes: 5 additions & 1 deletion tests/testthat/test-public_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ test_that("styler can style file", {
)
})

# style_active_file() must be tested manually.
test_that("styler does not return error when there is no file to style", {
expect_error(style_dir(paste0(base, "/xyzemptydir"), strict = FALSE), NA)
})