tests/testthat/test-member-providers.R

member_provider_fixture <- function(lines, snapshots = list(), language = "r") {
    uri <- if (language == "r") "file:///member-provider.R" else "file:///member-provider.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,
        get_signature = function(...) stop("unrelated bare function lookup"),
        get_help = function(...) stop("unrelated bare help lookup")
    )
    list(uri = uri, document = document, workspace = workspace)
}

member_provider_signature <- function(
  fixture, row = fixture$document$nline - 1L,
  col = nchar(fixture$document$line0(row))
) {
    signature_reply(
        1L, fixture$uri, fixture$workspace, fixture$document,
        list(row = row, col = col)
    )$result
}

member_provider_hover <- function(fixture, row, col) {
    hover_reply(
        1L, fixture$uri, fixture$workspace, fixture$document,
        list(row = row, col = col)
    )$result
}

test_that("Multiline dollar chains share completion, signatures and hover", {
    code <- c(
        "make_pipeline <- function(source) {",
        "self <- new.env(parent = emptyenv())",
        "self$source <- source",
        "self$filter <- function(predicate) self",
        "self$limit <- function(n = 10L) self",
        "self$collect <- function(format = \"list\") {",
        "list(data = source, format = format, metadata = list(cached = FALSE))",
        "}", "self", "}", "pipeline <- make_pipeline(iris)",
        "pipeline$", "  filter(\"Sepal.Length > 5\")$", "  limit(n = 5)$",
        "  filter(\"Petal.Length < 2\")$", "  collect()"
    )
    fixture <- member_provider_fixture(code)
    for (row in 11:15) {
        col <- if (row == 11L) 9L else 4L
        items <- member_completion(fixture$uri, fixture$workspace, fixture$document, list(row = row, col = col), TRUE, 200L)
        labels <- vapply(items, `[[`, character(1L), "label")
        expected <- if (row == 11L) c("collect", "filter", "limit", "source") else if (row == 13L) "limit" else if (row == 15L) "collect" else "filter"
        expect_identical(labels, expected)
        if (row > 11L) {
            expect_identical(items[[1L]]$textEdit$range$start$line, row)
            expect_identical(items[[1L]]$textEdit$range$start$character, 2L)
            expect_false(grepl("[(]", items[[1L]]$textEdit$newText))
        }
    }
    for (row in 12:15) {
        col <- regexpr("[(]", code[[row + 1L]])[[1L]]
        expected <- if (row == 13L) "limit(n = 10L)" else if (row == 15L) "collect(format = \"list\")" else "filter(predicate)"
        expect_identical(member_provider_signature(fixture, row, col)$signatures[[1L]]$label, expected)
        expect_identical(member_provider_hover(fixture, row, 4L)$contents, sprintf("```r\n%s\n```", expected))
    }
    fixture <- member_provider_fixture(c(code[1:11], "pipeline$ # continue", " # comment", "", "  collect(format = "))
    expect_identical(member_provider_signature(fixture)$signatures[[1L]]$label, "collect(format = \"list\")")
    expect_identical(member_provider_hover(fixture, 14L, 4L)$range$start$character, 2L)
    fixture <- member_provider_fixture(c(
        code[1:11],
        "pipeline$collect # trailing comment before (",
        "  # full-line comment before (",
        "  (",
        "    format # comment before =",
        "      = \"list\"",
        "  )"
    ))
    expect_identical(
        member_provider_signature(fixture, 14L, 4L)$signatures[[1L]]$label,
        "collect(format = \"list\")"
    )
    arg_loc <- member_argument_location(fixture$document, list(row = 14L, col = 6L))
    expect_identical(arg_loc$parameter, "format")
    expect_null(member_argument_location(fixture$document, list(row = 14L, col = 0L)))
    expect_null(member_argument_location(fixture$document, list(row = 14L, col = 22L)))
    for (chain in list(
        c(
            "pipeline$filter(\"multiline",
            "string\")$collect",
            "  (format = \"list\")"
        ),
        c(
            "(pipeline$filter(\"first line",
            "# literal\")$collect",
            "  (format = \"list\"))"
        ),
        c(
            "(pipeline$filter(r\"(a\"b)\")$collect # comment",
            "  (format = \"list\"))"
        ),
        c(
            "(pipeline$filter(r\"(a) \" # literal)\")$collect",
            "  (format = \"list\"))"
        ),
        c(
            "(pipeline$filter(r'-(a)- ' # literal)-')$collect # comment",
            "  (format = \"list\"))"
        )
    )) {
        fixture_chain <- member_provider_fixture(c(code[1:11], chain))
        call_row <- fixture_chain$document$nline - 1L
        callee_row <- call_row - 1L
        callee_col <- regexpr("collect", fixture_chain$document$line0(callee_row), fixed = TRUE)[[1L]]
        expect_identical(
            member_call_location(fixture_chain$document, list(row = call_row, col = 4L))$cursor$token,
            "collect"
        )
        expect_identical(
            member_provider_signature(fixture_chain, call_row, 4L)$signatures[[1L]]$label,
            "collect(format = \"list\")"
        )
        expect_identical(
            member_provider_hover(fixture_chain, callee_row, callee_col)$contents,
            "```r\ncollect(format = \"list\")\n```"
        )
        expect_identical(
            member_argument_location(fixture_chain$document, list(row = call_row, col = 5L))$parameter,
            "format"
        )
    }
    expect_identical(
        member_uncomment_line("pipeline$filter(r\"(a) \" # literal)\")$collect # comment"),
        "pipeline$filter(r\"(a) \" # literal)\")$collect "
    )
    fixture_same_line <- member_provider_fixture(c(
        code[1:11],
        "(pipeline$filter(r\"(a) \" # literal)\")$collect(format = \"list\"))"
    ))
    arg_col <- regexpr("format", fixture_same_line$document$line0(11L), fixed = TRUE)[[1L]]
    expect_identical(
        member_argument_location(fixture_same_line$document, list(row = 11L, col = arg_col))$parameter,
        "format"
    )
    fixture_split_operator <- member_provider_fixture(c(
        code[1:11],
        "(pipeline$filter(\"first line",
        "# literal\")$ # comment",
        "  collect(format = \"list\"))"
    ))
    items <- member_completion(
        fixture_split_operator$uri, fixture_split_operator$workspace,
        fixture_split_operator$document, list(row = 13L, col = 5L), TRUE, 200L
    )
    expect_identical(vapply(items, `[[`, character(1L), "label"), "collect")
    expect_identical(
        member_provider_signature(fixture_split_operator, 13L, 12L)$signatures[[1L]]$label,
        "collect(format = \"list\")"
    )
    expect_identical(
        member_provider_hover(fixture_split_operator, 13L, 4L)$contents,
        "```r\ncollect(format = \"list\")\n```"
    )
    for (empty_fixture in list(
        list(fixture = member_provider_fixture("x <- 1"), point = list(row = 1L, col = 0L)),
        list(fixture = member_provider_fixture(character()), point = list(row = 0L, col = 0L))
    )) {
        empty_fixture$fixture$workspace$get_parse_data <- function(...) NULL
        empty_fixture$fixture$workspace$get_help <- function(...) NULL
        empty_fixture$fixture$workspace$get_definition <- function(...) NULL
        empty_fixture$fixture$workspace$guess_namespace <- function(...) NULL
        empty_fixture$fixture$workspace$get_signature <- function(...) NULL
        expect_null(
            hover_reply(
                1L, empty_fixture$fixture$uri, empty_fixture$fixture$workspace,
                empty_fixture$fixture$document, empty_fixture$point
            )$result
        )
        expect_null(member_hover_location(empty_fixture$fixture$document, empty_fixture$point))
        expect_null(member_argument_location(empty_fixture$fixture$document, empty_fixture$point))
        expect_null(member_cursor(empty_fixture$fixture$document, empty_fixture$point))
        expect_identical(document_uncomment_lines(empty_fixture$fixture$document, empty_fixture$point$row), character())
        expect_null(document_call_callee(empty_fixture$fixture$document, empty_fixture$point))
    }
    quarto_multiline <- member_provider_fixture(c(
        "---",
        "title: \"Test\"",
        "---",
        "She said \"hello.",
        "```{r}",
        code[1:11],
        "(pipeline$filter(\"# literal\")$collect",
        "  (format = \"list\"))",
        "```"
    ), language = "quarto")
    expect_identical(
        member_call_location(quarto_multiline$document, list(row = 17L, col = 4L))$cursor$token,
        "collect"
    )
    expect_identical(
        member_provider_signature(quarto_multiline, 17L, 4L)$signatures[[1L]]$label,
        "collect(format = \"list\")"
    )
    expect_identical(
        member_provider_hover(quarto_multiline, 16L, 33L)$contents,
        "```r\ncollect(format = \"list\")\n```"
    )
    expect_identical(
        member_argument_location(quarto_multiline$document, list(row = 17L, col = 5L))$parameter,
        "format"
    )
    expect_null(document_r_bounds(quarto_multiline$document, 3L))
    expect_null(member_call_location(
        quarto_multiline$document,
        list(row = 17L, col = 4L),
        call = list(opening = list(row = 3L, col = 0L))
    ))
    expect_null(document_call_callee(quarto_multiline$document, list(row = 5L, col = 0L)))
    expect_null(member_call_location(
        member_provider_fixture(c("# comment", "(1)"))$document,
        list(row = 1L, col = 1L)
    ))
    quarto_same_line <- member_provider_fixture(c(
        "She said \"hello.",
        "```{r}",
        code[1:11],
        "(pipeline$filter(\"# literal\")$collect(format = \"list\"))",
        "```"
    ), language = "quarto")
    quarto_arg_col <- regexpr("format", quarto_same_line$document$line0(13L), fixed = TRUE)[[1L]]
    expect_identical(
        member_argument_location(quarto_same_line$document, list(row = 13L, col = quarto_arg_col))$parameter,
        "format"
    )
    quarto_split <- member_provider_fixture(c(
        "She said \"hello.",
        "```{r}",
        code[1:11],
        "(pipeline$filter(\"# literal\")$ # comment",
        "  collect(format = \"list\"))",
        "```"
    ), language = "quarto")
    quarto_items <- member_completion(
        quarto_split$uri, quarto_split$workspace, quarto_split$document,
        list(row = 14L, col = 5L), TRUE, 200L
    )
    expect_identical(vapply(quarto_items, `[[`, character(1L), "label"), "collect")
    expect_identical(
        member_provider_signature(quarto_split, 14L, 12L)$signatures[[1L]]$label,
        "collect(format = \"list\")"
    )
    expect_identical(
        member_provider_hover(quarto_split, 14L, 4L)$contents,
        "```r\ncollect(format = \"list\")\n```"
    )
    fixture_special <- member_provider_fixture(c(
        "`%#%` <- function(lhs, rhs) lhs",
        "`%\"%` <- function(lhs, rhs) rhs",
        "Widget <- R6::R6Class(\"Widget\", public = list(",
        "  run = function(text = 1 %#% 2, value = 1) self",
        "))",
        "x <- Widget$new()",
        "x$run(text = 1 %#% 2, value = 3)",
        "x$run(text = 1 %\"% 2, value = 3)"
    ))
    for (special_row in c(6L, 7L)) {
        special_col <- regexpr("value", fixture_special$document$line0(special_row), fixed = TRUE)[[1L]]
        special_hover <- member_provider_hover(fixture_special, special_row, special_col)
        expect_identical(
            special_hover$contents[[1L]],
            "```r\nrun(text = 1 %#% 2, value = 1)\n```"
        )
        expect_identical(
            special_hover$contents[[2L]],
            "Public method of R6 class `Widget`"
        )
        special_sig <- member_provider_signature(fixture_special, special_row, special_col + 8L)
        expect_identical(special_sig$signatures[[1L]]$label, "run(text = 1 %#% 2, value = 1)")
        expect_equal(special_sig$activeParameter, 1L)
    }
    fixture_symbol <- member_provider_fixture(c(
        "MyClass <- function(value = 1) value",
        "MyClass# comment without space",
        "  (value = 2)"
    ))
    expect_identical(
        member_call_location(fixture_symbol$document, list(row = 2L, col = 4L), symbols = TRUE)$cursor$token,
        "MyClass"
    )
    fixture <- member_provider_fixture(c("x <- list(`a b`=function(value=1) NULL)", "x$", "  `a b`("))
    expect_identical(member_provider_signature(fixture)$signatures[[1L]]$label, "`a b`(value = 1)")
    expect_identical(member_provider_hover(fixture, 2L, 4L)$contents, "```r\n`a b`(value = 1)\n```")
    for (lines in list(c("x <- list(run=function() NULL)", "\"x$\"", "run("),
            c("x <- list(run=function() NULL)", "# x$", "run("))) {
        fixture <- member_provider_fixture(lines)
        expect_null(member_hover_location(fixture$document, list(row = 2L, col = 2L)))
        expect_null(member_symbol(fixture$uri, fixture$workspace, fixture$document,
                member_call_location(fixture$document, list(row = 2L, col = 4L)))$signature)
    }
})

