rstudio / rstudio

RStudio is an integrated development environment (IDE) for R
https://posit.co/products/open-source/rstudio/
Other
4.65k stars 1.09k forks source link

nested fenced code block messes up source highlighting in the editor #10079

Open cderv opened 2 years ago

cderv commented 2 years ago

System details

RStudio Edition : Desktop [Open Source]
RStudio Version : 2022.1.0.199
OS Version      : Windows 10 x64 (build 19043)
R Version       : R version 4.1.1 (2021-08-10)

Steps to reproduce the problem

Open a new rmarkdown document and paste the following content

---
title: "Test"
output: html_document
---

# A Header

````{verbatim}
We can output arbitrary content verbatim.

```{r}
1 + 1

The content can contain inline code like r pi * 5^2, too.


# Another header 

Some content

Describe the problem in detail

The source editor will not correctly recognized the nested chunk and it will mess up the highlighting image

Example with the code chunk not being correctly highlighted, and headers not correctly found after the chunk.

Describe the behavior you expected

This syntax is something rather new in knitr. Last version support nested chunks and some engine will benefit from this like the comment engine or the verbatim engine.

WIth the current IDE behavior, it is rather hard to write and read such content.

I would expect the highlighting of the Markdown document in the IDE to be the same as without nested chunk.

cc @yihui

ronblum commented 2 years ago

Reproducible with older versions of RStudio as well, going back to RStudio Desktop 1.2.5042 on MacOS 12.

kevinushey commented 2 years ago

I believe supporting this will require a few changes.

First, the chunk highlighter would need to become stateful, and track the number of backticks used in each chunk header / footer. That implementation lives here:

https://github.com/rstudio/rstudio/blob/main/src/gwt/src/org/rstudio/studio/client/workbench/views/source/editors/text/ace/AceBackgroundHighlighter.java

Second, the Ace highlight rules would need to become stateful as well. Those rules are activated here:

https://github.com/rstudio/rstudio/blob/686bc16e95ead41172a6b5ab9726a12007514c16/src/gwt/acesupport/acemode/rmarkdown_highlight_rules.js#L58-L66

And the embedRules() helper lives here:

https://github.com/rstudio/rstudio/blob/686bc16e95ead41172a6b5ab9726a12007514c16/src/gwt/acesupport/acemode/utils.js#L98-L143

The onMatch() rule would need to both track and check the number of backticks that were matched, and only switch states when the "correct" closing number of backticks is seen.

piiskop commented 1 year ago

It is 2023 and i have just reported the same bug.

ronblum commented 1 year ago

This problem keeps popping up, including in #10703, #12788, #13384. Let's consolidate it in this issue.

Triage: Consider whether this should be higher than backlog-2, since it keeps coming up.

bwu62 commented 2 months ago

Also encountering this issue myself. Commenting here to bump and to subscribe to updates. Hope someone posts a resolution soon.

kevinushey commented 2 months ago

Moving back to triage due to amount of user interest.

rje42 commented 2 months ago

I have encountered the same phenomenon with TikZ blocks (not even nested chunks). See the image.

Screenshot 2024-07-03 at 09 25 24

Any fix would be greatly appreciated!

ronblum commented 2 months ago

@rje42 What version of RStudio are you using?

rje42 commented 2 months ago

@ronblum the lastest one just downloaded: Version 2024.04.2+764. It still occurs!