Skip to content

fix: highlight nested R Markdown chunks through the outer fence - #1947

Merged
eitsupi merged 1 commit into
REditorSupport:mainfrom
mvanhorn:fix/1500-rmd-nested-chunk-highlight
Nov 14, 2026
Merged

eitsupi merged 1 commit into
REditorSupport:mainfrom
mvanhorn:fix/1500-rmd-nested-chunk-highlight

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

Motivation

In an R Markdown document, highlighting of a code chunk stops at the first closing fence, even when that fence only ends a shorter example nested inside a longer chunk. The remainder of the outer chunk is then colored as ordinary text.

The chunk scanner treated every line of three or more backticks as the end of the open chunk. It did not record the length of the opening fence, so a shorter inner fence matched the same end pattern as the outer one.

Summary

The scanner now stores the opening fence length and closes the chunk only when a later fence is at least that long. A shorter fence stays inside the outer chunk as content. A fence that meets the length still ends the chunk, and a following chunk is parsed on its own. Added cases cover a longer outer fence around a shorter example, a longer fence before the next chunk, and info strings, bare fences, and unclosed chunks.

Testing

  • Ran pnpm run format:check locally on macOS for the vscode-R isChunkStartLine change; it passed.
  • pnpm run format:check reported that all matched files use the correct format.
  • js_tests did not run the chunk suite.
  • Ran pnpm run format:check in a Linux container (localhost/osc-linux-node:22) for the vscode-R isChunkStartLine change; it passed.
  • A run with the vscode-R isChunkStartLine change reverted gave no clear result, so no test is shown to catch this bug.
  • The changed spec src/test/suite/chunks.test.ts did not run here: no test runner for it was available.

Closes #1500

The scanner now stores the opening fence length and closes the chunk
only when a later fence is at least that long. A shorter fence stays
inside the outer chunk as content. A fence that meets the length still
ends the chunk, and a following chunk is parsed on its own. Added cases
cover a longer outer fence around a shorter example, a longer fence
before the next chunk, and info strings, bare fences, and unclosed
chunks.

Fixes REditorSupport#1500
@eitsupi
eitsupi merged commit d7eaa67 into REditorSupport:main Nov 14, 2026
16 of 17 checks passed
@mvanhorn

Copy link
Copy Markdown
Contributor Author

Appreciate the merge @eitsupi! Nested R Markdown examples should now stay highlighted through the longer outer fence instead of stopping at the first inner closing fence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bad highlighting of nested code chunks in R Markdown documents

2 participants