test_that("R6 self, super and private refer to their declared method contexts", {
    parent <- paste0("Parent <- R6::R6Class(\"Parent\", public=list(run=function(value=1) self, base=42),",
        "private=list(inherited=function(key=2) self), active=list(parent_active=function() stop(\"getter\")))")
    code <- c(parent, "Child <- R6::R6Class(\"Child\", inherit=Parent,", "public=list(",
        "test=function() {", "  self$child(option = TRUE)", "  super$run(value = 2)", "},",
        "child=function(option=FALSE) self),", "private=list(secret=function(key=1) NULL),",
        "active=list(current=function() self$child()))")
    fixture <- member_provider_fixture(code)
    for (case in list(list(row = 4L, col = 7L, expected = c("base", "child", "current", "parent_active", "run", "test")),
            list(row = 5L, col = 8L, expected = c("inherited", "parent_active", "run")))) {
        items <- member_completion(fixture$uri, fixture$workspace, fixture$document, case[c("row", "col")], TRUE, 200L)
        expect_identical(vapply(items, `[[`, character(1L), "label"), case$expected)
    }
    expect_identical(member_provider_hover(fixture, 4L, 10L)$contents[[1L]], "```r\nchild(option = FALSE)\n```")
    expect_identical(member_provider_hover(fixture, 5L, 10L)$contents[[1L]], "```r\nrun(value = 1)\n```")
    expect_identical(member_provider_signature(fixture, 4L, 14L)$signatures[[1L]]$label, "child(option = FALSE)")
    expect_identical(member_provider_hover(fixture, 9L, 37L)$contents[[1L]], "```r\nchild(option = FALSE)\n```")

    fixture <- member_provider_fixture(c(parent, "Child <- R6::R6Class(\"Child\", inherit=Parent,",
            "private=list(test=function() private$secret(), secret=function(key=1) NULL))"))
    expect_identical(member_provider_hover(fixture, 2L, 42L)$contents[[1L]], "```r\nsecret(key = 1)\n```")
    fixture <- member_provider_fixture(c(parent, "Child <- R6::R6Class(\"Child\", inherit=Parent,",
            "public=list(test=function(self) self$run()))"))
    expect_null(member_provider_hover(fixture, 2L, 38L))
    fixture <- member_provider_fixture(c("f <- function() self$run()"))
    expect_null(member_provider_hover(fixture, 0L, 23L))
    # A trailing parse error cannot remove the class context before it.
    fixture <- member_provider_fixture(c(code, "broken <- )"))
    expect_identical(member_provider_hover(fixture, 4L, 10L)$contents[[1L]], "```r\nchild(option = FALSE)\n```")
    fixture <- member_provider_fixture(c(parent, "Child <- R6::R6Class(\"Child\", inherit=Parent,",
            "public=list(test=function() super$run()$child(), child=function(option=FALSE) self))"))
    expect_identical(member_provider_hover(fixture, 2L, 42L)$contents[[1L]], "```r\nchild(option = FALSE)\n```")
    fixture <- member_provider_fixture(c(parent, "Child <- R6::R6Class(\"Child\", inherit=Parent,",
            "public=list(test=function() {self <- list(local=1); self$local}))"))
    expect_identical(member_provider_hover(fixture, 2L, 59L)$contents, "```r\n1\n```")
    fixture <- member_provider_fixture(c("```{r}", code, "```", "```{python}", "self$child()", "```"), language = "quarto")
    expect_identical(member_provider_hover(fixture, 5L, 10L)$contents[[1L]], "```r\nchild(option = FALSE)\n```")
    expect_null(member_call_location(fixture$document, list(row = 13L, col = 11L)))
})

