1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
#' Interact with Documents open in RStudio
#'
#' Use these functions to interact with documents open in RStudio.
#'
#' @param location An object specifying the positions, or ranges, wherein text
#' should be inserted. See \bold{Details} for more information.
#'
#' @param text A character vector, indicating what text should be inserted at
#' each aforementioned range. This should either be length one (in which case,
#' this text is applied to each range specified); otherwise, it should be the
#' same length as the \code{ranges} list.
#'
#' @param id The document id. When \code{NULL} or blank, the requested operation
#' will apply to the currently open, or last focused, RStudio document.
#'
#' @param position The cursor position, typically created through
#' \code{\link{document_position}()}.
#'
#' @param ranges A list of one or more ranges, typically created
#' through \code{\link{document_range}()}.
#'
#' @param type The type of document to be created. One of \code{"r"} (default),
#' \code{"rmarkdown"}, or \code{"sql"}.
#'
#' @param execute Should the code be executed after the document
#' is created?
#'
#' @param allowConsole Allow the pseudo-id `#console` to be returned, if the \R
#' console is currently focused? Set this to `FALSE` if you'd always like to
#' target the currently-active or last-active editor in the Source pane.
#'
#' @details
#'
#' \code{location} should be a (list of) \code{\link{document_position}} or
#' \code{\link{document_range}} object(s), or numeric vectors coercable to
#' such objects.
#'
#' To operate on the current selection in a document, call \code{insertText()}
#' with only a text argument, e.g.
#'
#' \preformatted{
#' insertText("# Hello\\n")
#' insertText(text = "# Hello\\n")
#' }
#'
#' Otherwise, specify a (list of) positions or ranges, as in:
#'
#' \preformatted{
#' # insert text at the start of the document
#' insertText(c(1, 1), "# Hello\\n")
#'
#' # insert text at the end of the document
#' insertText(Inf, "# Hello\\n")
#'
#' # comment out the first 5 rows
#' pos <- Map(c, 1:5, 1)
#' insertText(pos, "# ")
#'
#' # uncomment the first 5 rows, undoing the previous action
#' rng <- Map(c, Map(c, 1:5, 1), Map(c, 1:5, 3))
#' modifyRange(rng, "")
#' }
#'
#' \code{modifyRange} is a synonym for \code{insertText}, but makes its intent
#' clearer when working with ranges, as performing text insertion with a range
#' will replace the text previously existing in that range with new text. For
#' clarity, prefer using \code{insertText} when working with
#' \code{\link{document_position}}s, and \code{modifyRange} when working with
#' \code{\link{document_range}}s.
#'
#' @note
#' The \code{insertText}, \code{modifyRange} and \code{setDocumentContents}
#' functions were added with version 0.99.796 of RStudio.
#'
#' The \code{setCursorPosition} and \code{setSelectionRanges} functions were
#' added with version 0.99.1111 of RStudio.
#'
#' The \code{documentSave} and \code{documentSaveAll} functions were added
#' with version 1.1.287 of RStudio.
#'
#' The \code{documentId} and \code{documentPath} functions were added with
#' version 1.4.843 of RStudio.
#'
#' @name rstudio-documents
NULL
#' @name rstudio-documents
#' @export
insertText <- function(location = NULL,
text = NULL,
id = NULL)
{
# unfortunate gymnastics needed for older versions of RStudio
if (getVersion() < "1.4")
{
if (is.null(location) && is.null(text))
{
callFun("insertText",
id = id)
}
else if (is.null(location))
{
callFun("insertText",
text = text,
id = id)
}
else if (is.null(text))
{
callFun("insertText",
location = location,
id = id)
}
else
{
callFun("insertText",
location = location,
text = text,
id = id)
}
}
else
{
callFun("insertText",
location = location,
text = text,
id = id)
}
}
#' @name rstudio-documents
#' @export
modifyRange <- insertText
#' @name rstudio-documents
#' @export
setDocumentContents <- function(text, id = NULL) {
location <- document_range(
document_position(1, 1),
document_position(Inf, 1)
)
insertText(location, text, id)
}
#' @name rstudio-documents
#' @export
setCursorPosition <- function(position, id = NULL) {
callFun("setSelectionRanges", position, id)
}
#' @name rstudio-documents
#' @export
setSelectionRanges <- function(ranges, id = NULL) {
callFun("setSelectionRanges", ranges, id)
}
#' @name rstudio-documents
#' @export
documentId <- function(allowConsole = TRUE) {
callFun("documentId", allowConsole = allowConsole)
}
#' @name rstudio-documents
#' @export
documentPath <- function(id = NULL) {
path <- callFun("documentPath", id = id)
Encoding(path) <- "UTF-8"
path
}
#' @name rstudio-documents
#' @export
documentSave <- function(id = NULL) {
callFun("documentSave", id)
}
#' @name rstudio-documents
#' @export
documentSaveAll <- function() {
callFun("documentSaveAll")
}
#' Retrieve Information about an RStudio Editor
#'
#' Returns information about an RStudio editor.
#'
#' The \code{selection} field returned is a list of document selection objects.
#' A document selection is just a pairing of a document \code{range}, and the
#' \code{text} within that range.
#'
#' @note
#' The \code{getActiveDocumentContext} function was added with version 0.99.796
#' of RStudio, while the \code{getSourceEditorContext} and the \code{getConsoleEditorContext}
#' functions were added with version 0.99.1111.
#'
#' @return A \code{list} with elements:
#' \tabular{ll}{
#' \code{id} \tab The document ID.\cr
#' \code{path} \tab The path to the document on disk.\cr
#' \code{contents} \tab The contents of the document.\cr
#' \code{selection} \tab A \code{list} of selections. See \bold{Details} for more information.\cr
#' }
#'
#' @param id The ID of a particular document, as retrieved by `documentId()`
#' or similar. Supported in RStudio 2022.06.0 or newer.
#'
#' @rdname rstudio-editors
#' @name rstudio-editors
#' @export
getActiveDocumentContext <- function() {
getDocumentContext("getActiveDocumentContext")
}
#' @name rstudio-editors
#' @export
getSourceEditorContext <- function(id = NULL) {
getDocumentContext("getSourceEditorContext", id)
}
#' @name rstudio-editors
#' @export
getConsoleEditorContext <- function() {
getDocumentContext("getConsoleEditorContext")
}
#' @note The \code{documentNew} function was introduced in RStudio 1.2.640.
#'
#' @name rstudio-documents
#' @export
documentNew <- function(
text,
type = "r",
position = document_position(0, 0),
execute = FALSE)
{
type <- match.arg(type, choices = c("r", "rmarkdown", "sql"))
callFun("documentNew", type, text, position[1], position[2], execute)
}
#' @param path The path to the document.
#' @param line The line in the document to navigate to.
#' @param col The column in the document to navigate to.
#' @param moveCursor Boolean; move the cursor to the requested location after
#' opening the document?
#'
#' @note The \code{documentOpen} function was introduced in RStudio 1.4.1106.
#'
#' @name rstudio-documents
#' @export
documentOpen <- function(
path,
line = -1L,
col = -1L,
moveCursor = TRUE)
{
path <- normalizePath(path, winslash = "/", mustWork = TRUE)
callFun("documentOpen", path, line, col, moveCursor)
}
#' @param save Whether to commit unsaved changes to the document before closing it.
#'
#' @note The \code{documentClose} function was introduced in RStudio 1.2.1255
#'
#' @details
#'
#' \code{documentClose} accepts an ID of an open document rather than a path.
#' You can retrieve the ID of the active document using the \code{documentId()}
#' function.
#'
#' Closing is always done non-interactively; that is, no prompts are given to
#' the user. If the user has made changes to the document but not saved them,
#' then the \code{save} parameter governs the behavior: when \code{TRUE},
#' unsaved changes are committed, and when \code{FALSE} they are discarded.
#'
#' @name rstudio-documents
#' @export
documentClose <- function(id = NULL, save = TRUE) {
callFun("documentClose", id, save)
}
|