Rhino 1.10.0 Update: Automated Styling & Auto-complete for box Modules
In Rhino 1.7.0, we began introducing the Rhino style guide and linting for box::use() calls to promote best practices for code quality. These linter functions were eventually separated from Rhino 1.8.0 into the {box.linters} package.
Among the checks performed by {box.linters} are rules covering box::use() calls. For existing projects, manually modifying each source R file to get to zero lint can be tedious and time consuming. This is especially true for large code-base projects.
Find out how you can use {rhino} for production-grade Shiny practices in healthcare. Read our case study on complex scenario modeling.
It was tempting to partially or completely turn off linting to get a Rhino project to pass CI checks. However, this is counter to our doctrines about code quality. In Rhino 1.10.0, we are introducing automated styling for box::use() calls that conform to the Rhino style guide. This is provided by {box.linters} 0.10.5, and is now included in the existing rhino::format_r() function.
A long-standing pain-point of working with projects with large code-bases is code modularity. At Appsilon and with Rhino, we manage code modularity by using the {box} package. However, as it is with R6 classes, package and module functions syntax auto-completion and on-hover documentation are not available. For users of VSCode and Vim, these are provided by the {languageserver} package. In Rhino 1.10.0, we are introducing syntax auto-completion for functions attached by box::use() calls. This is provided by the new {box.lsp} package and is now, also, baked-into new Rhino apps.
box::use() Call Automated Styling
Styling modifies your code. As with {styler}, review the changes made by the styler functions.
{box.linters} 0.10.4 introduces three functions for styling box::use() calls:
- style_box_use_dir() to style files inside directories.
- style_box_use_file() to style one file.
- style_box_use_text() to style code contained in a text string.
{box.linters} styling modifies your box::use() calls to conform to the Rhino style guide:
- All packages are called under one and the same box::use() call.
- All local modules are called under one and the same box::use() call.
- box::use() calls are reformatted to multiple lines at the package and module level.
- Packages, modules, and functions are sorted alphabetically.
- Trailing commas are added.
For more information on styling guidelines, see the Rhino Style Guide.
Requirements:
- R version >= 4.3.0
- {treesitter} package
- {treesitter.r} package
For Rhino users starting 1.10.0, this is incorporated into rhino::format_r(). If the requirements are met, format_r() will style your box::use() calls.
For non-Rhino users and Rhino users with version < 1.10.0, box::use() styling can be executed by running one of the box.linters::style_* family of functions. For example, box.linters::style_box_use_dir(“.”) performs styling on all of your .R files in your current working directory.
{box} Module Syntax Auto-Completion in VSCode and Vim
Along with Rhino 1.10.0, for VSCode and Vim users, we are introducing {box.lsp} to provide syntax auto-completion support for {box} modules. {box.lsp} is an extension to {languageserver}.
As of {box.lsp} 0.1., the following works:
Requirements:
- VSCode or Vim
- {languageserver} package
For new Rhino apps created with Rhino version >= 1.10.0, your project already has {box.lsp} configured in your .Rprofile file.
For all other users, you can activate {languageserver} support for box modules by running box.lsp::use_box_lsp(). This modifies your .Rprofile file.
.Rprofile file lines for {languageserver} support for {box} modules:
# box.lsp languageserver external hook
options(
languageserver.parser_hooks = list(
"box::use" = box.lsp::box_use_parser
)
)
You may have to restart your R session (or restart your editor) for the extension to take effect.
{box.lsp} does not affect users of RStudio Desktop or Posit Workbench.
Summing Up Rhino 1.10.0 Update
Rhino 1.10.0 provides automated styling and syntax auto-completion for box modules, enhancing the coding experience and maintaining high code quality standards.
Want to stay up to date on the latest in Rhino and R/Shiny? Sign up today for our weekly newsletter.