test_that("Multiline members and R6 method context work on the first LSP request after edits", {
    skip_on_cran()
    client <- language_client()
    path <- withr::local_tempfile(fileext = ".R")
    uri <- path_to_uri(path)
    declarations <- c("x <- list(run=function(value=1) NULL)")
    did_open(client, path, text = declarations)
    notify(client, "workspace/didChangeConfiguration", list(settings = list(parse_delay = 0.5)))
    edit <- function(lines, version) {
        notify(client, "textDocument/didChange", list(textDocument = list(uri = uri, version = version),
                contentChanges = list(list(text = paste(lines, collapse = "\n")))))
    }
    edit(c(declarations, "x$", "  ru"), 2L)
    result <- respond_completion(client, path, c(2L, 4L), retry = FALSE)
    expect_identical(vapply(result$items, `[[`, character(1L), "label"), "run")
    edit(c(declarations, "x$", "  run(value = "), 3L)
    expect_identical(respond_signature(client, path, c(2L, 14L), retry = FALSE)$signatures[[1L]]$label, "run(value = 1)")
    edit(c(declarations, "x$", "  run()"), 4L)
    expect_identical(respond_hover(client, path, c(2L, 4L), retry = FALSE)$contents[[1L]], "```r\nrun(value = 1)\n```")
    code <- c("Parent <- R6::R6Class(\"Parent\", public=list(run=function(value=1) self))",
        "Child <- R6::R6Class(\"Child\", inherit=Parent, public=list(",
        "test=function() {self$},", "child=function(flag=TRUE) self))")
    edit(code, 5L)
    result <- respond_completion(client, path, c(2L, 22L), retry = FALSE)
    expect_setequal(vapply(result$items, `[[`, character(1L), "label"), c("run", "test", "child"))
    code[[3L]] <- "test=function() super$run(),"
    edit(code, 6L)
    expect_identical(respond_hover(client, path, c(2L, 24L), retry = FALSE)$contents[[1L]], "```r\nrun(value = 1)\n```")
})

test_that("Member signatures follow fluent source closures without calling arguments", {
    marker <- withr::local_tempfile()
    fixture <- member_provider_fixture(c(
        "factory <- function(x) list(step=function() list(finish=function(value, flag=TRUE) x))",
        sprintf(
            "factory({writeLines(\"ran\", %s); stop(\"input\")})$step()$finish(flag = ",
            encodeString(marker, quote = "\"")
        )
    ))
    result <- member_provider_signature(fixture)
    expect_identical(result$signatures[[1L]]$label, "finish(value, flag = TRUE)")
    expect_identical(result$activeParameter, 1L)
    expect_length(result$signatures[[1L]]$parameters, 2L)
    expect_false(file.exists(marker))

    fixture <- member_provider_fixture(c(
        "x <- list(run=function(first, ..., option=TRUE) NULL)", "x$run(",
        " first=1,", " option = "
    ))
    expect_identical(member_provider_signature(fixture)$activeParameter, 2L)
    fixture <- member_provider_fixture(c(
        "f <- function() {x <- list(run=function(local=1) NULL); x$run(", "}"
    ))
    expect_identical(member_provider_signature(fixture, 0L)$signatures[[1L]]$label, "run(local = 1)")
})

