diff --git a/R/rules-other.R b/R/rules-other.R index 418b433ad..754849960 100644 --- a/R/rules-other.R +++ b/R/rules-other.R @@ -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) diff --git a/R/transform.R b/R/transform.R index 87f25783f..f63d1bc1d 100644 --- a/R/transform.R +++ b/R/transform.R @@ -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 diff --git a/man/transform_file.Rd b/man/transform_file.Rd new file mode 100644 index 000000000..e617999d0 --- /dev/null +++ b/man/transform_file.Rd @@ -0,0 +1,33 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/transform.R +\name{transform_file} +\alias{transform_file} +\title{Transform a file an give customized message} +\usage{ +transform_file(path, fun, verbose = FALSE, max_char_path, + message_before = "", message_after = " [DONE]", + message_after_if_changed = " *", ...) +} +\arguments{ +\item{path}{A vector of file paths.} + +\item{fun}{A function that returns a character vector.} + +\item{verbose}{Should the function show a message with a list of changed +files?} + +\item{max_char_path}{The number of characters of the longest path. Determines +the indention level of \code{message_after}.} + +\item{message_before}{The message to print before the path.} + +\item{message_after}{The message to print after the path.} + +\item{message_after_if_changed}{The message to print after \code{message_after} if +any file was transformed.} + +\item{...}{Further arguments passed to \code{utf8::transform_lines_enc()}.} +} +\description{ +Wraps \code{utf8::transform_lines_enc()} and gives customized messages. +} diff --git a/tests/testthat/public-api/xyzemptydir/non_r_file b/tests/testthat/public-api/xyzemptydir/non_r_file new file mode 100644 index 000000000..1bb8bf6d7 --- /dev/null +++ b/tests/testthat/public-api/xyzemptydir/non_r_file @@ -0,0 +1 @@ +# empty diff --git a/tests/testthat/test-public_api.R b/tests/testthat/test-public_api.R index 68546db82..2d9e68e04 100644 --- a/tests/testthat/test-public_api.R +++ b/tests/testthat/test-public_api.R @@ -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) +}) + +