Skip to content

Commit

Permalink
Merge branch 'integrate-box-linters-styling' into integrate-box-lsp
Browse files Browse the repository at this point in the history
  • Loading branch information
radbasa committed Sep 3, 2024
2 parents eae5f03 + 631e7fd commit a37f75a
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 5 deletions.
4 changes: 4 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ Depends:
R (>= 2.10)
Imports:
box (>= 1.1.3),
<<<<<<< HEAD
box.linters (>= 0.9.1),
box.lsp,
=======
box.linters (>= 0.10.2),
>>>>>>> integrate-box-linters-styling
cli,
config,
fs,
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# rhino (development version)

* Integrate `box.lsp` for auto-complete support for `box` modules in VS Code.
* Integrated {box.linters} styling functions to style `box::use()` calls according to the Rhino style guide.
* Added compatibility check for `treesitter` and `treesitter.r` dependencies

# [rhino 1.9.0](https://github.com/Appsilon/rhino/releases/tag/v1.9.0)

Expand Down
3 changes: 3 additions & 0 deletions R/dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ read_dependencies <- function() {
}

write_dependencies <- function(deps) {
if (as.numeric(R.Version()$major) >= 4 && as.numeric(R.Version()$minor) >= 3.0) {
deps <- c(deps, "treesitter", "treesitter.r")
}
deps <- sort(unique(c("rhino", deps))) # Rhino is always needed as a dependency.
deps <- purrr::map_chr(deps, function(name) glue::glue("library({name})"))
deps <- c(
Expand Down
44 changes: 41 additions & 3 deletions R/tools.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ check_paths <- function(paths) {
#' with the `legacy_max_lint_r_errors` option in `rhino.yml`.
#' This can be useful when inheriting legacy code with multiple styling issues.
#'
#' The [box.linters::namespaced_function_calls()] linter requires the `{treesitter}` and
#' `{treesitter.r}` packages. These require R >= 4.3.0. `lint_r()` will continue to run and skip
#' `namespaced_function_calls()` if its dependencies are not available.
#'
#' @param paths Character vector of directories and files to lint.
#' When `NULL` (the default), check `app` and `tests/testthat` directories.
#'
Expand All @@ -87,6 +91,17 @@ check_paths <- function(paths) {
#' @export
# nolint end
lint_r <- function(paths = NULL) {
if (!box.linters::is_treesitter_installed()) {
cli::cli_warn(
c(
"!" = paste(
"`box.linters::namespaced_function_calls()` requires {{treesitter}} and {{treesitter.r}}",
"to be installed."
),
"i" = "`lintr_r()` will continue to run using the other linter functions."
)
)
}
if (is.null(paths)) {
paths <- c("app", "tests/testthat")
}
Expand Down Expand Up @@ -122,12 +137,22 @@ rhino_style <- function() {

#' Format R
#'
#' Uses the `{styler}` package to automatically format R sources.
#' Uses the `{styler}` and `{box.linters}` packages to automatically format R sources.
#'
#' The code is formatted according to the `styler::tidyverse_style` guide with one adjustment:
#' spacing around math operators is not modified to avoid conflicts with `box::use()` statements.
#'
#' `box.linters::style_*` functions require the `treesitter` and `treesitter.r` packages. These, in
#' turn, require R >= 4.3.0. `box.linters` provides styling functions specific to the `box::use()`
#' calls. These include:
#' * Separate `box::use()` calls for packages and local modules
#' * Alphabetically sorted packages, modules, and functions.
#' * Trailing commas
#'
#'
#' @param paths Character vector of files and directories to format.
#' @param exclude_files Character vector with regular expressions of files that should be excluded
#' from styling.
#' @return None. This function is called for side effects.
#'
#' @examples
Expand All @@ -139,11 +164,24 @@ rhino_style <- function() {
#' format_r("app/view")
#' }
#' @export
format_r <- function(paths) {
format_r <- function(paths, exclude_files = NULL) {
style_box_use <- box.linters::style_box_use_dir
if (!box.linters::is_treesitter_installed()) {
style_box_use <- function(path, exclude_files) { }
cli::cli_warn(
c(
"x" = "The packages {{treesitter}} and {{treesitter.r}} are required by some features of `format_r()`.", #nolint
"i" = "These package require R version >= 4.3.0 to install."
)
)
}

for (path in paths) {
if (fs::is_dir(path)) {
styler::style_dir(path, style = rhino_style)
style_box_use(path, exclude_files = exclude_files)
styler::style_dir(path, style = rhino_style, exclude_files = exclude_files)
} else {
style_box_use(path, exclude_files = exclude_files)
styler::style_file(path, style = rhino_style)
}
}
Expand Down
16 changes: 14 additions & 2 deletions man/format_r.Rd

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

4 changes: 4 additions & 0 deletions man/lint_r.Rd

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

2 changes: 2 additions & 0 deletions tests/e2e/test-lint-r.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
install.packages(c("treesitter", "treesitter.r"))

rhino::lint_r()
# Create bad scripts and test if formatting returns the expected result
test_file_path <- fs::path("app", "logic", "bad-style.R")
Expand Down

0 comments on commit a37f75a

Please sign in to comment.