test_that("Member hover preserves quoted names, fields and UTF-16 ranges", {
    fixture <- member_provider_fixture(c(
        "x <- list(`a b`=function(value=1) NULL, answer=42)",
        "\"\U0001f680\"; x$`a b`(value=1)", "x$answer"
    ))
    for (col in 7:12) {
        result <- member_provider_hover(fixture, 1L, col)
        expect_identical(result$contents, "```r\n`a b`(value = 1)\n```")
        expect_identical(result$range$start$character, 8L)
        expect_identical(result$range$end$character, 13L)
    }
    expect_identical(
        member_provider_signature(fixture, 1L, 20L)$signatures[[1L]]$label,
        "`a b`(value = 1)"
    )
    expect_identical(member_provider_hover(fixture, 2L, 4L)$contents, "```r\n42\n```")
})

test_that("R6 members use public inherited declarations without initialization or getters", {
    fixture <- member_provider_fixture(c(
        "Parent <- R6::R6Class(\"Parent\", public=list(run=function(value, option=FALSE) self))",
        "Child <- R6::R6Class(\"Child\", inherit=Parent, public=list(initialize=function() stop(\"initialize\")),",
        "active=list(danger=function() stop(\"getter\")))", "Child$new()$run(option = "
    ))
    expect_identical(
        member_provider_signature(fixture)$signatures[[1L]]$label,
        "run(value, option = FALSE)"
    )
    expect_identical(
        member_provider_hover(fixture, 3L, 13L)$contents[[1L]],
        "```r\nrun(value, option = FALSE)\n```"
    )
    fixture$document$set_content(2L, c(fixture$document$content[1:3], "Child$new()$danger"))
    data <- parse_document(fixture$uri, fixture$document$content)
    data$version <- 2L
    fixture$document$update_parse_data(data)
    expect_identical(member_provider_hover(fixture, 3L, 14L)$contents, "Active binding of R6 class `Child`")
})

test_that("Copied R6 command methods share completion, signatures and hover", {
    lines <- c(
        "commands <- function(send) list(GET=function(key) send(key), SET=function(key, value, option=NULL) send(key, value))",
        "Client <- R6::R6Class(\"Client\", lock_objects=FALSE, public=list(initialize=function(send) {",
        "methods <- commands(send); for (name in names(methods)) {self[[name]] <- methods[[name]]}",
        "}))", "client <- Client$new(unknown)", "client$SET(option = "
    )
    fixture <- member_provider_fixture(lines)
    result <- member_provider_signature(fixture)
    expect_identical(result$signatures[[1L]]$label, "SET(key, value, option = NULL)")
    expect_identical(result$activeParameter, 2L)
    expect_identical(member_provider_hover(fixture, 5L, 8L)$contents[[1L]],
        "```r\nSET(key, value, option = NULL)\n```")
})

test_that("R6 initializer early returns preserve public method providers", {
    for (result in c("private", "invisible(NULL)", "list(secret=1)")) {
        fixture <- member_provider_fixture(c(
            sprintf('Client <- R6::R6Class("Client", public=list(run=NULL, initialize=function() {self$run <- function(value, flag=TRUE) NULL; return(%s)}), private=list(secret=1))', result),
            "client <- Client$new()", "client$run(flag = "
        ))
        signature <- member_provider_signature(fixture)
        expect_identical(signature$signatures[[1L]]$label, "run(value, flag = TRUE)")
        expect_identical(signature$activeParameter, 1L)
        expect_identical(member_provider_hover(fixture, 2L, 8L)$contents[[1L]],
            "```r\nrun(value, flag = TRUE)\n```")
    }
})

test_that("R6 assignment attempts retain original method signatures and opaque active bindings", {
    for (body in c("self$run <- function(replacement=2) NULL; self$x <- function(fake=1) NULL",
            "methods <- list(run=function(replacement=2) NULL, x=function(fake=1) NULL); for (name in names(methods)) self[[name]] <- methods[[name]]")) {
        declaration <- sprintf('Client <- R6::R6Class("Client", lock_objects=FALSE, public=list(run=function(original=1) NULL, initialize=function() {%s}), active=list(x=function(value) NULL))', body)
        fixture <- member_provider_fixture(c(declaration, "client <- Client$new()", "client$run("))
        expect_identical(member_provider_signature(fixture)$signatures[[1L]]$label, "run(original = 1)")
        expect_identical(member_provider_hover(fixture, 2L, 8L)$contents[[1L]], "```r\nrun(original = 1)\n```")
        fixture <- member_provider_fixture(c(declaration, "client <- Client$new()", "client$x("))
        expect_length(member_provider_signature(fixture)$signatures, 0L)
        expect_identical(member_provider_hover(fixture, 2L, 8L)$contents, "Active binding of R6 class `Client`")
    }
})

test_that("Installed callr factory members share completion, signatures and hover", {
    snapshot <- member_prepare_package("callr")
    fixture <- member_provider_fixture(c(
        'job <- callr::r_bg(function() stop("must not run"))', "job$get_result("
    ), list(callr = snapshot))
    items <- member_completion(fixture$uri, fixture$workspace, fixture$document,
        list(row = 1L, col = 4L), TRUE, 200L)
    labels <- vapply(items, `[[`, character(1L), "label")
    # 'cleanup' added in {callr} 3.8.0, so weaken this test slightly
    expected <- intersect(c("cleanup", "get_result"), names(callr::r_process$public_methods))
    expect_true(all(expected %in% labels))
    item <- items[[match("get_result", labels)]]
    expect_identical(item$detail, "get_result()")
    expect_identical(item$kind, CompletionItemKind$Method)
    expect_identical(member_provider_signature(fixture)$signatures[[1L]]$label, "get_result()")
    expect_identical(member_provider_hover(fixture, 1L, 5L)$contents[[1L]], "```r\nget_result()\n```")
})

test_that("Declared R6 members survive an initializer inference timeout across providers", {
    marker <- withr::local_tempfile()
    fixture <- member_provider_fixture(c(
        'Client <- R6::R6Class("Client", public = list(get_result = function() NULL, initialize = function(command) stop("opaque")))',
        sprintf('job <- Client$new({writeLines("ran", %s); stop("input")})', encodeString(marker, quote = "\"")),
        "job$get_result("
    ))
    construct <- member_r6_construct
    expired <- 0L
    testthat::local_mocked_bindings(member_r6_construct = function(callee, actuals, index, budget, depth, trail) {
        if (is.null(callee$result_shape$fields$get_result)) {
            return(construct(callee, actuals, index, budget, depth, trail))
        }
        # Expire during initializer analysis, after the declared receiver is
        # known. Selecting its public method must not start another traversal.
        budget$deadline <- -Inf
        budget$next_time_check <- NULL
        value <- construct(callee, actuals, index, budget, depth, trail)
        expect_true(budget$exhausted)
        expired <<- expired + 1L
        value
    }, .package = "languageserver")
    items <- member_completion(fixture$uri, fixture$workspace, fixture$document,
        list(row = 2L, col = 4L), TRUE, 200L)
    expect_true("get_result" %in% vapply(items, `[[`, character(1L), "label"))
    expect_identical(member_provider_signature(fixture)$signatures[[1L]]$label, "get_result()")
    expect_identical(member_provider_hover(fixture, 2L, 5L)$contents[[1L]], "```r\nget_result()\n```")
    expect_identical(expired, 3L)
    fixture <- member_provider_fixture(c("x <- opaque()", "x$get_result("))
    expect_null(member_provider_hover(fixture, 1L, 3L))
    expect_false(file.exists(marker))
})

