-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* addins --------- Co-authored-by: Konrad Pagacz <konrad.pagacz@gmail.com>
- Loading branch information
Showing
9 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ Cargo.lock | |
# MacOS | ||
.DS_Store | ||
|
||
tergo.Rproj | ||
# RStudio | ||
.Rproj.user | ||
tergo.Rproj | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ Depends: | |
Suggests: | ||
rextendr (== 0.3.1), | ||
roxygen2, | ||
rstudioapi (>= 0.7), | ||
devtools, | ||
pkgdown, | ||
desc | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.