Skip to content

Commit

Permalink
use chromote::find_chrome()
Browse files Browse the repository at this point in the history
  • Loading branch information
stla committed Jul 1, 2023
1 parent 7052e1e commit 47c47c9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Type: Package
Package: prettifyAddins
Title: 'RStudio' Addins to Prettify 'JavaScript', 'C++', 'Python', and
More
Version: 2.6.0
Version: 2.6.0.9000
Authors@R: c(
person("Stéphane", "Laurent", , "laurent_step@outlook.fr", role = c("aut", "cre")),
person("James Long and contributors", role = "cph",
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# prettifyAddins 2.6.0.9000

The function `reindent_chromote` now uses `chromote::find_chrome` to find the
path of the executable of the browser.


# prettifyAddins 2.6.0

New function `reindent_chromote`. It is similar to `reindent_PhantomJS` but it
Expand Down
27 changes: 18 additions & 9 deletions R/prettify_chromote.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
#'
#' @return The reindented code in a character string.
#'
#' @note This function requires 'Chrome' and the executable file 'chrome'
#' must be in the system path.
#' @note This function uses \code{chromote::find_chrome()} to find the
#' executable of Google Chrome or another Chromium-based browser. If it is
#' not found you will get an error. In this case set the environment variable
#' \code{CHROMOTE_CHROME} to the path of such an executable
#' (e.g. \code{Sys.setenv(CHROMOTE_CHROME = "path/to/chrome.exe")}).
#'
#' @importFrom rstudioapi isAvailable
#' @importFrom tools file_ext
Expand All @@ -39,12 +42,17 @@
#' if(Sys.which("chrome") != "") {
#' cat(reindent_chromote(code, "python"))
#' }
reindent_chromote <- function(contents = NA, language = NA, tabSize = NULL){
reindent_chromote <- function(contents = NA, language = NA, tabSize = NULL) {

if(Sys.which("chrome") == ""){
chromePath <- Sys.which("chrome")
if(is.null(chromePath)) {
chromePath <- find_chrome()
}
if(is.null(chromePath)){
stop(
"This function requires 'Chrome' and the executable file ",
"'chrome' must be in the path."
"This function requires the Google Chrome browser or any Chromium-based ",
"browser and none has been found. Try setting the 'CHROMOTE_CHROME' ",
"environment variable to the path of an executable of such a browser."
)
}

Expand Down Expand Up @@ -120,7 +128,7 @@ reindent_chromote <- function(contents = NA, language = NA, tabSize = NULL){
mode <- file.path(folder, "mode", mode, paste0(mode, ".js"))

chrm <- Chrome$new(
path = "chrome",
path = chromePath,
args = "--disable-gpu --headless --remote-debugging-port=9222"
)
chromote <- Chromote$new(browser = chrm)
Expand Down Expand Up @@ -159,8 +167,9 @@ editor.setCursor(0);
', language, tabSize, tabSize))

result <- session$Runtime$evaluate("editor.getValue();")
value <- result$result$value

. <- session$close()
on.exit(session$close())

result$result$value
value
}
7 changes: 5 additions & 2 deletions man/reindent_chromote.Rd

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

0 comments on commit 47c47c9

Please sign in to comment.