test_that("Installed processx members preserve method parameters across providers", {
    skip_if_not_installed("processx")
    snapshot <- member_prepare_package("processx")
    for (case in list(c("is_alive(", "is_alive()"), c("wait(timeout = ", "wait(timeout = -1)"))) {
        fixture <- member_provider_fixture(c(
            'proc <- processx::process$new("must-not-launch")', paste0("proc$", case[[1L]])
        ), list(processx = snapshot))
        items <- member_completion(fixture$uri, fixture$workspace, fixture$document,
            list(row = 1L, col = 5L), TRUE, 200L)
        labels <- vapply(items, `[[`, character(1L), "label")
        expect_true(all(c("is_alive", "wait", "get_exit_status") %in% labels))
        method <- sub("[(].*", "", case[[1L]])
        item <- items[[match(method, labels)]]
        expect_identical(item$detail, case[[2L]])
        expect_identical(item$kind, CompletionItemKind$Method)
        result <- member_provider_signature(fixture)
        expect_identical(result$signatures[[1L]]$label, case[[2L]])
        if (method == "wait") {
            expect_identical(result$activeParameter, 0L)
            expect_identical(result$signatures[[1L]]$parameters[[1L]]$label, c(5L, 17L))
        }
        expect_identical(member_provider_hover(fixture, 1L, 6L)$contents[[1L]],
            sprintf("```r\n%s\n```", case[[2L]]))
    }
})

test_that("Installed processx prepares well inside the snapshot budget with every method", {
    skip_if_not_installed("processx")
    cache_dir <- withr::local_tempdir()
    withr::local_envvar(
        R_USER_CACHE_DIR = cache_dir,
        R_USER_DATA_DIR = cache_dir,
        R_USER_CONFIG_DIR = cache_dir,
        XDG_CACHE_HOME = cache_dir,
        R_LANGSVR_PACKAGE_CACHE_DIR = cache_dir
    )
    withr::local_options(languageserver.package_cache_dir = cache_dir)
    snapshot <- member_prepare_package("processx")
    # The budget is 32 MB. R6 method contexts are stored once per class
    # level, so a class with many methods stays far below it.
    expect_lt(as.numeric(utils::object.size(snapshot)), 16 * 1024^2)
    instance <- snapshot$package_roots$process$fields$new$result_shape
    for (method in instance$fields) {
        expect_null(method$closure$private)
        expect_null(method$closure$super)
    }
    fixture <- member_provider_fixture(c(
        'proc <- processx::process$new("must-not-launch")', "proc$"
    ), list(processx = snapshot))
    items <- member_completion(fixture$uri, fixture$workspace, fixture$document,
        list(row = 1L, col = 5L), TRUE, 200L)
    labels <- vapply(items, `[[`, character(1L), "label")
    expect_setequal(labels, names(processx::process$public_methods))
    clone <- items[[match("clone", labels)]]
    expect_identical(clone$detail, "clone(deep = FALSE)")
})

test_that("testthat reporter generators can be snapshotted together", {
    reporters <- c("Reporter", "SilentReporter", "ListReporter", "StopReporter", "CheckReporter")
    ns <- new.env(parent = baseenv())
    for (name in reporters) assign(name, get(name, asNamespace("testthat")), ns)
    input <- member_namespace_input(ns, package = "reporterfixture", exports = reporters)
    index <- member_package_index(input)
    for (name in reporters) index$package_roots[name] <- list(member_r6_runtime_shape(ns[[name]], index))
    index$roots <- member_public_roots(index, input)
    for (name in names(index$roots)) {
        index$namespace_roots[paste("reporterfixture", name, sep = "::")] <- list(index$roots[[name]])
    }
    snapshot <- NULL
    expect_no_error(snapshot <- member_index_freeze(index))
    fixture <- member_provider_fixture(c("r <- reporterfixture::SilentReporter$new()", "r$"),
        list(reporterfixture = snapshot))
    items <- member_completion(fixture$uri, fixture$workspace, fixture$document,
        list(row = 1L, col = 2L), TRUE, 200L)
    labels <- vapply(items, `[[`, character(1L), "label")
    expect_true(all(c("add_result", "end_reporter", "start_test") %in% labels))
})

test_that("Function syntax drops source references kept by keep.source installs", {
    code <- c(
        "function(x, y = function(z) { z + 1 }, ..., n) {",
        "    g <- function(a, b) {",
        "        a[1, ] <- b[, 2, drop = FALSE]",
        "        lapply(a, function(e) { e$k })",
        "    }",
        "    switch(x, a = , b = 1, 2)",
        "}"
    )
    with_source <- eval(parse(text = code, keep.source = TRUE)[[1L]], baseenv())
    without_source <- eval(parse(text = code, keep.source = FALSE)[[1L]], baseenv())
    expect_false(is.null(attr(with_source, "srcref")))
    stripped <- member_function_syntax(with_source)
    expect_identical(stripped, member_function_syntax(without_source))
    expect_identical(serialize(stripped, NULL), serialize(member_function_syntax(without_source), NULL))
    expect_identical(typeof(stripped[[2L]]), "pairlist")
})

test_that("Installed testthat prepares within the snapshot budget and resolves reporter methods", {
    # Starts subprocesses; too slow and timing-sensitive for CRAN machines.
    skip_on_cran()
    skip_if_not_installed("testthat")
    # Prepare in a clean worker, as production does, so serial testthat runs do
    # not inflate `asNamespace("testthat")$the` with active reporter state.
    pkg_path <- getNamespaceInfo(asNamespace("languageserver"), "path")
    cache_dir <- withr::local_tempdir()
    worker_env <- c(
        callr::rcmd_safe_env(),
        R_USER_CACHE_DIR = cache_dir,
        R_USER_DATA_DIR = cache_dir,
        R_USER_CONFIG_DIR = cache_dir,
        XDG_CACHE_HOME = cache_dir,
        R_LANGSVR_PACKAGE_CACHE_DIR = cache_dir
    )
    snapshot <- callr::r(
        function(pkg_path, cache_dir, libs) {
            .libPaths(libs)
            options(languageserver.package_cache_dir = cache_dir)
            if (file.exists(file.path(pkg_path, "Meta", "package.rds"))) {
                loadNamespace("languageserver", lib.loc = dirname(pkg_path))
            } else {
                getExportedValue("pkgload", "load_all")(
                    pkg_path, quiet = TRUE, export_all = FALSE, helpers = FALSE
                )
            }
            get("member_prepare_package", envir = asNamespace("languageserver"))("testthat")
        },
        args = list(pkg_path = pkg_path, cache_dir = cache_dir, libs = .libPaths()),
        env = worker_env,
        system_profile = FALSE,
        user_profile = FALSE
    )
    expect_lt(as.numeric(utils::object.size(snapshot)), 30 * 1024^2)
    fixture <- member_provider_fixture(c(
        "reporter <- testthat::ProgressReporter$new()", "reporter$"
    ), list(testthat = snapshot))
    items <- member_completion(fixture$uri, fixture$workspace, fixture$document,
        list(row = 1L, col = 9L), TRUE, 200L)
    labels <- vapply(items, `[[`, character(1L), "label")
    expect_true(all(c("add_result", "end_reporter", "start_test") %in% labels))
    item <- items[[match("start_test", labels)]]
    expect_identical(item$kind, CompletionItemKind$Method)
})

