languageserver
languageserver copied to clipboard
Document symbol display in Rmd is not reliable
In R Markdown files, sometimes document symbols are not displayed in VSCode's breadcrumbs view.
However, I have no idea what causes this issue. Please see the reprex below.
Reprex
Case 1: Document Symbol Appears Correctly
- Download this Rmd file
- Open the file in VSCode.
- Press
Ctrl+Shift+. (period).
Case 2: Document Symbol Does Not Appear
- Download another Rmd file
- Open the file in VSCode.
- Press
Ctrl+Shift+. (period).
System Info
Version: 1.55.2 (system setup) Commit: 3c4e3df9e89829dce27b7b5c24508306b151f30d Date: 2021-04-13T09:35:57.887Z Electron: 11.3.0 Chrome: 87.0.4280.141 Node.js: 12.18.3 V8: 8.7.220.31-electron.0 OS: Windows_NT x64 10.0.19042
For case 2, if you turn on diagnostics, you will notice a syntax error at line 101-102 as the following:
Although you mark the chunk as eval=FALSE, the code is still parsed. If a syntax error is found, then we can't generate document symbols with it.
In this case, I suggest that you write as the following so that it won't be regarded as an RMarkdown code chunk but a plain R code block in the markdown document and the code in it won't be parsed.
```r
ggplot(data = <DATA>) +
<GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))
```
Aw, thanks! That makes sense. BTW, my lsp diagnostic has already been turned on like below:
"r.lsp.diagnostics": true
However, it does not give me such the error message as your screenshot.
Would there be any additional settings I need to configure?
You may want to update your lintr to latest development version and then you should be able to see the parse error.
Thanks! It works now.
I guess we might want to handle the case that eval = false or error = TRUE.
@randy3k, that would be greatly appreciated. I'll re-open this issue.
I have just found that Math formula-contained code chunk (e.g., bookdown-style Theorem) also generates errors.
```{theorem, name="Pythagorean theorem"}
For a right triangle, if $c$ denotes the length of the hypotenuse
and $a$ and $b$ denote the lengths of the other two sides, we have
$$a^2 + b^2 = c^2$$
```
Retrieved from https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html
Shouldn't it be excluded from errors?
@jooyoungseo Thanks for reporting!
Currently, we use lintr::lint() to diagnose the whole Rmd document. It handles code chunks internally (https://github.com/jimhester/lintr/blob/master/R/extract.R). If we keep it this way, then we probably need lintr to fix its code chunk detection. Would you like to also raise an issue at https://github.com/jimhester/lintr/issues?
Looks like there's a notable gap between how knitr and lintr handles rmarkdown code chunks.
```{r}
x <- rnorm(1000)+1
y <- rnorm(1000)+2
```
```{R}
x <- rnorm(1000)+1
y <- rnorm(1000)+2
```
In the above example, knitr renders both but lintr only checks the first chunk.
@renkun-ken -- Since I don't use {lintr} separately, I am not sure how I could best elaborate this issue in their repo concisely. Would you mind moving this issue there?
I've already raised the linked issue above so there's no need for you to file a new one.
@renkun-ken, thank you! Hope that it could be addressed on their side soon.