REditorSupport / REditorSupport/languageserver

VSCode outline fails in Rmarkdown document if there is pseudo chunk text inside the chunks

Open
#641 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
675
Forks
118
Avg merge
1d 37m
Merged PRs (30d)
11

Description

I know this is a specific case.
I am building functions inside a Rmarkdown document, which builds chunks.
I remarked that, as soon as there is 3 backticks in my code, the VSCode document outline is not displayed.

Here is a reprex.
With this code, there is no outline available in VSCode, near the Rmd name
image

---
title: "test vscode"
output: html_document
author: statnmap
---

```{r development, include=FALSE}
library(testthat)
```

# combine_tbl_to_file

```{r function}
#' Create R code chunk content
#' @return A character string of R code chunk content
#' @noRd
create_r_chunk <- function() {
  paste(
    "```{r}",
    "```",
    sep = "\n"
  )
}

cat(create_r_chunk())
```

As soon as I remove the paste with backticks, the outline is correct:
image

---
title: "test vscode"
output: html_document
author: statnmap
---

```{r development, include=FALSE}
library(testthat)
```

# combine_tbl_to_file

```{r function}
#' Create R code chunk content
#' @return A character string of R code chunk content
#' @noRd
create_r_chunk <- function() {
  paste(
    "",
    sep = "\n"
  )
}

cat(create_r_chunk())
```

Maybe the parsing you use to detect chunks can be a little more restrictive, and only considering chunks when there is nothing or spaces before the backticks ?

You could maybe use patterns used by {knitr}:

r$> knitr:::all_patterns$md
$chunk.begin
[1] "^[\t >]*```+\\s*\\{([a-zA-Z0-9_]+( *[ ,].*)?)\\}\\s*$"

$chunk.end
[1] "^[\t >]*```+\\s*$"

$ref.chunk
[1] "^\\s*<<(.+)>>\\s*$"

$inline.code
[1] "(?<!(^``))(?<!(\n``))`r[ #]([^`]+)\\s*`"

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the R Markdown example in VSCode and inspect the chunk parsing used for the document outline. Compare its handling of embedded backticks with the knitr patterns shown for chunk.begin and chunk.end; done means the outline remains available when pseudo chunk text appears inside an R chunk.

Written by the indexing model from the issue text.

Assessment

Tech stack
r, vscode
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.