test_that("callr and processx members work through LSP without library calls or execution", {
    skip_on_cran()
    skip_if_not_installed("processx")
    root <- withr::local_tempdir()
    marker <- file.path(root, "executed")
    client <- language_client(working_dir = root)
    # The first completion is held until the cold resolve and member workers
    # finish. Coverage loads the instrumented namespace in both workers while
    # the other parallel test process competes for the same runner CPUs.
    timeout <- if (identical(Sys.getenv("R_COVR"), "true")) 150 else 15
    cases <- list(
        list(package = "callr", receiver = "job", method = "get_result", signature = "get_result()",
            code = sprintf('job <- callr::r_bg(function() {writeLines("ran", %s); stop("input")})',
                encodeString(marker, quote = "\""))),
        list(package = "processx", receiver = "proc", method = "is_alive", signature = "is_alive()",
            code = sprintf('proc <- processx::process$new({writeLines("ran", %s); stop("command")})',
                encodeString(marker, quote = "\"")))
    )
    for (case in cases) {
        path <- file.path(root, paste0(case$package, ".R"))
        call <- paste0(case$receiver, "$", case$method, "()")
        did_open(client, path, text = paste(case$code, call, sep = "\n"))
        deadline <- Sys.time() + timeout
        repeat {
            remaining <- max(1, as.numeric(difftime(deadline, Sys.time(), units = "secs")))
            result <- respond_completion(client, path, c(1L, nchar(case$receiver) + 1L),
                retry = FALSE, timeout = remaining)
            labels <- vapply(result$items, `[[`, character(1L), "label")
            if (case$method %in% labels || Sys.time() > deadline) break
            Sys.sleep(0.1)
        }
        expect_true(case$method %in% labels, info = case$package)
        result <- respond_signature(client, path, c(1L, nchar(call) - 1L), retry = FALSE)
        expect_length(result$signatures, 1L)
        if (length(result$signatures) != 1L) next
        expect_identical(result$signatures[[1L]]$label, case$signature)
        result <- respond_hover(client, path, c(1L, nchar(case$receiver) + 2L), retry = FALSE)
        expect_identical(result$contents[[1L]], sprintf("```r\n%s\n```", case$signature))
        expect_false(file.exists(marker))
    }
})

test_that("Installed Redux methods provide signatures and hover without connecting", {
    skip_if_not_installed("redux")
    snapshot <- member_prepare_package("redux")
    for (case in list(c("GET(key = ", "GET(key)", "0"),
            c("SET(value = ", "SET(key, value, EX = NULL, PX = NULL, condition = NULL)", "1"),
            c("PING(message = ", "PING(message = NULL)", "0"))) {
        fixture <- member_provider_fixture(c('redis <- redux::hiredis(host = "127.0.0.1")',
                paste0("redis$", case[[1L]])), list(redux = snapshot))
        result <- member_provider_signature(fixture)
        expect_identical(result$signatures[[1L]]$label, case[[2L]])
        expect_identical(result$activeParameter, as.integer(case[[3L]]))
        expect_identical(member_provider_hover(fixture, 1L, 7L)$contents[[1L]],
            sprintf("```r\n%s\n```", case[[2L]]))
    }
})

test_that("Redux completion, signature and hover work through LSP without a server", {
    skip_on_cran()
    skip_if_not_installed("redux")
    root <- withr::local_tempdir()
    path <- file.path(root, "redis.R")
    client <- language_client(working_dir = root)
    lines <- c('redis <- redux::hiredis(host = "127.0.0.1")', "redis$GET(key = \"key\")")
    did_open(client, path, text = paste(lines, collapse = "\n"))
    deadline <- Sys.time() + 15
    repeat {
        result <- respond_completion(client, path, c(1L, 6L), retry = FALSE)
        labels <- vapply(result$items, `[[`, character(1L), "label")
        if ("GET" %in% labels || Sys.time() > deadline) break
        Sys.sleep(0.1)
    }
    expect_true(all(c("GET", "SET", "PING") %in% labels))
    result <- respond_signature(client, path, c(1L, 10L), retry = FALSE)
    expect_identical(result$signatures[[1L]]$label, "GET(key)")
    result <- respond_hover(client, path, c(1L, 7L), retry = FALSE)
    expect_identical(result$contents[[1L]], "```r\nGET(key)\n```")
})

test_that("Unknown and shadowed member receivers never resolve unrelated bare functions", {
    for (lines in list(
        c("unknown$sum("),
        c("x <- list(sum=function(value) NULL)", "f <- function(x) {x$sum(", "}"),
        c("x <- list(sum=function(value) NULL)", "x <- opaque()", "x$sum(")
    )) {
        fixture <- member_provider_fixture(lines)
        row <- if (endsWith(tail(lines, 1L), "}")) length(lines) - 2L else length(lines) - 1L
        expect_length(member_provider_signature(fixture, row)$signatures, 0L)
        col <- regexpr("sum", lines[[row + 1L]])[[1L]]
        expect_null(member_provider_hover(fixture, row, col))
    }
    fixture <- member_provider_fixture(c("x <- list(sum=function(value) NULL)", "# x$sum("))
    expect_length(member_provider_signature(fixture)$signatures, 0L)
    expect_null(member_provider_hover(fixture, 1L, 6L))
})

test_that("Member providers stay inside R cells", {
    fixture <- member_provider_fixture(c(
        "```{r}", "x <- list(run=function(value=1) NULL)",
        "x$run(", "```", "```{python}", "x$run(", "```",
        "```{r}", "  (", "```"
    ), language = "quarto")
    expect_identical(member_provider_signature(fixture, 2L)$signatures[[1L]]$label, "run(value = 1)")
    expect_identical(member_provider_hover(fixture, 2L, 3L)$contents, "```r\nrun(value = 1)\n```")
    expect_null(member_provider_hover(fixture, 5L, 3L))
    expect_null(member_provider_signature(fixture, 5L)$signatures)
    expect_null(member_call_location(fixture$document, list(row = 8L, col = 3L)))
    expect_null(member_call_location(
        Document$new("file:///paren.R", content = "(x"),
        list(row = 0L, col = 2L)
    ))
})

