Skip to content

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

Closed

Description

@cderv

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

Activity

  1. moved this to Need External Work in R Markdown Team Projectson Nov 12, 2021
  2. ronblum commented on Nov 12, 2021

    @ronblum
    Contributor

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

  3. kevinushey commented on Nov 12, 2021

    @kevinushey
    Collaborator

    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:

    // Embed R highlight rules
    Utils.embedRules(
    this,
    RHighlightRules,
    "r",
    this.$reChunkStartString,
    this.$reChunkEndString,
    ["start", "listblock", "allowBlock"]
    );

    And the embedRules() helper lives here:

    this.embedRules = function(HighlightRules, EmbedRules,
    prefix, reStart, reEnd,
    startStates, endState)
    {
    if (typeof startStates === "undefined")
    startStates = ["start"];
    if (typeof endState === "undefined")
    endState = "start";
    startStates = that.asArray(startStates);
    var rules = HighlightRules.$rules;
    // some highlight modes (notably, YAML) can have rules
    // that manipulate a stack of saved states -- in the
    // context of YAML, this is done to handle e.g. multiline
    // strings (tracking the indent used for those strings).
    // we need to clear that stack whenever we switch from one
    // mode to another.
    var onMatch = function(value, state, stack, line) {
    stack.splice(0);
    return this.token;
    }
    // define the highlight rules that allow us to transition
    // into the embedded mode
    for (var i = 0; i < startStates.length; i++) {
    rules[startStates[i]].unshift({
    token: "support.function.codebegin",
    regex: reStart,
    onMatch: onMatch,
    next: prefix + "-start"
    });
    }
    // call into Ace to embed rules with given 'prefix', and
    // define the rule that's used to return to parent mode
    HighlightRules.embedRules(EmbedRules, prefix + "-", [{
    token: "support.function.codeend",
    regex: reEnd,
    onMatch: onMatch,
    next: "start"
    }]);
    };

    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.

  4. 17 remaining items

  5. modified the milestones: Backlog-1, Later on Sep 26, 2023
  6. bwu62 commented on Jun 24, 2024

    @bwu62

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

  7. removed this from the Later milestone on Jul 2, 2024
  8. kevinushey commented on Jul 2, 2024

    @kevinushey
    Collaborator

    Moving back to triage due to amount of user interest.

  9. rje42 commented on Jul 3, 2024

    @rje42

    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!

  10. ronblum commented on Jul 9, 2024

    @ronblum
    Contributor

    @rje42 What version of RStudio are you using?

  11. added this to the Kousa Dogwood milestone on Jul 9, 2024
  12. rje42 commented on Jul 10, 2024

    @rje42

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

  13. self-assigned this
    on Sep 12, 2024
  14. gtritchie commented on Sep 18, 2024

    @gtritchie
    Member

    Verified in build 2024.10.0-186 on Mac. The expected region is now highlighted.

    I opened a separate issue on code folding quirks in this scenario: #15191

  15. moved this from Need External Work to Done in R Markdown Team Projectson Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions