From 8c8d44e03bec56c2fb97c9830ef2f460a5732221 Mon Sep 17 00:00:00 2001 From: Konrad Pagacz Date: Sun, 10 Nov 2024 18:31:17 +0200 Subject: [PATCH] feat: deal with trailing trimming and force and extensions in R styling --- antidotum/tergo/.Rbuildignore | 1 + antidotum/tergo/.lintr | 3 +- antidotum/tergo/DESCRIPTION | 2 +- antidotum/tergo/R/extendr-wrappers.R | 2 +- antidotum/tergo/R/styling.R | 42 +++++++++++++++++++++++----- antidotum/tergo/README.md | 2 +- antidotum/tergo/man/style.Rd | 9 +++--- antidotum/tergo/man/style_file.Rd | 2 +- antidotum/tergo/man/style_pkg.Rd | 21 +++++++++++--- antidotum/tergo/man/style_text.Rd | 2 +- antidotum/tergo/man/tergo-package.Rd | 2 +- antidotum/tergo/src/rust/Cargo.toml | 2 +- antidotum/tergo/tools/configure.R | 7 +++-- antidotum/tergo/update_authors.R | 1 - balnea/Cargo.toml | 4 +-- unguentum/Cargo.toml | 2 +- unguentum/src/lib.rs | 4 +-- unguentum/src/post_format_hooks.rs | 7 +++++ 18 files changed, 83 insertions(+), 32 deletions(-) diff --git a/antidotum/tergo/.Rbuildignore b/antidotum/tergo/.Rbuildignore index de9742b4..910a17be 100644 --- a/antidotum/tergo/.Rbuildignore +++ b/antidotum/tergo/.Rbuildignore @@ -3,3 +3,4 @@ ^\.lintr ^pre-commit\.sh ^update_authors\.R +^\_pkgdown\.yml diff --git a/antidotum/tergo/.lintr b/antidotum/tergo/.lintr index 7f21acdc..9db3bb99 100644 --- a/antidotum/tergo/.lintr +++ b/antidotum/tergo/.lintr @@ -1,3 +1,4 @@ linters: linters_with_defaults( - line_length_linter(120L) + line_length_linter(120L), + cyclocomp_linter(complexity_limit = 50L) ) diff --git a/antidotum/tergo/DESCRIPTION b/antidotum/tergo/DESCRIPTION index d15c93ea..1501831a 100644 --- a/antidotum/tergo/DESCRIPTION +++ b/antidotum/tergo/DESCRIPTION @@ -23,6 +23,6 @@ Suggests: devtools, pkgdown, desc -URL: https://r.tergo.pagacz.io, +URL: https://rtergo.pagacz.io, https://github.com/kpagacz/tergo BugReports: https://github.com/kpagacz/tergo/issues diff --git a/antidotum/tergo/R/extendr-wrappers.R b/antidotum/tergo/R/extendr-wrappers.R index 6083f7f0..7403065c 100644 --- a/antidotum/tergo/R/extendr-wrappers.R +++ b/antidotum/tergo/R/extendr-wrappers.R @@ -36,5 +36,5 @@ get_config <- function(path) .Call(wrap__get_config, path) #' @export get_default_config <- function() .Call(wrap__get_default_config) - # nolint end + diff --git a/antidotum/tergo/R/styling.R b/antidotum/tergo/R/styling.R index 1ab809c3..037c06db 100644 --- a/antidotum/tergo/R/styling.R +++ b/antidotum/tergo/R/styling.R @@ -6,24 +6,47 @@ #' 1. The configuration passed to the function. #' 2. The configuration file. #' -#' @param config_file (`character`) the path to the configuration file -#' @param configuration (`list`) the path to the configuration for formatting +#' @param config_file (`character`) The path to the configuration file +#' @param configuration (`list`) The path to the configuration for formatting +#' @param ... additional parameters to [tergo::style_pkg()] #' #' @export #' @examples #' style() #' style(config_file = "tergo.toml", configuration = list()) -style <- function(config_file = "tergo.toml", configuration = list()) { +style <- function(config_file = "tergo.toml", configuration = list(), ...) { style_pkg(path = getwd(), config_file = config_file, configuration = configuration) } #' Style a package #' #' @inheritParams style -#' @param path (`character`) the path to the package +#' @param path (`character`) The path to the package. +#' @param force (`logical(1`) Whether to format the files even +#' if no package was found. `TRUE` - format the `.R` and `.r` files +#' found in the directory (recursive). `FALSE` exit without formatting +#' anything. +#' @param extensions (`character`) The extensions of the files to format. #' #' @export -style_pkg <- function(path = ".", config_file = "tergo.toml", configuration = list()) { +style_pkg <- function(path = ".", + config_file = "tergo.toml", + configuration = list(), + force = FALSE, + extensions = c(".R", ".r")) { + if (!is.character(path) || length(path) != 1) { + stop("Path must be a character") + } + if (!is.character(config_file) || length(config_file) != 1) { + stop("Config file must be a character") + } + if (!is.logical(force) || length(force) != 1) { + stop("Force must be a logical") + } + if (!is.list(configuration)) { + stop("Configuration must be a list") + } + # Read a configuration file wd <- path config <- NULL @@ -55,9 +78,14 @@ style_pkg <- function(path = ".", config_file = "tergo.toml", configuration = li break } } - files <- list.files(package_root, recursive = TRUE, full.names = TRUE) - files <- Filter(function(file) any(endsWith(file, c(".R", ".r"))), files) + if (!file.exists(file.path(package_root, "DESCRIPTION")) && !force) { + message("No package detected. Exiting without formatting anything.") + return(invisible()) + } + + files <- list.files(package_root, recursive = TRUE, full.names = TRUE) + files <- Filter(function(file) any(endsWith(file, extensions)), files) # Format for (file in files) { tryCatch( diff --git a/antidotum/tergo/README.md b/antidotum/tergo/README.md index 4abf105a..5e5da0a8 100644 --- a/antidotum/tergo/README.md +++ b/antidotum/tergo/README.md @@ -46,7 +46,7 @@ install.packages('tergo', repos = c('https://kpagacz.r-universe.dev', 'https://c ## Usage -See the R manual for the reference - . +See the [R manual](rtergo.pagacz.io) for the reference. To style your package, run: diff --git a/antidotum/tergo/man/style.Rd b/antidotum/tergo/man/style.Rd index 33590dce..dbad07e3 100644 --- a/antidotum/tergo/man/style.Rd +++ b/antidotum/tergo/man/style.Rd @@ -4,12 +4,14 @@ \alias{style} \title{Style a package} \usage{ -style(config_file = "tergo.toml", configuration = list()) +style(config_file = "tergo.toml", configuration = list(), ...) } \arguments{ -\item{config_file}{(\code{character}) the path to the configuration file} +\item{config_file}{(\code{character}) The path to the configuration file} + +\item{configuration}{(\code{list}) The path to the configuration for formatting} -\item{configuration}{(\code{list}) the path to the configuration for formatting} +\item{...}{additional parameters to \code{\link[=style_pkg]{style_pkg()}}} } \description{ Style a package @@ -25,5 +27,4 @@ package. The precedence of the configuration is (from the highest to lowest): \examples{ style() style(config_file = "tergo.toml", configuration = list()) - } diff --git a/antidotum/tergo/man/style_file.Rd b/antidotum/tergo/man/style_file.Rd index 3a1a2beb..d68d1fd8 100644 --- a/antidotum/tergo/man/style_file.Rd +++ b/antidotum/tergo/man/style_file.Rd @@ -9,7 +9,7 @@ style_file(file, configuration = list()) \arguments{ \item{file}{(\code{character}) the file to format} -\item{configuration}{(\code{list}) the path to the configuration for formatting} +\item{configuration}{(\code{list}) The path to the configuration for formatting} } \description{ Style a file diff --git a/antidotum/tergo/man/style_pkg.Rd b/antidotum/tergo/man/style_pkg.Rd index be060a0d..35d5c794 100644 --- a/antidotum/tergo/man/style_pkg.Rd +++ b/antidotum/tergo/man/style_pkg.Rd @@ -4,14 +4,27 @@ \alias{style_pkg} \title{Style a package} \usage{ -style_pkg(path = ".", config_file = "tergo.toml", configuration = list()) +style_pkg( + path = ".", + config_file = "tergo.toml", + configuration = list(), + force = FALSE, + extensions = c(".R", ".r") +) } \arguments{ -\item{path}{(\code{character}) the path to the package} +\item{path}{(\code{character}) The path to the package.} -\item{config_file}{(\code{character}) the path to the configuration file} +\item{config_file}{(\code{character}) The path to the configuration file} -\item{configuration}{(\code{list}) the path to the configuration for formatting} +\item{configuration}{(\code{list}) The path to the configuration for formatting} + +\item{force}{(\verb{logical(1}) Whether to format the files even +if no package was found. \code{TRUE} - format the \code{.R} and \code{.r} files +found in the directory (recursive). \code{FALSE} exit without formatting +anything.} + +\item{extensions}{(\code{character}) The extensions of the files to format.} } \description{ Style a package diff --git a/antidotum/tergo/man/style_text.Rd b/antidotum/tergo/man/style_text.Rd index 3ec22c77..06c2e9ac 100644 --- a/antidotum/tergo/man/style_text.Rd +++ b/antidotum/tergo/man/style_text.Rd @@ -9,7 +9,7 @@ style_text(text, configuration = list()) \arguments{ \item{text}{(\code{character}) the text to style} -\item{configuration}{(\code{list}) the path to the configuration for formatting} +\item{configuration}{(\code{list}) The path to the configuration for formatting} } \value{ (\code{character}) the text formatted as R code diff --git a/antidotum/tergo/man/tergo-package.Rd b/antidotum/tergo/man/tergo-package.Rd index e3eee13d..bb70f1df 100644 --- a/antidotum/tergo/man/tergo-package.Rd +++ b/antidotum/tergo/man/tergo-package.Rd @@ -11,7 +11,7 @@ Style your code FAST. \seealso{ Useful links: \itemize{ - \item \url{https://r.tergo.pagacz.io} + \item \url{https://rtergo.pagacz.io} \item \url{https://github.com/kpagacz/tergo} \item Report bugs at \url{https://github.com/kpagacz/tergo/issues} } diff --git a/antidotum/tergo/src/rust/Cargo.toml b/antidotum/tergo/src/rust/Cargo.toml index 14981df6..06118ea2 100644 --- a/antidotum/tergo/src/rust/Cargo.toml +++ b/antidotum/tergo/src/rust/Cargo.toml @@ -8,7 +8,7 @@ crate-type = ["staticlib"] name = "tergo" [dependencies] -tergo-lib = "0.2.4" +tergo-lib = "0.2.5" toml = "0.8.19" extendr-api = "*" diff --git a/antidotum/tergo/tools/configure.R b/antidotum/tergo/tools/configure.R index 2cc576ab..27984878 100644 --- a/antidotum/tergo/tools/configure.R +++ b/antidotum/tergo/tools/configure.R @@ -152,8 +152,10 @@ check_cargo <- function() { ### Check cargo toolchain ### -cargo_check_result <- tryCatch(check_cargo(), # Defer errors if it's raised by functions here -string2path_error_cargo_check = function(e) e$message) +cargo_check_result <- tryCatch( + check_cargo(), # Defer errors if it's raised by functions here + string2path_error_cargo_check = function(e) e$message +) # If cargo is confirmed fine, exit here. But, even if the cargo is not available # or too old, it's not the end of the world. There might be a pre-compiled @@ -180,4 +182,3 @@ Please refer to to install Rust. ) ) quit("no", status = 2) - diff --git a/antidotum/tergo/update_authors.R b/antidotum/tergo/update_authors.R index f4803b40..62cf4af4 100644 --- a/antidotum/tergo/update_authors.R +++ b/antidotum/tergo/update_authors.R @@ -129,4 +129,3 @@ cat( file = "LICENSE.note", append = TRUE ) - diff --git a/balnea/Cargo.toml b/balnea/Cargo.toml index acbd4e9a..3dde4894 100644 --- a/balnea/Cargo.toml +++ b/balnea/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tergo-lib" -version = "0.2.4" +version = "0.2.5" edition = "2021" description = "A tool to format R code" license = "MIT" @@ -14,7 +14,7 @@ path = "src/lib.rs" [dependencies] tokenizer = { package = "tergo-tokenizer", path = "../aqua", version = "0.2.1" } parser = { package = "tergo-parser", path = "../spongia", version = "0.2.1" } -formatter = { package = "tergo-formatter", path = "../unguentum", version = "0.2.2" } +formatter = { package = "tergo-formatter", path = "../unguentum", version = "0.2.4" } log = "0.4.21" env_logger = "0.11.3" serde = { version = "1.0.210", features = ["derive"] } diff --git a/unguentum/Cargo.toml b/unguentum/Cargo.toml index b13453e2..2c589a12 100644 --- a/unguentum/Cargo.toml +++ b/unguentum/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tergo-formatter" -version = "0.2.3" +version = "0.2.4" edition = "2021" license = "MIT" description = "Formatter for tergo" diff --git a/unguentum/src/lib.rs b/unguentum/src/lib.rs index 50e7b36d..5c94ecb0 100644 --- a/unguentum/src/lib.rs +++ b/unguentum/src/lib.rs @@ -12,6 +12,7 @@ use crate::format::Mode; use log::trace; use parser::ast::Expression; use post_format_hooks::trim_line_endings; +use post_format_hooks::trim_trailing_line; use std::collections::VecDeque; use std::rc::Rc; @@ -54,11 +55,10 @@ pub fn format_code( let mut formatted = simple_doc_to_string(simple_doc); // Post-format hooks - let post_format_hooks = vec![trim_line_endings]; + let post_format_hooks = vec![trim_line_endings, trim_trailing_line]; for hook in post_format_hooks { formatted = hook(formatted); } - // Add a new line because trimming whitespace removes the trailing line formatted } diff --git a/unguentum/src/post_format_hooks.rs b/unguentum/src/post_format_hooks.rs index e23965f8..14f259c8 100644 --- a/unguentum/src/post_format_hooks.rs +++ b/unguentum/src/post_format_hooks.rs @@ -6,3 +6,10 @@ pub(crate) fn trim_line_endings(s: String) -> String { acc }) } + +pub(crate) fn trim_trailing_line(mut s: String) -> String { + let trimmed = s.trim_end(); + s.truncate(trimmed.len()); + s.push('\n'); + s +}