Skip to content

Commit 4883f44

Browse files
authored
Merge pull request #344 from renkun-ken/hover-non-function
Fix hover on non-function symbol
2 parents be13407 + 525aea4 commit 4883f44

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

R/hover.R

+28-11
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ hover_reply <- function(id, uri, workspace, document, point) {
1919
token_result <- document$detect_token(point)
2020
range <- token_result$range
2121

22-
if (is.null(token_result$package)) {
23-
signs <- workspace$guess_namespace(token_result$token)
24-
} else {
25-
signs <- token_result$package
26-
}
27-
28-
sig <- workspace$get_signature(token_result$token, signs,
29-
exported_only = token_result$accessor != ":::")
3022
contents <- NULL
3123
resolved <- FALSE
3224

@@ -48,7 +40,7 @@ hover_reply <- function(id, uri, workspace, document, point) {
4840
if (token_name %in% c("SYMBOL", "SYMBOL_FUNCTION_CALL")) {
4941
# symbol
5042
preceding_dollar <- xml_find_first(token, "preceding-sibling::OP-DOLLAR")
51-
if (length(preceding_dollar) == 0 && (is.null(signs) || signs != WORKSPACE || is.null(sig))) {
43+
if (length(preceding_dollar) == 0) {
5244
enclosing_scopes <- xdoc_find_enclosing_scopes(xdoc,
5345
row, col, top = TRUE)
5446
xpath <- glue(hover_xpath,
@@ -205,8 +197,30 @@ hover_reply <- function(id, uri, workspace, document, point) {
205197

206198
if (!resolved) {
207199
contents <- workspace$get_help(token_result$token, token_result$package)
208-
if (is.null(contents) && !is.null(sig)) {
200+
if (is.null(contents)) {
201+
def_text <- NULL
202+
209203
doc <- workspace$get_documentation(token_result$token, token_result$package)
204+
signs <- if (is.null(token_result$package)) {
205+
workspace$guess_namespace(token_result$token)
206+
} else {
207+
token_result$package
208+
}
209+
sig <- workspace$get_signature(token_result$token, signs,
210+
exported_only = token_result$accessor != ":::")
211+
212+
if (is.null(sig)) {
213+
def <- workspace$get_definition(token_result$token, token_result$package,
214+
exported_only = token_result$accessor != ":::")
215+
if (!is.null(def)) {
216+
def_doc <- workspace$documents$get(def$uri)
217+
def_line1 <- def$range$start$line + 1
218+
def_text <- def_doc$line(def_line1)
219+
}
220+
} else {
221+
def_text <- sig
222+
}
223+
210224
doc_string <- NULL
211225
if (is.character(doc)) {
212226
doc_string <- doc
@@ -217,7 +231,10 @@ hover_reply <- function(id, uri, workspace, document, point) {
217231
doc_string <- doc$markdown
218232
}
219233
}
220-
contents <- c(sprintf("```r\n%s\n```", sig), doc_string)
234+
contents <- c(
235+
if (!is.null(def_text)) sprintf("```r\n%s\n```", def_text),
236+
doc_string
237+
)
221238
}
222239
}
223240

R/workspace.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ Workspace <- R6::R6Class("Workspace",
154154

155155
get_documentation = function(topic, pkgname = NULL, isf = FALSE) {
156156
if (is.null(pkgname)) {
157-
pkgname <- self$guess_namespace(topic, isf = TRUE)
157+
pkgname <- self$guess_namespace(topic, isf = isf)
158158
if (is.null(pkgname)) {
159159
return(NULL)
160160
}

tests/testthat/test-hover.R

+18
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,24 @@ test_that("Hover works with local function", {
275275
))
276276
})
277277

278+
test_that("Hover works across multiple files", {
279+
skip_on_cran()
280+
client <- language_client()
281+
282+
withr::local_tempfile(c("defn_file", "query_file"), fileext = ".R")
283+
writeLines(c("test <- 1"), defn_file)
284+
writeLines(c("test + 1"), query_file)
285+
286+
client %>% did_save(defn_file)
287+
client %>% did_save(query_file)
288+
289+
result <- client %>% respond_hover(query_file, c(0, 0))
290+
291+
expect_equal(result$range$start, list(line = 0, character = 0))
292+
expect_equal(result$range$end, list(line = 0, character = 4))
293+
expect_equal(result$contents, "```r\ntest <- 1\n```")
294+
})
295+
278296
test_that("Simple hover works in Rmarkdown", {
279297
skip_on_cran()
280298
client <- language_client()

0 commit comments

Comments
 (0)