Skip to content

Commit

Permalink
provide 'setDocumentContents' API
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinushey committed Dec 3, 2015
1 parent 701799b commit 148be88
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 20 deletions.
5 changes: 3 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ S3method(print,document_selection)
export(as.document_position)
export(as.document_range)
export(callFun)
export(document_position)
export(document_range)
export(findFun)
export(getActiveDocumentContext)
export(getVersion)
Expand All @@ -21,10 +23,9 @@ export(insertText)
export(is.document_position)
export(is.document_range)
export(isAvailable)
export(makePosition)
export(makeRange)
export(navigateToFile)
export(previewRd)
export(setDocumentContents)
export(sourceMarkers)
export(verifyAvailable)
export(versionInfo)
Expand Down
25 changes: 24 additions & 1 deletion R/document-api.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@ insertText <- function(location, text, id = NULL) {
callFun("insertText", location, text, id)
}

#' Set the Contents of a Document
#'
#' Set the contents of a document, deleting any other text existing in the
#' document previously.
#'
#' @param text The text to insert into the document.
#'
#' @param id The document id. When \code{NULL} or blank,
#' the mutation will apply to the currently open, or last
#' focused, RStudio document. Use the \code{id} returned
#' from \code{\link{getActiveDocumentContext}()} to ensure
#' that the operation is applied on the intended document.
#' @export
setDocumentContents <- function(text, id = NULL) {

location <- document_range(
document_position(1, 1),
document_position(Inf, 1)
)

insertText(location, text, id)
}

#' Get the Active Document Context
#'
#' Returns information about the currently active
Expand All @@ -80,5 +103,5 @@ insertText <- function(location, text, id = NULL) {
getActiveDocumentContext <- function() {
context <- callFun("getActiveDocumentContext")
context$selection <- as.document_selection(context$selection)
`class<-`(context, "document_context")
structure(context, class = "document_context")
}
12 changes: 6 additions & 6 deletions R/document-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#'
#' @export
#' @family location
makePosition <- function(row, column) {
document_position <- function(row, column) {
structure(c(row = as.numeric(row), column = as.numeric(column)),
class = "document_position")
}
Expand Down Expand Up @@ -39,7 +39,7 @@ as.document_position.default <- function(x) {
if (length(x) != 2 || !is.numeric(x))
stop("'x' should be a numeric vector of length 2", call. = FALSE)

makePosition(row = x[[1]], column = x[[2]])
document_position(row = x[[1]], column = x[[2]])
}

#' @export
Expand Down Expand Up @@ -73,9 +73,9 @@ print.document_position <- function(x, ...) {
#' @name document_range
#'
#' @export
makeRange <- function(start, end = NULL) {
document_range <- function(start, end = NULL) {

# Allow users to write e.g. 'makeRange(c(1, 2, 3, 4))';
# Allow users to write e.g. 'document_range(c(1, 2, 3, 4))';
# ie, ignore using the 'end' argument
if (is.null(end)) {

Expand Down Expand Up @@ -110,13 +110,13 @@ as.document_range.document_range <- function(x) {

#' @export
as.document_range.default <- function(x) {
makeRange(x)
document_range(x)
}

#' @export
format.document_range <- function(x, ...) {
startPos <- as.document_position(x$start)
endPos <- as.document_position(x$end)
endPos <- as.document_position(x$end)
paste(format(startPos, ...), "--", format(endPos, ...))
}

Expand Down
3 changes: 1 addition & 2 deletions man/document_position.Rd

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

3 changes: 1 addition & 2 deletions man/document_range.Rd

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

22 changes: 22 additions & 0 deletions man/setDocumentContents.Rd

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

14 changes: 7 additions & 7 deletions tests/testthat/test-document-apis.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ test_that("various APIs for interacting with an RStudio document work", {
scratchRanges <- Map(c, Map(c, 7:11, 1), Map(c, 7:11, Inf))

# Insert text at position
insertText(makePosition(7, Inf), " Hello")
insertText(document_position(7, Inf), " Hello")
insertText(list(
makePosition(8, Inf),
makePosition(9, Inf)
document_position(8, Inf),
document_position(9, Inf)
), " Hello")

insertText(list(
makePosition(10, Inf),
makePosition(11, Inf)
document_position(10, Inf),
document_position(11, Inf)
), c(" First", " Second"))

# Clean up
insertText(scratchRanges, "#")

# Insert text at range
insertText(c(7, 1, 7, Inf), "# Howdy")
insertText(makeRange(start = c(8, 1), end = c(8, Inf)), "# Hello There")
insertText(document_range(start = c(8, 1), end = c(8, Inf)), "# Hello There")

# Clean up
insertText(scratchRanges, "#")
Expand All @@ -57,7 +57,7 @@ test_that("various APIs for interacting with an RStudio document work", {
# Clean up things we appended to the document
end <- grep("^# -- Final Scratch Space -- #", context$contents)
insertText(
makeRange(start = c(end + 1, 1), end = c(Inf, 1)),
document_range(start = c(end + 1, 1), end = c(Inf, 1)),
""
)

Expand Down

0 comments on commit 148be88

Please sign in to comment.