Repository navigation
fix C++ chunk indentation in R Markdown documents - #18470
Conversation
TokenUtils tokenized rows without threading a tokenizer context, so onMatch handlers received a fresh empty object on every call; the chunk-end highlight rule then dereferenced the missing chunk info and threw, so pressing Enter in a C++ chunk raised an uncaught TypeError and skipped auto-indent whenever a completed chunk appeared earlier in the document. Thread a per-row context through TokenUtils (mirroring RCodeModel), and guard the context reads in the chunk, quarto, YAML multiline, and Sexpr rules so context-less tokenization degrades gracefully instead of crashing. Also make the chunk/quarto/yaml openers store fresh context objects (mutating an inherited object corrupts the contexts saved for previously-tokenized rows), and store the Sexpr brace count as primitive context fields so per-row context snapshots capture it. Addresses #18468
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
RStudio E2E - all platformsAll tests passed!
View / download the full reportView report online -- opens in the browser, no download needed. Available for 14 days. Download playwright-report.zip -- extract and open
All platforms - commit |
Code review findingsActionable items from an automated multi-agent review. The context-threading fix in Important
Suggestions
|
- derive the missing-context fallback states from the current state name, so guards no longer escape embed prefixes (e.g. 'r-quarto-yaml-start' now falls back to 'r-start' rather than top-level 'start'); the markdown chunk-end guard also recovers the fence width from 'github-block-<N>' - thread a tokenizer context through Tokenizer.java's doTokenize, fixing a TypeError raised when reformatting R code with quarto '#|' lines - warn (once) on the console when a missing-context guard fires, so context-threading regressions stay observable - add src/gwt/test/context_test_rmd.html covering the C++ chunk indent, quarto '#|' and yaml multiline snapshot corruption, \Sexpr brace counts, and the new fallbacks; fails against the pre-fix tokenizer - e2e: resolve the active editor via the empty marker, assert the exact indent and the r-cpp tokenizer state, and clarify index-base comments - clarify which components hold the per-row context snapshots, and soften the fresh-object comments where inherited mutation was unreachable
|
All 13 items verified and addressed in 8748e17. One deviation on item 3: |
TokenUtils.$tokenizeUpToRow (factored out on main in #18470) had the same two defects this branch fixed in RCodeModel: a shallow context copy, and an assumeGood resume keyed only on the end state. Adopt cloneContext / equalContexts there too, and drop the now-stale comment describing the limitation as shared with RCodeModel and the BackgroundTokenizer.
* invalidate following rows when tokenizer context changes The Ace background tokenizer re-tokenizes the row following an edited row only when the edited row's end state changes. Tokenizer contexts (carrying e.g. rainbow paren / fenced div nesting depth, YAML multiline-string indents, and chunk fence widths) also flow into following rows, but context changes did not propagate invalidation, so an edit that changed only context data left stale highlighting on every row below. Modes with a code-model scope tree masked this by force-retokenizing the document after every edit; plain Markdown and YAML documents showed the stale highlighting indefinitely. Updates the ace builds with rstudio/rstudio-ace#13, which compares each row's newly-computed context against the stored one and invalidates the following row when they differ. The rebuilt artifact also drops a stray (hand-patched) comment left from the #13605 investigation. Addresses #18472 * use structural context comparison instead of JSON.stringify Updates the ace builds with the second commit of rstudio/rstudio-ace#13: contexts are now deep-copied at the row boundary, so each row's stored context is an independent snapshot that in-place mutation cannot corrupt, and compared with a structural equality helper that is key-order independent, never throws, and depth-caps recursion. * use structuredClone for context snapshots Updates the ace builds with the third commit of rstudio/rstudio-ace#13: contexts are snapshotted with the platform deep clone, supporting nested objects and arrays as context values. * harden tokenizer context helpers against non-plain data Updates the ace builds with the latest rstudio/rstudio-ace#13: contexts holding a Date, Map, Set, RegExp, or class instance now compare unequal (Object.keys sees no content on them, so the structural comparison would have reported any two as equal -- a silently missed invalidation), and cloneContext skips the structuredClone for missing and empty contexts, the common case for modes that never touch their context. * add markdown fenced-div regression test; strengthen yaml test Review feedback: the rainbow fenced div symptom from #18472 was untested (and the cleanup list already claimed a syntax_highlight.md file), the yaml test's final assertion checked only the first token's type, its off-screen premise was implicit, and the target-editor lookup went through the marker DOM walk. The new markdown test inserts a closing fence at the top of a tall plain .md document and asserts an off-screen block's tokens cycle to the next rainbow color -- a positive assertion, where the yaml test's is an absence. Both context tests now pin exact token type/value pairs, assert the target row sits below the last rendered row before editing (the guard the tests' validity rests on), resolve the active editor directly, and give the initial tokenization poll the same 15s budget as the post-edit one. Both fail against the previous ace build and pass with this one. Addresses #18472. * adopt context helpers in RCodeModel's token cache Review feedback, folded in from the proposed follow-up: RCodeModel.$tokenizeUpToRow kept both halves of the bug fixed for ace's BackgroundTokenizer -- the shallow Object.assign copy let highlight rules mutate the previous row's stored context through shared nested objects, and the assumeGood short-circuit keyed only on the end state, so a context-only change stopped invalidation and following rows kept stale cached tokens (feeding indentation, folding, and the scope tree). Use the cloneContext / equalContexts helpers exported from ace/background_tokenizer, so both incremental caches share the same snapshot and comparison semantics. Addresses #18472. * adopt context helpers in TokenUtils' token cache TokenUtils.$tokenizeUpToRow (factored out on main in #18470) had the same two defects this branch fixed in RCodeModel: a shallow context copy, and an assumeGood resume keyed only on the end state. Adopt cloneContext / equalContexts there too, and drop the now-stale comment describing the limitation as shared with RCodeModel and the BackgroundTokenizer.
* Resolve e2e daily builds per branch, not via /rstudio/latest The e2e workflows installed the build named by dailies.rstudio.com/rstudio/latest/index.json while running the tests from the checked-out branch. Those are two different branches whenever the alias lags a rollover, and its index.json does lag: after the 2026.08 branch cut the /rstudio/latest/ page served Autumn Hawkbit (2026.09) while /rstudio/latest/index.json still named a Yellow Yarrow (2026.08) build. CI reads the JSON, so the scheduled rotation installed a 2026.08 binary and ran main's tests against it. Every test covering behavior added after the cut failed on every platform, with nothing wrong in either the test or the product -- #18454 and #18470 both landed that way. Resolve the manifest from version/RELEASE instead. The new os-resolve-daily-manifest action turns it into the "flower" path segment the same way docker/jenkins/publish-build.sh does when it writes the manifest, so the reader and the writer stay in step, and a manifest named after its own branch cannot be mis-pointed by an alias. It computes a URL and makes no network request, so it is safe in the jobs that end up using an override URL instead. A missing platform key now fails with the branch and key named, and says a source build is the way to test a branch with no daily. Autumn Hawkbit has no Windows build yet, so the Windows engines take that path until one is published; installing a 2026.08 .exe was never a valid substitute. Addresses #18498 * Trigger PR e2e on resolver and version/RELEASE changes The pull_request paths filter lists every composite action these workflows use, but the new os-resolve-daily-manifest was missing, so a PR touching only the resolver would not run the workflow that exercises it. version/RELEASE joins it for the same reason one step further out: it selects which branch's daily each platform resolves, so a change there changes the binary under test. A flower with no daily yet falls through decide() to a source build, so such a PR proves the transition works instead of leaving it to the next cron rotation. Addresses #18498
Summary
Pressing Enter inside a C++ (e.g. Rcpp) chunk of an R Markdown or Quarto document raised an uncaught exception and skipped auto-indent whenever another completed chunk appeared earlier in the document:
TokenUtils.$tokenizeUpToRow(used byCppCodeModelfor C++ chunk indentation) calledgetLineTokens(line, state, row)without threading a tokenizer context, so everyonMatchhandler received a fresh empty object; the chunk-end highlight rule then dereferenced the missing chunk information unguarded. Broken since eafc5fa (September 2024), which introduced context-based chunk tracking.Changes
token_utils.js: thread a per-row context through$tokenizeUpToRow, mirroringRCodeModel(including the$invalidateRow/$insertNewRows/$removeRowsbookkeeping).utils.js,markdown_highlight_rules.js: guard the chunk-end rules so that a missing chunk context degrades to "this fence closes the chunk" (the pre-context behavior) instead of crashing; same for the quarto#|exit rules and the YAML multiline indent checks. The fallbacks derive their target from the current state name, so embedded modes (e.g.r-quarto-yaml-*) exit to the enclosing mode rather than escaping to top-levelstart(a dynamically assignedthis.nextis not prefix-rewritten by Ace'saddRules), the markdown fence guard recovers the fence width from thegithub-block-<N>state name, and each guard warns once on the console when it fires so context-threading regressions stay observable.Tokenizer.java: thread a single tokenizer context throughdoTokenize(used by Reformat Code), fixing a pre-existingTypeErrorraised when reformatting R code containing Quarto#|comment lines.TokenUtils.$tokenizeUpToRowandBackgroundTokenizer.$tokenizeRoware shallow copies, so in-place mutation retroactively corrupts the snapshots saved for earlier rows), and the Sweave\Sexpr{}brace count is now stored as primitive context fields so per-row snapshots capture it.Testing
{Rcpp}chunk below a completed R chunk indents the new line (asserting the exact indent and ther-cpp-tokenizer state); the e2e fixture fails any test raising a client exception, so this also guards the TypeError directly. The test fails without the fix (exception raised, no indent).src/gwt/test/context_test_rmd.htmltestpage (run byant testpage) pins the chunk / quarto#|/ YAML multiline /\Sexprcontext fixes at the tokenizer level, including the missing-context fallbacks; it fails against the pre-fix code.Fixes #18468.