languageserver icon indicating copy to clipboard operation
languageserver copied to clipboard

Document symbol display in Rmd is not reliable

Open jooyoungseo opened this issue 4 years ago • 13 comments
trafficstars

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

  1. Download this Rmd file
  2. Open the file in VSCode.
  3. Press Ctrl+Shift+. (period).

Case 2: Document Symbol Does Not Appear

  1. Download another Rmd file
  2. Open the file in VSCode.
  3. 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

jooyoungseo avatar Apr 23 '21 16:04 jooyoungseo

For case 2, if you turn on diagnostics, you will notice a syntax error at line 101-102 as the following:

image

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>))
```

renkun-ken avatar Apr 24 '21 09:04 renkun-ken

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?

screenshot

jooyoungseo avatar Apr 24 '21 11:04 jooyoungseo

screenshot2

jooyoungseo avatar Apr 24 '21 11:04 jooyoungseo

You may want to update your lintr to latest development version and then you should be able to see the parse error.

renkun-ken avatar Apr 24 '21 11:04 renkun-ken

Thanks! It works now.

jooyoungseo avatar Apr 24 '21 12:04 jooyoungseo

I guess we might want to handle the case that eval = false or error = TRUE.

randy3k avatar Apr 24 '21 15:04 randy3k

@randy3k, that would be greatly appreciated. I'll re-open this issue.

jooyoungseo avatar Apr 25 '21 14:04 jooyoungseo

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 avatar May 08 '21 18:05 jooyoungseo

@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?

renkun-ken avatar May 09 '21 02:05 renkun-ken

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 avatar May 09 '21 04:05 renkun-ken

@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?

jooyoungseo avatar May 10 '21 12:05 jooyoungseo

I've already raised the linked issue above so there's no need for you to file a new one.

renkun-ken avatar May 10 '21 13:05 renkun-ken

@renkun-ken, thank you! Hope that it could be addressed on their side soon.

jooyoungseo avatar May 10 '21 18:05 jooyoungseo