Vscode-r: goto defination not work

Created on 12 Jul 2020  路  22Comments  路  Source: Ikuyadeu/vscode-R

bug

Most helpful comment

@kongdd

You may try the following code in your R session in the workspace and see if it could reproduce the wrong XML token:

library(xml2)
library(glue)

e <- parse("./R/CMIP5Files.R")
xml <- xmlparsedata::xml_parse_data(e)
xml_file <- tempfile(fileext = ".xml")
writeLines(xml, xml_file)
xdoc <- read_xml(xml_file)

xdoc_find_token <- function(x, line, col) {
  xpath <- glue("//*[not(*)][(@line1 < {line} or (@line1 = {line} and @col1 <= {col})) and (@line2 > {line} or (@line2 = {line} and @col2 >= {col}))]",
    line = line, col = col)
  xml_find_first(x, xpath)
}

node <- xdoc_find_token(xdoc, 185, 62)
node
xml_text(node)

The expected result is

> node                                                                                                                                                                                                                                   
{xml_node}
<SYMBOL line1="185" col1="57" line2="185" col2="71" start="20592" end="20606">

> xml_text(node)                                                                                                                                                                                                                         
[1] "filter_duration"

but somehow you get OP-COMMA and ,, respectively. Would you like to post what you get in node?

All 22 comments

Would you like to elaborate?

I am using VScode to write R package. Pressing F12 give an error no defination found for all the functions.

Would you like to provide a minimal reproducible example?

Go-to-definition is actually provided by languageserver via vscode-r-lsp. Have you installed both correctly?

Taking this package (https://github.com/kongdd/CMIP5tools/blob/master/R/CMIP5Files.R#L185) as example, I just can't jump to the defination of function filter_duration.

Maybe I should pull an issue at vscode-r-lsp.

Would you like to try the latest development version of languageserver with

devtools::install_github("REditorSupport/languageserver")

I tried with your example of filter_duration and it works perfectly.

Sorry. I have tried. But still not worknig.

  1. I have reinstalled languageserver by devtools::install_github("REditorSupport/languageserver").

  2. I have clear all the files of VScode in ~/.config/Code, ~/.vscode and reinstall VScode after sudo apt purge code.
    After that, reinstall R LSP.

But still not work! Still give me the error that no defination found

Is OS system maters? I am using Ubuntu 20.04.

I'll try on Ubuntu 20.04 and see if there's something wrong.

Thank you! In my tries, win10 2004 and WSL ubuntu1804 have the same problem.

I tested on macOS 10.15.5 and Ubuntu 20.04 and everything works as expected.

Would you please follow the steps below:

  1. Turn on r.lsp.debug in VSCode
  2. Restart/reload vscode
  3. Open the R project and select Output panel and view R Language Server logging.
  4. Go to the file
  5. Put the cursor on filter_duration and go to definition.
  6. Copy all logs as code here and let's see if anything is not working properly.

Also, please post your R session info. Thanks!

Thank you! The log is in the attachment.

VScode V1.47.0, Ubuntu 20.04

> library(languageserver)
> sessionInfo()

R version 4.0.2 (2020-06-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3

locale:
 [1] LC_CTYPE=en_AU.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_AU.UTF-8        LC_COLLATE=en_AU.UTF-8    
 [5] LC_MONETARY=en_AU.UTF-8    LC_MESSAGES=en_AU.UTF-8   
 [7] LC_PAPER=en_AU.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_AU.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] languageserver_0.3.6

loaded via a namespace (and not attached):
[1] compiler_4.0.2 R6_2.4.1       xml2_1.3.2  

LSP.log

Thanks!

The expected logging should look like


[2020-07-13 01:02:10.204] SYMBOL filter_duration

[2020-07-13 01:02:10.219] guess namespace: _workspace_

[2020-07-13 01:02:10.219] guess namespace: _workspace_

[2020-07-13 01:02:10.220] deliver:  ["Response", "Message", "R6"]

[2020-07-13 01:02:12.238] received:  Content-Length: 191

[2020-07-13 01:02:12.249] received payload.

[2020-07-13 01:02:12.249] handling request:  textDocument/definition

[2020-07-13 01:02:12.261] token: {
  "full_token": "filter_duration",
  "right_token": "r_duration",
  "package": null,
  "accessor": "",
  "token": "filter_duration"
}

[2020-07-13 01:02:12.264] definition:  SYMBOL filter_duration

[2020-07-13 01:02:12.266] definition:  {
  "uri": "file:///home/renkun/Workspaces/CMIP5tools/R/CMIP5Files.R",
  "range": {
    "start": {
      "line": 126,
      "character": 0
    },
    "end": {
      "line": 149,
      "character": 1
    }
  }
}

But in your log, it seems it does not find the definition of a wrong token OP-COMMA:

[2020-07-13 00:55:54.768] handling request:  textDocument/definition

[2020-07-13 00:55:54.769] token: {
  "full_token": "filter_duration",
  "right_token": "",
  "package": null,
  "accessor": "",
  "token": "filter_duration"
}

[2020-07-13 00:55:54.777] definition:  OP-COMMA ,

[2020-07-13 00:55:54.778] deliver:  ["Response", "Message", "R6"]

Your document highlight does not seem to work either. I'll take a closer look at how this could happen. I guess there are some encoding issue involved? @randy3k any idea?

row <- point$row + 1
col <- point$col + 1
token <- xdoc_find_token(xdoc, row, col)

@kongdd What's the encoding of your file?

Thanks for your help! Waiting for your news.

I am using the UTF-8.

But the textDocument/hover can work normally.

2020-07-13 01:14:07.741] handling request:  textDocument/hover

