Skip to content

Commit

Permalink
feat: deal with trailing trimming and force and extensions in R styling
Browse files Browse the repository at this point in the history
  • Loading branch information
kpagacz committed Nov 10, 2024
1 parent 52117db commit 8c8d44e
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 32 deletions.
1 change: 1 addition & 0 deletions antidotum/tergo/.Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
^\.lintr
^pre-commit\.sh
^update_authors\.R
^\_pkgdown\.yml
3 changes: 2 additions & 1 deletion antidotum/tergo/.lintr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
linters: linters_with_defaults(
line_length_linter(120L)
line_length_linter(120L),
cyclocomp_linter(complexity_limit = 50L)
)
2 changes: 1 addition & 1 deletion antidotum/tergo/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion antidotum/tergo/R/extendr-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

42 changes: 35 additions & 7 deletions antidotum/tergo/R/styling.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion antidotum/tergo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ install.packages('tergo', repos = c('https://kpagacz.r-universe.dev', 'https://c

## Usage

See the R manual for the reference - <r.tergo.pagacz.io>.
See the [R manual](rtergo.pagacz.io) for the reference.

To style your package, run:

Expand Down
9 changes: 5 additions & 4 deletions antidotum/tergo/man/style.Rd

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

2 changes: 1 addition & 1 deletion antidotum/tergo/man/style_file.Rd

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

21 changes: 17 additions & 4 deletions antidotum/tergo/man/style_pkg.Rd

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

2 changes: 1 addition & 1 deletion antidotum/tergo/man/style_text.Rd

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

2 changes: 1 addition & 1 deletion antidotum/tergo/man/tergo-package.Rd

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

2 changes: 1 addition & 1 deletion antidotum/tergo/src/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "*"

Expand Down
7 changes: 4 additions & 3 deletions antidotum/tergo/tools/configure.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -180,4 +182,3 @@ Please refer to <https://www.rust-lang.org/tools/install> to install Rust.
)
)
quit("no", status = 2)

1 change: 0 additions & 1 deletion antidotum/tergo/update_authors.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,3 @@ cat(
file = "LICENSE.note",
append = TRUE
)

4 changes: 2 additions & 2 deletions balnea/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"] }
2 changes: 1 addition & 1 deletion unguentum/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tergo-formatter"
version = "0.2.3"
version = "0.2.4"
edition = "2021"
license = "MIT"
description = "Formatter for tergo"
Expand Down
4 changes: 2 additions & 2 deletions unguentum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -54,11 +55,10 @@ pub fn format_code<T: config::FormattingConfig>(
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
}
7 changes: 7 additions & 0 deletions unguentum/src/post_format_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 8c8d44e

Please sign in to comment.