test_that("Installed package members retain method signatures and documentation identity", {
    skip_if_not_installed("polars")
    snapshot <- member_prepare_package("polars")
    fixture <- member_provider_fixture(c(
        "library(polars)", "q <- pl$scan_csv(csv_file)",
        "q$group_by(\"Species\", .maintain_order = "
    ), list(polars = snapshot))
    queried <- character()
    fixture$workspace$get_documentation <- function(key, package, isf, uri) {
        expect_identical(package, "polars")
        expect_identical(uri, fixture$uri)
        queried <<- c(queried, key)
        list(
            description = paste("Documentation for", key),
            arguments = list(.maintain_order = "Preserve group order.")
        )
    }
    signature <- member_provider_signature(fixture)
    expect_identical(signature$signatures[[1L]]$label, "group_by(..., .maintain_order = FALSE)")
    expect_identical(signature$activeParameter, 1L)
    hover <- member_provider_hover(fixture, 2L, 5L)
    expect_match(hover$contents[[2L]], "lazyframe__group_by", fixed = TRUE)
    argument <- member_provider_hover(fixture, 2L, 25L)
    expect_identical(argument$contents[[1L]], "```r\ngroup_by(..., .maintain_order = FALSE)\n```")
    expect_identical(argument$contents[[2L]], "`.maintain_order` - Preserve group order.")
    expect_true(all(queried == "lazyframe__group_by"))

    for (case in list(
        c("pl$col(\"x\")$sum(", "expr__sum"),
        # Series delegates this operation to Expr and copies its formals.
        c("pl$Series(\"x\", 1:3)$sum(", "expr__sum"),
        c("pl$col(\"x\")$str$to_uppercase(", "expr_str_to_uppercase")
    )) {
        fixture$document$set_content(2L, c("library(polars)", case[[1L]]))
        data <- parse_document(fixture$uri, fixture$document$content)
        data$version <- 2L
        fixture$document$update_parse_data(data)
        result <- member_provider_signature(fixture)
        expect_length(result$signatures, 1L)
        expect_identical(tail(queried, 1L), case[[2L]])
        col <- nchar(case[[1L]]) - 2L
        hover <- member_provider_hover(fixture, 1L, col)
        expect_match(hover$contents[[2L]], case[[2L]], fixed = TRUE)
    }
    fixture$document$set_content(3L, c("library(polars)", "pl$scan_csv(csv_file)$`_ldf`$collect("))
    data <- parse_document(fixture$uri, fixture$document$content)
    data$version <- 3L
    fixture$document$update_parse_data(data)
    expect_identical(member_provider_signature(fixture)$signatures[[1L]]$label, "collect(engine)")
    expect_identical(tail(queried, 1L), "PlRLazyFrame_collect")
})

test_that("Polars query providers prefer receiver metadata over competing cached exports", {
    skip_if_not_installed("polars")
    snapshot <- member_prepare_package("polars")
    unrelated <- member_generic_index("")
    unrelated$package <- "unrelated"
    unrelated$exports <- c("scan_csv", "csv_file", "filter", "col", "group_by", "agg", "all", "median", "collect", "sum")
    lines <- c(
        "library(polars)", "",
        "csv_file <- tempfile(fileext = \".csv\")",
        "write.csv(iris, csv_file, row.names = FALSE)", "",
        "q <- pl$scan_csv(csv_file, infer_schema_files = 10)", "q # not working", "",
        "q1 <- q$filter(pl$col(\"Sepal.Length\") > 5)", "q1 # not working", "",
        "q1 <- q$filter(pl$col(\"Sepal.Length\") > 5)$group_by(\"Species\")$agg(pl$all()$median())$collect()",
        "q1 # not working", "",
        "q2 <- q1$group_by(\"Species\")$agg(pl$all()$sum())", "q2 # not working"
    )
    metadata <- MemberMetadataCache$new(32 * 1024^2)
    metadata$set("unrelated", as.list(member_index_thaw(member_index_freeze(unrelated))))
    metadata$set("polars", as.list(member_index_thaw(snapshot)))
    # Prepare in a clean worker, as production does, so S4 fixtures declared by
    # other tests do not inflate the methods namespace snapshot.
    methods_snapshot <- callr::r(function() languageserver:::member_prepare_package("methods"))
    metadata$set("methods", as.list(member_index_thaw(methods_snapshot)))
    expect_true(metadata$has("polars"))
    restored <- character()
    cache <- metadata
    metadata <- list(keys = cache$keys, catalog = cache$catalog, get = function(package) {
        if (!cache$.__enclos_env__$private$indexes$has(package)) restored <<- c(restored, package)
        cache$get(package)
    })
    for (row in c(6L, 9L, 12L, 15L)) {
        edited <- lines
        name <- sub(" .*", "", edited[[row + 1L]])
        edited[[row + 1L]] <- paste0(name, "$group_by(\"Species\", .maintain_order = ")
        fixture <- member_provider_fixture(edited)
        fixture$workspace$member_metadata <- metadata
        queried <- character()
        fixture$workspace$get_documentation <- function(key, package, isf, uri) {
            expect_identical(package, "polars")
            queried <<- c(queried, key)
            list(description = paste("Documentation for", key))
        }
        point <- list(row = row, col = nchar(name) + 1L)
        resolved <- member_resolve_cursor(fixture$uri, fixture$workspace, fixture$document, point,
            member_cursor(fixture$document, point))
        expect_identical(resolved$value$type, if (row < 12L) "polars_lazy_frame" else "polars_data_frame")
        expect_false(resolved$budget$exhausted)
        items <- member_completion(fixture$uri, fixture$workspace, fixture$document, point, TRUE, 200L)
        labels <- vapply(items, `[[`, character(1L), "label")
        expect_true(all(c("filter", "group_by") %in% labels))
        expect_identical("collect" %in% labels, row < 12L)
        expect_identical("lazy" %in% labels, row >= 12L)
        item <- items[[match("group_by", labels)]]
        signature <- "group_by(..., .maintain_order = FALSE)"
        expect_identical(item$data$type, "member")
        expect_identical(item$data$package, "polars")
        expect_identical(item$detail, signature)
        expect_identical(member_provider_signature(fixture, row)$signatures[[1L]]$label, signature)
        hover <- member_provider_hover(fixture, row, nchar(name) + 3L)
        expect_identical(hover$contents[[1L]], sprintf("```r\n%s\n```", signature))
        expect_true(all(queried == if (row < 12L) "lazyframe__group_by" else "dataframe__group_by"))
    }
    expect_identical(restored, "polars")
})