[2020-07-13 01:14:07.744] token: {
  "full_token": "filter_duration",
  "right_token": "uration",
  "package": null,
  "accessor": "",
  "token": "filter_duration"
}

[2020-07-13 01:14:07.746] guess namespace: _workspace_

[2020-07-13 01:14:07.747] hover: {
  "uri": "file:///home/kong/github/CMIP5tools/R/CMIP5Files.R",
  "version": 1
}

[2020-07-13 01:14:07.758] SYMBOL filter_duration

[2020-07-13 01:14:07.760] guess namespace: _workspace_

[2020-07-13 01:14:07.762] guess namespace: _workspace_

[2020-07-13 01:14:07.764] deliver:  ["Response", "Message", "R6"]

Could you tell me how to debug function in languageserver. I am willing to involve in.

The token was detected correctly. It may be the issue of xdoc_find_token?

@kongdd
The easier way is to insert logger$info in the source code, install the package, and examine the log.

Yes, document$detect_token(point) returns the correct document token, but xdoc_find_token(xdoc, row, col) returns the wrong XML token.

@kongdd I'm wondering if any definition ever works for you? Or it just stops working occasionally like this? I guess we could isolate some minimal code to reproduce this and see why the XML document and XPath have inconsistent results between your machine and mine.

@kongdd

You may try the following code in your R session in the workspace and see if it could reproduce the wrong XML token:

library(xml2)
library(glue)

e <- parse("./R/CMIP5Files.R")
xml <- xmlparsedata::xml_parse_data(e)
xml_file <- tempfile(fileext = ".xml")
writeLines(xml, xml_file)
xdoc <- read_xml(xml_file)

xdoc_find_token <- function(x, line, col) {
  xpath <- glue("//*[not(*)][(@line1 < {line} or (@line1 = {line} and @col1 <= {col})) and (@line2 > {line} or (@line2 = {line} and @col2 >= {col}))]",
    line = line, col = col)
  xml_find_first(x, xpath)
}

node <- xdoc_find_token(xdoc, 185, 62)
node
xml_text(node)

The expected result is

> node                                                                                                                                                                                                                                   
{xml_node}
<SYMBOL line1="185" col1="57" line2="185" col2="71" start="20592" end="20606">

> xml_text(node)                                                                                                                                                                                                                         
[1] "filter_duration"

but somehow you get OP-COMMA and ,, respectively. Would you like to post what you get in node?

The only way I could reproduce your "no definition" is to put the cursor in the end of filter_duration and before ,, which we have the correct document token but cannot find the XML token at line 185 col 72 since the XML doc has

<expr line1="185" col1="57" line2="185" col2="71" start="20592" end="20606">
<SYMBOL line1="185" col1="57" line2="185" col2="71" start="20592" end="20606">filter_duration</SYMBOL>
</expr>
<OP-COMMA line1="185" col1="72" line2="185" col2="72" start="20607" end="20607">,</OP-COMMA>

If this is the case, then you might need to put your cursor on the word, not in the end of the word, or otherwise it only finds the OP-COMMA node which encloses your cursor position.

Thank you @renkun-ken and @randy3k .

It turn out the problem of cursor position. When I double click the function name, the curcor jump to the end of the function name and "no defination found" was returned. But when I put the cursor in the middle of the function name, it works smoothly.

Thanks for your powerful package!

Just notice that selecting the function name and go-to-definition does not work as the cursor is in the end of the token, which I think is obviously not ideal.

I'm taking a closer look and see if we can do anything about it.

It seems to me it is an alignment issue. Maybe just need to +1 or -1.

Was this page helpful?
0 / 5 - 0 ratings