Skip to content

Commit

Permalink
Merge pull request #467 from renkun-ken/hover-eq-assign
Browse files Browse the repository at this point in the history
Fix hover on function defined with `EQ_ASSIGN`
  • Loading branch information
renkun-ken authored Aug 11, 2021
2 parents b2148e3 + 695a0c6 commit 2a8ac66
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion R/hover.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ hover_reply <- function(id, uri, workspace, document, point) {
if (length(all_defs)) {
last_def <- all_defs[[length(all_defs)]]
def_func <- xml_find_first(last_def,
"self::expr[LEFT_ASSIGN | RIGHT_ASSIGN | EQ_ASSIGN]/expr[FUNCTION | OP-LAMBDA]")
"self::*[LEFT_ASSIGN | RIGHT_ASSIGN | EQ_ASSIGN]/expr[FUNCTION | OP-LAMBDA]")
if (length(def_func)) {
func_line1 <- as.integer(xml_attr(def_func, "line1"))
func_col1 <- as.integer(xml_attr(def_func, "col1"))
Expand Down
47 changes: 46 additions & 1 deletion tests/testthat/test-hover.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ test_that("Hover on user function works", {
writeLines(
c(
"test1 <- function(x, y) x + 1",
"test1"
"test1",
"test2 = function(x, y) x - 1",
"test2"
),
temp_file
)
Expand All @@ -43,6 +45,49 @@ test_that("Hover on user function works", {
expect_length(result$contents, 1)
expect_equal(result$contents[1], "```r\ntest1(x, y)\n```")
expect_equal(result$range$end$character, 5)

result <- client %>% respond_hover(temp_file, c(3, 3))
expect_length(result$contents, 1)
expect_equal(result$contents[1], "```r\ntest2(x, y)\n```")
expect_equal(result$range$end$character, 5)
})

test_that("Hover on user function with multi-lined arguments works", {
skip_on_cran()
client <- language_client()

temp_file <- withr::local_tempfile(fileext = ".R")
writeLines(
c(
"test1 <- function(",
" x, # arg 1",
" y # arg 2",
") {",
" x + y",
"}",
"test1",
"test2 = function(",
" x, # arg 1",
" y # arg 2",
") {",
" x + y",
"}",
"test2"
),
temp_file
)

client %>% did_save(temp_file)

result <- client %>% respond_hover(temp_file, c(6, 3))
expect_length(result$contents, 1)
expect_equal(result$contents[1], "```r\ntest1(x, y)\n```")
expect_equal(result$range$end$character, 5)

result <- client %>% respond_hover(temp_file, c(13, 3))
expect_length(result$contents, 1)
expect_equal(result$contents[1], "```r\ntest2(x, y)\n```")
expect_equal(result$range$end$character, 5)
})

test_that("Hover on variable works", {
Expand Down

0 comments on commit 2a8ac66

Please sign in to comment.