test_that("Polars providers survive large metadata for several open documents over LSP", {
    skip_on_cran()
    skip_if_not_installed("polars")
    root <- withr::local_tempdir()
    # A test-only request reads the actual server cache. Namespace completion
    # can succeed before member metadata arrives, so it is not a readiness test.
    script <- paste(
        "languageserver:::lsp_settings$update_from_options()",
        "server <- languageserver:::LanguageServer$new('localhost', NULL)",
        "server$request_handlers[['test/memberMetadata']] <- function(self, id, params) {",
        "cache <- self$get_workspace(params$uri)$member_metadata",
        "ready <- all(c('polars', 'methods', 'R6') %in% cache$keys())",
        "if (ready && isTRUE(params$evict)) cache$get('methods')",
        "decoded <- cache$.__enclos_env__$private$indexes$keys()",
        "self$deliver(languageserver:::Response$new(id, result = list(ready = ready, decoded = decoded)))",
        "}", "server$run()", sep = "; "
    )
    original_new <- LanguageClient$new
    mockery::stub(language_client, "LanguageClient$new", function(command, args) {
        original_new(command, c("--no-echo", "-e", script))
    })
    client <- language_client(working_dir = root)
    path <- file.path(root, "polars.R")
    lines <- c(
        "library(polars)",
        "q <- pl$scan_csv(csv_file, infer_schema_files = 10)",
        "q1 <- q$filter(pl$col(\"Sepal.Length\") > 5)", "q1",
        "q1 <- q$filter(pl$col(\"Sepal.Length\") > 5)$group_by(\"Species\")$agg(pl$all()$median())$collect()",
        "q1", "q2 <- q1$group_by(\"Species\")$agg(pl$all()$sum())", "q2",
        "Leaf <- methods::setClass(\"Leaf\", slots = c(value = \"numeric\"))",
        "Dataset <- R6::R6Class(\"Dataset\", public = list(value = 1))"
    )
    did_open(client, path, text = lines)
    other <- file.path(root, "other.R")
    did_open(client, other, text = "methods::setClass(\"Other\", slots = c(value = \"numeric\"))")
    ready <- respond(client, "test/memberMetadata", list(uri = path_to_uri(path), evict = TRUE),
        timeout = if (identical(Sys.getenv("R_COVR"), "true")) 60 else 20,
        retry_when = function(result) !isTRUE(result$ready))
    expect_true(ready$ready)
    expect_true("methods" %in% ready$decoded)
    expect_false("polars" %in% ready$decoded)
    for (row in c(3L, 5L, 7L)) {
        edited <- lines
        name <- edited[[row + 1L]]
        edited[[row + 1L]] <- paste0(name, "$group_by(\"Species\", .maintain_order = ")
        notify(client, "textDocument/didChange", list(
            textDocument = list(uri = path_to_uri(path), version = row),
            contentChanges = list(list(text = paste(edited, collapse = "\n")))
        ))
        completion <- respond_completion(client, path, c(row, nchar(name) + 1L), retry = FALSE)
        expect_true(all(vapply(completion$items, function(item) identical(item$data$type, "member"), logical(1L))))
        labels <- vapply(completion$items, `[[`, character(1L), "label")
        expect_true(all(c("filter", "group_by") %in% labels))
        expect_identical("collect" %in% labels, row == 3L)
        expect_identical("lazy" %in% labels, row != 3L)
        signature <- respond_signature(client, path, c(row, nchar(edited[[row + 1L]])), retry = FALSE)
        expect_identical(signature$signatures[[1L]]$label, "group_by(..., .maintain_order = FALSE)")
        hover <- respond_hover(client, path, c(row, nchar(name) + 3L), retry = FALSE)
        expect_identical(hover$contents[[1L]], "```r\ngroup_by(..., .maintain_order = FALSE)\n```")
    }
})

test_that("Member signature and hover work over LSP immediately after an edit", {
    skip_on_cran()
    skip_if_not_installed("polars")
    root <- withr::local_tempdir()
    client <- language_client(working_dir = root)
    path <- file.path(root, "polars.R")
    uri <- path_to_uri(path)
    lines <- c("library(polars)", "q <- pl$scan_csv(csv_file)", "q$group_by(\"Species\")")
    did_open(client, path, text = paste(lines, collapse = "\n"))
    deadline <- Sys.time() + if (identical(Sys.getenv("R_COVR"), "true")) 60 else 20
    repeat {
        ready <- respond_completion(client, path, c(2L, 2L), retry = FALSE)
        if (any(vapply(ready$items, function(item) identical(item$data$type, "member"), logical(1L)))) break
        if (Sys.time() > deadline) break
        Sys.sleep(0.1)
    }
    expect_true(any(vapply(ready$items, function(item) identical(item$data$type, "member"), logical(1L))))
    notify(client, "workspace/didChangeConfiguration", list(settings = list(parse_delay = 0.5)))
    lines[[3L]] <- "q$group_by(\"Species\", .maintain_order = "
    notify(client, "textDocument/didChange", list(
        textDocument = list(uri = uri, version = 2L),
        contentChanges = list(list(text = paste(lines, collapse = "\n")))
    ))
    # No request retry or delay after didChange: the handler must wait for the
    # current incomplete parse, then infer the method on the first request.
    result <- respond_signature(client, path, c(2L, nchar(lines[[3L]])), retry = FALSE)
    expect_identical(result$signatures[[1L]]$label, "group_by(..., .maintain_order = FALSE)")
    expect_equal(result$activeParameter, 1L)
    expect_match(result$signatures[[1L]]$documentation$value, "group", ignore.case = TRUE)

    lines[[3L]] <- "q$group_by(\"Species\", .maintain_order = TRUE)"
    notify(client, "textDocument/didChange", list(
        textDocument = list(uri = uri, version = 3L),
        contentChanges = list(list(text = paste(lines, collapse = "\n")))
    ))
    hover <- respond_hover(client, path, c(2L, 5L), retry = FALSE)
    expect_identical(hover$contents[[1L]], "```r\ngroup_by(..., .maintain_order = FALSE)\n```")
    expect_match(hover$contents[[2L]], "group", ignore.case = TRUE)
    expect_equal(hover$range$start$character, 2L)
    expect_equal(hover$range$end$character, 10L)
    argument <- respond_hover(client, path, c(2L, 25L), retry = FALSE)
    expect_match(argument$contents[[2L]], "`.maintain_order`", fixed = TRUE)
})

Try the languageserver package in your browser

Any scripts or data that you put into this service are public.

languageserver documentation built on Nov. 16, 2026, 9:07 a.m.