Nothing
s4_fixture <- function(lines, snapshots = list(), point = NULL, language = "r") {
uri <- if (language == "r") "file:///s4-members.R" else "file:///s4-members.qmd"
document <- Document$new(uri, language = language, version = 1L, content = lines)
data <- parse_document(uri, lines, is_rmarkdown = document$is_rmarkdown)
data$version <- 1L
document$update_parse_data(data)
metadata <- collections::dict()
for (package in names(snapshots)) metadata$set(package, member_index_thaw(snapshots[[package]]))
workspace <- list(member_metadata = metadata, get_documentation = function(...) NULL)
if (is.null(point)) point <- list(row = length(lines) - 1L, col = nchar(tail(lines, 1L)))
list(uri = uri, document = document, workspace = workspace, point = point)
}
s4_items <- function(lines, snapshots = list(), point = NULL, language = "r") {
fixture <- s4_fixture(lines, snapshots, point, language)
member_completion(fixture$uri, fixture$workspace, fixture$document, fixture$point, TRUE, 200L)
}
s4_labels <- function(...) vapply(s4_items(...), `[[`, character(1L), "label")
test_that("S4 slots follow declarations, inheritance, factories and aliases", {
declarations <- c(
"setClass(\"Parent\", slots=c(value=\"numeric\"))",
"Widget <- methods::setClass(\"Widget\", contains=\"Parent\", slots=list(label=\"character\"))"
)
expect_identical(s4_labels(c(declarations, "x <- new(\"Widget\")", "y <- x", "y@")), c("label", "value"))
expect_identical(s4_labels(c(declarations, "Widget()@")), c("label", "value"))
expect_identical(s4_labels(c(
declarations,
"factory <- function(x) new(\"Widget\", value=x)",
"factory(stop(\"must not run\"))@"
)), c("label", "value"))
expect_identical(s4_labels(c(
"setClass(\"Legacy\", representation(value=\"numeric\", \"Parent\"))",
declarations[[1L]], "new(\"Legacy\")@"
)), "value")
expect_identical(s4_labels(c(
"factory <- function() {setClass(\"Local\", slots=c(value=\"numeric\")); new(\"Local\")}",
"factory()@"
)), "value")
expect_identical(s4_labels(c(
"setClass(\"Vector\", contains=\"numeric\", slots=c(label=\"character\"))",
"new(\"Vector\")@"
)), c(".Data", "label"))
expect_true("@" %in% unlist(CompletionOptions$triggerCharacters))
expect_identical(s4_labels(c("setClass(\"Store\", contains=\"environment\")", "new(\"Store\")@")), ".xData")
})
test_that("S4 nested slots retain declared and supplied structures", {
declarations <- c(
"setClass(\"Leaf\", slots=c(value=\"numeric\"))",
"setClass(\"Tree\", slots=c(leaf=\"Leaf\", items=\"list\"))"
)
expect_identical(s4_labels(c(declarations, "new(\"Tree\")@leaf@")), "value")
expect_identical(s4_labels(c(declarations, "new(\"Tree\", items=list(alpha=1))@items$")), "alpha")
expect_identical(s4_labels(c(declarations, "methods::slot(new(\"Tree\"), \"leaf\")@")), "value")
expect_length(s4_labels(c(declarations, "new(\"Tree\")$")), 0L)
expect_length(s4_labels("list(value=1)@"), 0L)
expect_length(s4_labels(c(declarations, "new(\"Tree\")@missing@")), 0L)
expect_identical(s4_labels(c(
"setClass(\"A\", slots=c(common=\"numeric\", a=\"character\"))",
"setClass(\"B\", slots=c(common=\"numeric\", b=\"character\"))",
"setClassUnion(\"Either\", c(\"A\", \"B\"))",
"setClass(\"Box\", slots=c(item=\"Either\"))", "new(\"Box\")@item@"
)), "common")
})
test_that("S4 slot completion recovers edits and quotes names", {
declaration <- "setClass(\"Widget\", slots=c(`a b`=\"numeric\", beta=\"character\"))"
expect_identical(s4_labels(c("bad <- )", declaration, "new(\"Widget\") @ be")), "beta")
items <- s4_items(c(declaration, "x <- new(\"Widget\")", "x@`a"))
expect_identical(items[[1L]]$textEdit$newText, "`a b`")
expect_identical(items[[1L]]$textEdit$range$start$character, 2L)
expect_identical(items[[1L]]$detail, "a b: numeric")
expect_length(s4_labels(c(declaration, "# x@")), 0L)
expect_length(s4_labels(c(declaration, "\"x@\"")), 0L)
item <- s4_items(c(declaration, "x <- new(\"Widget\")", "\"\U0001f680\"; x@beXYZ"), point = list(row = 2L, col = 9L))[[1L]]
expect_identical(item$textEdit$range$start$character, 8L)
expect_identical(item$textEdit$range$end$character, 13L)
expect_identical(s4_labels(c("```{r}", declaration, "new(\"Widget\")@", "```"),
point = list(row = 2L, col = 14L), language = "quarto"), c("a b", "beta"))
expect_length(s4_labels(c("```{r}", declaration, "```", "```{python}", "new(\"Widget\")@", "```"),
point = list(row = 4L, col = 14L), language = "quarto"), 0L)
})
test_that("S4 declarations remain conservative around shadowing and unknown types", {
declaration <- "setClass(\"Widget\", slots=c(value=\"numeric\"))"
expect_length(s4_labels(c("new(\"Widget\")@", declaration), point = list(row = 0L, col = 14L)), 0L)
expect_length(s4_labels(c(declaration, "new <- function(...) unknown()", "new(\"Widget\")@")), 0L)
expect_identical(s4_labels(c(declaration, "new <- function(...) unknown()", "methods::new(\"Widget\")@")), "value")
expect_length(s4_labels(c("setClass <- function(...) NULL", declaration, "new(\"Widget\")@")), 0L)
expect_length(s4_labels(c(declaration, "new(class_name)@")), 0L)
expect_length(s4_labels(c("setClass(\"Widget\", slots=dynamic)", "new(\"Widget\")@")), 0L)
expect_length(s4_labels(c("c <- function(...) unknown", declaration, "new(\"Widget\")@")), 0L)
expect_length(s4_labels(c("list <- function(...) unknown", "setClass(\"Widget\", slots=list(value=\"numeric\"))", "new(\"Widget\")@")), 0L)
expect_length(s4_labels(c("setClass(\"Widget\", contains=\"Unknown\")", "new(\"Widget\")@")), 0L)
expect_length(s4_labels(c("setClass(\"Widget\", contains=\"Widget\")", "new(\"Widget\")@")), 0L)
expect_length(s4_labels(c("setClass(\"Widget\", contains=\"VIRTUAL\")", "new(\"Widget\")@")), 0L)
expect_identical(s4_labels(c(declaration, "setClass(\"Widget\", slots=c(changed=\"logical\"))", "new(\"Widget\")@")), "changed")
expect_length(s4_labels(c("setClass(\"Widget\", slots=c(value=))", "new(\"Widget\")@")), 0L)
expect_length(s4_labels(c("setClass(Class=, slots=c(value=\"numeric\"))", "new(\"Widget\")@")), 0L)
})
test_that("S4 hover and signatures use known function slot syntax without execution", {
marker <- withr::local_tempfile()
declarations <- c(
"setClass(\"Widget\", slots=c(value=\"numeric\", run=\"function\"),",
"prototype=list(run=function(value, flag=TRUE) value), validity=function(object) stop(\"validity\"))",
"setMethod(\"initialize\", \"Widget\", function(.Object, ...) stop(\"initialize\"))",
"x <- new(\"Widget\")"
)
fixture <- s4_fixture(c(declarations, "x@run(flag = "))
signature <- signature_reply(1L, fixture$uri, fixture$workspace, fixture$document, fixture$point)$result
expect_identical(signature$signatures[[1L]]$label, "run(value, flag = TRUE)")
expect_identical(signature$activeParameter, 1L)
hover <- hover_reply(1L, fixture$uri, fixture$workspace, fixture$document, list(row = 4L, col = 4L))$result
expect_identical(hover$contents, "```r\nrun(value, flag = TRUE)\n```")
fixture <- s4_fixture(c(declarations, "x@value"))
hover <- hover_reply(1L, fixture$uri, fixture$workspace, fixture$document, fixture$point)$result
expect_identical(hover$contents, "```r\ndouble\n```")
fixture <- s4_fixture(c(declarations, sprintf(
"new(\"Widget\", run=function(first, second=2) NULL)@run({writeLines(\"ran\", %s); stop(\"argument\")}, ",
encodeString(marker, quote = "\"")
)))
signature <- signature_reply(1L, fixture$uri, fixture$workspace, fixture$document, fixture$point)$result
expect_identical(signature$signatures[[1L]]$label, "run(first, second = 2)")
expect_false(file.exists(marker))
fixture <- s4_fixture(c("setClass(\"Opaque\", slots=c(run=\"function\"))", "new(\"Opaque\")@run("))
expect_length(signature_reply(1L, fixture$uri, fixture$workspace, fixture$document, fixture$point)$result$signatures, 0L)
})
test_that("Installed S4 metadata supplies current slots without constructing objects", {
skip_if_not_installed("stats4")
snapshot <- member_prepare_package("stats4")
expected <- sort(names(methods::getSlots(methods::getClassDef("mle", where = asNamespace("stats4")))))
expect_identical(s4_labels(c("library(stats4)", "new(\"mle\")@"), list(stats4 = snapshot)), expected)
expect_identical(s4_labels("stats4::mle(function(x) x^2)@", list(stats4 = snapshot)), expected)
expect_length(s4_labels("new(\"mle\")@", list(stats4 = snapshot)), 0L)
expect_identical(member_index_freeze(member_index_thaw(snapshot))$s4_classes, snapshot$s4_classes)
})
test_that("S4 namespace constructors and objects use inert descriptor metadata", {
scope <- new.env(parent = asNamespace("methods"))
name <- paste0("S4Fixture", Sys.getpid())
scope$Widget <- methods::setClass(name, slots = c(value = "numeric"), where = scope, package = "s4fixture")
withr::defer(suppressWarnings(methods::removeClass(name, where = scope)))
scope$object <- scope$Widget()
store <- paste0(name, "Store")
Store <- methods::setClass(store, contains = "environment", where = scope, package = "languageserver")
withr::defer(suppressWarnings(methods::removeClass(store, where = scope)))
scope$store <- Store()
marker <- withr::local_tempfile()
makeActiveBinding("unsafe", function() {
writeLines("ran", marker)
stop("getter")
}, scope)
input <- member_namespace_input(scope, package = "s4fixture", exports = c("Widget", "object"))
snapshot <- member_index_freeze(member_package_index(input))
expect_identical(s4_labels(c("library(s4fixture)", "Widget()@"), list(s4fixture = snapshot)), "value")
expect_identical(s4_labels("s4fixture::Widget()@", list(s4fixture = snapshot)), "value")
expect_identical(s4_labels(c("library(s4fixture)", "object@"), list(s4fixture = snapshot)), "value")
store_input <- member_namespace_input(scope, package = "languageserver", exports = "store")
store_snapshot <- member_index_freeze(member_package_index(store_input))
expect_identical(s4_labels("languageserver::store@", list(languageserver = store_snapshot)), ".xData")
expect_false(file.exists(marker))
changed <- snapshot
changed$s4_classes[[name]]$slots <- list(changed = "character")
expect_identical(s4_labels("s4fixture::Widget()@", list(s4fixture = changed)), "changed")
})
test_that("Static S4 namespace slots preserve stored objects and callable syntax", {
scope <- new.env(parent = asNamespace("methods"))
leaf <- paste0("StaticS4Leaf", Sys.getpid())
box <- paste0("StaticS4Box", Sys.getpid())
scope$Leaf <- methods::setClass(leaf, slots = c(value = "numeric"),
where = scope, package = "languageserver")
withr::defer(suppressWarnings(methods::removeClass(leaf, where = scope)))
scope$Box <- methods::setClass(box, slots = c(typed = leaf, flexible = "ANY", items = "list", run = "function"),
where = scope, package = "languageserver")
withr::defer(suppressWarnings(methods::removeClass(box, where = scope)))
marker <- withr::local_tempfile()
scope$object <- scope$Box(
typed = scope$Leaf(value = 1), flexible = scope$Leaf(value = 2),
items = list(child = scope$Leaf(value = 3)),
run = function(first, second = 2) {
writeLines("method", marker)
list(done = first)
}
)
scope$private <- scope$object
store <- paste0(box, "Store")
Store <- methods::setClass(store, contains = "environment", where = scope, package = "languageserver")
withr::defer(suppressWarnings(methods::removeClass(store, where = scope)))
scope$store <- Store()
scope$store@.xData$payload <- list(flag = TRUE)
snapshot <- member_index_freeze(member_package_index(member_namespace_input(
scope, package = "languageserver", exports = c("object", "store")
)))
snapshots <- list(languageserver = snapshot)
for (code in list("languageserver::object@", c("library(languageserver)", "object@"),
c("original <- languageserver::object", "copy <- original", "copy@"))) {
expect_identical(s4_labels(code, snapshots), c("flexible", "items", "run", "typed"))
}
expect_identical(s4_labels("languageserver::object@typed@", snapshots), "value")
expect_identical(s4_labels("languageserver::object@flexible@", snapshots), "value")
expect_identical(s4_labels("languageserver::object@items$", snapshots), "child")
expect_identical(s4_labels("languageserver::object@items$child@", snapshots), "value")
expect_identical(s4_labels("languageserver:::private@flexible@", snapshots), "value")
expect_identical(s4_labels(c("copy <- languageserver:::private", "copy@"), snapshots),
c("flexible", "items", "run", "typed"))
expect_length(s4_labels("languageserver::private@", snapshots), 0L)
expect_identical(s4_labels("languageserver::store@", snapshots), ".xData")
expect_identical(s4_labels("languageserver::store@.xData$", snapshots), "payload")
expect_identical(s4_labels("languageserver::store@.xData$payload$", snapshots), "flag")
item <- s4_items("languageserver::object@ru", snapshots)[[1L]]
expect_identical(item$detail, "run(first, second = 2)")
expect_identical(item$kind, CompletionItemKind$Method)
fixture <- s4_fixture("languageserver::object@run(second = ", snapshots)
signature <- signature_reply(1L, fixture$uri, fixture$workspace, fixture$document, fixture$point)$result
expect_identical(signature$signatures[[1L]]$label, "run(first, second = 2)")
expect_identical(s4_labels("languageserver::object@run(stop(\"argument\"))$", snapshots), "done")
expect_length(s4_labels(c("object <- unknown()", "object@"), snapshots), 0L)
expect_false(file.exists(marker))
expect_identical(unserialize(serialize(snapshot, NULL)), snapshot)
})
test_that("Static callable S4 objects retain slots and their stored data part", {
scope <- new.env(parent = asNamespace("methods"))
name <- paste0("StaticS4Function", Sys.getpid())
Class <- methods::setClass(name, contains = "function", slots = c(label = "character"),
where = scope, package = "languageserver")
withr::defer(suppressWarnings(methods::removeClass(name, where = scope)))
list_name <- paste0(name, "List")
List <- methods::setClass(list_name, contains = "list", where = scope, package = "languageserver")
withr::defer(suppressWarnings(methods::removeClass(list_name, where = scope)))
marker <- withr::local_tempfile()
scope$object <- Class(.Data = function(value = 1L) {
writeLines("method", marker)
list(done = value)
}, label = "callable")
scope$container <- List(.Data = list(list(child = TRUE)))
fn <- function(value = 1L) list(done = value)
environment(fn) <- baseenv()
scope$bundle <- list(callable = Class(.Data = fn, label = "bundled"))
snapshot <- member_index_freeze(member_package_index(member_namespace_input(
scope, package = "languageserver", exports = c("object", "container", "bundle")
)))
snapshots <- list(languageserver = snapshot)
expect_identical(s4_labels("languageserver::object@", snapshots), c(".Data", "label"))
expect_identical(s4_labels(c("library(languageserver)", "object@"), snapshots), c(".Data", "label"))
expect_identical(s4_labels("languageserver::object@.Data()$", snapshots), "done")
expect_identical(s4_labels("languageserver::object()$", snapshots), "done")
expect_identical(s4_labels("languageserver::bundle$callable@", snapshots), c(".Data", "label"))
expect_identical(s4_labels("languageserver::bundle$callable()$", snapshots), "done")
expect_identical(s4_labels("languageserver::container@.Data[[1L]]$", snapshots), "child")
item <- s4_items("languageserver::object@.D", snapshots)[[1L]]
expect_identical(item$detail, ".Data(value = 1L)")
expect_false(file.exists(marker))
})
test_that("Static S4 snapshots bound stored slots and skip deferred class metadata", {
scope <- new.env(parent = asNamespace("methods"))
class <- paste0("StaticS4Deferred", Sys.getpid())
Class <- methods::setClass(class, slots = c(value = "numeric"), where = scope, package = "languageserver")
withr::defer(suppressWarnings(methods::removeClass(class, where = scope)))
object <- Class(value = 1)
marker <- withr::local_tempfile()
lookup <- new.env(parent = emptyenv())
delayedAssign(paste0(".__C__", class), {
writeLines("descriptor", marker)
stop("descriptor")
}, assign.env = lookup)
describe <- function(...) stop("stored slot visitor")
snapshot <- member_s4_instance_snapshot(object, describe, 0L, 4L, lookup)
expect_identical(snapshot$reason, "unknown_s4_class")
expect_false(file.exists(marker))
snapshot <- member_s4_instance_snapshot(object, describe, 5L, 4L, scope)
expect_identical(snapshot$reason, "snapshot_depth")
snapshot <- member_s4_instance_snapshot(object, describe, 0L, 4L, scope, reserve = function(n) FALSE)
expect_identical(snapshot$reason, "snapshot_budget")
})
test_that("S4 dependency slots and union branches preserve class package identities", {
skip_if_not_installed("stats4")
loadNamespace("stats4")
classes <- list(Box = list(name = "Box", slots = list(fit = structure("mle", package = "stats4"))))
dependencies <- member_s4_dependencies(classes)
expect_true("mle" %in% names(dependencies$stats4))
input <- list(package = "s4fixture", definitions = list(), exports = "object", s4_classes = classes,
s4_dependencies = dependencies, s4_objects = list(object = list(name = "Box", package = "s4fixture")))
snapshot <- member_index_freeze(member_package_index(input))
expect_true("coef" %in% s4_labels("s4fixture::object@fit@", list(s4fixture = snapshot)))
fixture <- s4_fixture("s4fixture::object@fit@coef", list(s4fixture = snapshot))
expect_identical(hover_reply(1L, fixture$uri, fixture$workspace, fixture$document, fixture$point)$result$contents, "```r\ndouble\n```")
index <- member_generic_index("")
index$s4_dependencies <- list(
a = list(Leaf = list(slots = list(common = "numeric", a = "character"))),
b = list(Leaf = list(slots = list(common = "numeric", b = "character")))
)
a <- member_value(type = "Box", slot_types = list(item = "Leaf"), s4_class = list(name = "Box", package = "a"))
b <- member_value(type = "Box", slot_types = list(item = "Leaf"), s4_class = list(name = "Box", package = "b"))
nested <- member_s4_slot(member_join(a, b), "item", index, list())
expect_identical(names(nested$slot_types), "common")
})
test_that("S4 source packages, local slot writes and prototype cycles stay static", {
code <- c("Widget <- methods::setClass(\"Widget\", slots=c(value=\"numeric\"))")
input <- list(package = "s4fixture", exports = "Widget", definitions = list(Widget = parse(text = code)[[1L]][[3L]]),
expressions = list(parse(text = code)))
snapshot <- member_index_freeze(member_package_index(input))
expect_identical(s4_labels("s4fixture::Widget()@", list(s4fixture = snapshot)), "value")
fixture <- s4_fixture(c(
"setClass(\"Widget\", slots=c(run=\"function\"))",
"factory <- function() {x <- new(\"Widget\"); x@run <- function(first, second=2) NULL; x}",
"factory()@run("
))
expect_identical(signature_reply(1L, fixture$uri, fixture$workspace, fixture$document, fixture$point)$result$signatures[[1L]]$label,
"run(first, second = 2)")
expect_identical(s4_labels(c(
"setClass(\"Loop\", slots=c(item=\"Loop\"), prototype=list(item=new(\"Loop\")@item))",
"new(\"Loop\")@item@"
)), "item")
index <- member_generic_index("")
index$s4_classes <- list(Widget = list(slots = list(value = "numeric")))
budget <- new.env(parent = emptyenv())
budget$remaining <- 0L
expect_identical(member_s4_shape("Widget", index, list(), budget = budget)$reason, "budget")
expect_true(budget$exhausted)
})
test_that("S4 providers wait for the first request after an edit over LSP", {
skip_on_cran()
client <- language_client()
path <- withr::local_tempfile(fileext = ".R")
uri <- path_to_uri(path)
lines <- c(
"setClass(\"Widget\", slots=c(value=\"numeric\", run=\"function\"), prototype=list(run=function(value, flag=TRUE) value))",
"x <- new(\"Widget\")", "x"
)
did_open(client, path, text = paste(lines, collapse = "\n"))
notify(client, "workspace/didChangeConfiguration", list(settings = list(parse_delay = 0.5)))
edit <- function(text, version) {
lines[[3L]] <<- text
notify(client, "textDocument/didChange", list(
textDocument = list(uri = uri, version = version),
contentChanges = list(list(text = paste(lines, collapse = "\n")))
))
}
edit("x@", 2L)
result <- respond_completion(client, path, c(2L, 2L), retry = FALSE)
expect_setequal(vapply(result$items, `[[`, character(1L), "label"), c("run", "value"))
edit("x@run(flag = ", 3L)
result <- respond_signature(client, path, c(2L, nchar(lines[[3L]])), retry = FALSE)
expect_identical(result$signatures[[1L]]$label, "run(value, flag = TRUE)")
edit("x@value", 4L)
result <- respond_hover(client, path, c(2L, 4L), retry = FALSE)
expect_identical(result$contents[[1L]], "```r\ndouble\n```")
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.