Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RStudio Addins #23

Merged
merged 12 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/rpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
- { os: ubuntu-latest, r: "oldrel-1" }
- { os: ubuntu-latest, r: "oldrel-2" }


env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Cargo.lock
# MacOS
.DS_Store

tergo.Rproj
# RStudio
.Rproj.user
tergo.Rproj

1 change: 1 addition & 0 deletions antidotum/tergo/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Depends:
Suggests:
rextendr (== 0.3.1),
roxygen2,
rstudioapi (>= 0.7),
devtools,
pkgdown,
desc
Expand Down
99 changes: 99 additions & 0 deletions antidotum/tergo/R/addins.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#' Check for required RStudio API package
#'
#' Ensures the `rstudioapi` package is available, as it is required for addin functionality.
check_rstudioapi <- function() {
if (!requireNamespace("rstudioapi", quietly = TRUE)) {
stop("The 'rstudioapi' package is required for this addin.")
}
}

#' Style the current package (RStudio addin)
#'
#' Automatically styles all R code in the current project/package using
#' \code{\link[tergo]{style_pkg}}. If not called within a project, it
#' defaults to the current working directory.
#'
#' @keywords internal
style_pkg_addin <- function() {
check_rstudioapi()

# Detect current project directory (fallback to working directory)
project_path <- rstudioapi::getActiveProject()
if (is.null(project_path)) {
project_path <- getwd()
}

# Attempt styling silently
result <- try(tergo::style_pkg(path = project_path), silent = TRUE)

if (inherits(result, "try-error")) {
stop("tergo::style_pkg failed to style the package.")
}

invisible(NULL)
}

#' Style the active file (RStudio addin)
#'
#' Styles the currently active file in the RStudio editor and saves the formatted code.
#'
#' @keywords internal
style_active_file_addin <- function() {
check_rstudioapi()

# Get the source editor context
context <- rstudioapi::getActiveDocumentContext()
file_path <- context$path

# Ensure the file is saved before attempting to style
if (is.null(file_path) || file_path == "") {
rstudioapi::showDialog(title = "Cannot style", message = "Please save the document before styling.")
return(invisible(NULL))
}

# Attempt styling silently
result <- try(tergo::style_file(file_path), silent = TRUE)

if (inherits(result, "try-error")) {
stop(sprintf("tergo::style_file failed to style the %s file.", file_path))
}

invisible(NULL)
}

#' Style the selected text (RStudio addin)
#'
#' Styles the selected text in the RStudio editor, replacing it with the formatted version.
#'
#' @keywords internal
style_selection_addin <- function() {
check_rstudioapi()

# Get the source editor context
context <- rstudioapi::getSourceEditorContext()

# Check if there are any selections
if (length(context$selection) == 0) {
rstudioapi::showDialog(title = "No selection found", message = "Please select some code before using this addin.")
return(invisible(NULL))
}

text <- context$selection[[1L]]$text
range <- context$selection[[1L]]$range

result <- try(
expr = {
styled_text <- tergo::style_text(text)
rstudioapi::modifyRange(range, styled_text, id = context$id)
},
silent = TRUE
)

# Show dialog only if all selections were successfully styled
if (inherits(result, "try-error")) {
stop("tergo::style_text failed to style the text.")
}

invisible(rstudioapi::documentSave(context$id))
}

1 change: 1 addition & 0 deletions antidotum/tergo/R/styling.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ style_pkg <- function(path = ".",
}
)
}
message(sprintf("Sucessfully styled %i files.", length(files)))
}

#' Style a file
Expand Down
17 changes: 17 additions & 0 deletions antidotum/tergo/inst/rstudio/addins.dcf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Name: Style This Package
Description: Recursively style R files in the current package.
Binding: style_pkg_addin
Interactive: true
Package: tergo

Name: Style Active File
Description: Style the currently open file in the RStudio editor using tergo.
Binding: style_active_file_addin
Interactive: true
Package: tergo

Name: Style Selected Text
Description: Style only the currently highlighted code in the editor using tergo.
Binding: style_selection_addin
Interactive: true
Package: tergo
14 changes: 14 additions & 0 deletions antidotum/tergo/man/style_active_file_addin.Rd

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

12 changes: 12 additions & 0 deletions antidotum/tergo/man/style_pkg_addin.Rd

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

14 changes: 14 additions & 0 deletions antidotum/tergo/man/style_selection_addin.Rd

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

Loading