From 1a7b8070b5b9fc68ddd41e58cbcf7db6c15e7ec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Thu, 20 Jun 2024 13:05:54 +0200 Subject: [PATCH] Do not fail syntax highlighting in RStudio w/o rstudioapi Closes #697. --- NEWS.md | 3 +++ R/prettycode.R | 12 +++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/NEWS.md b/NEWS.md index 7dda06b2e..5cfae06bb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,6 +8,9 @@ * The `{.obj_type_friendly}` inline style now only shows the first class name (#669 @olivroy). +* Syntax highlighting now does not fail in RStudio if the rstudioapi + package is not installed (#697). + # cli 3.6.2 * `ansi_collapse(x, trunc = 1, style = "head")` now indeed shows one diff --git a/R/prettycode.R b/R/prettycode.R index 74b568a58..7c695efbc 100644 --- a/R/prettycode.R +++ b/R/prettycode.R @@ -240,12 +240,14 @@ code_theme_default <- function() { if (rs$type %in% c("rstudio_console", "rstudio_console_starting")) { opt <- code_theme_opt("cli.code_theme_rstudio") if (!is.null(opt)) return(opt) - code_theme_default_rstudio() - } else { - opt <- code_theme_opt("cli.code_theme_terminal") - if (!is.null(opt)) return(opt) - code_theme_default_term() + if (requireNamespace("rstudioapi", quietly = TRUE)) { + return(code_theme_default_rstudio()) + } } + + opt <- code_theme_opt("cli.code_theme_terminal") + if (!is.null(opt)) return(opt) + code_theme_default_term() } code_theme_opt <- function(option) {