Repository navigation
invalidate following rows when tokenizer context changes - #18473
Conversation
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
✅ 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 platforms7 test(s) failed
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 |
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.
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.
Code review findingsActionable items from an automated multi-agent review. No correctness defect was found in the fix itself. Invalidation chaining, the Important
Suggestions
Worth confirming before mergeGiven how much of the test's validity rests on the off-screen assumption (finding 9), confirm it is red on |
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.
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.
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.
|
Applied in 578d969, e5fa288, and c688184; the ace artifact was rebuilt from rstudio/rstudio-ace#13, which gained the source changes plus unit tests.
Red/green re-confirmed on desktop with the final tests: against main's ace build both context tests fail with exactly the predicted stale tokens (the yaml row keeps its |
…validation # Conflicts: # e2e/rstudio/tests/panes/editor/syntax-highlighting.test.ts # src/gwt/acesupport/acemode/r_code_model.js
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.
Summary
An edit that changes only tokenizer context data -- the nesting depth tracked for rainbow parentheses / rainbow fenced divs, the indent recorded for a YAML multiline string, chunk fence widths -- left stale syntax highlighting on every row below the edit. The Ace background tokenizer propagated invalidation to following rows only when a row's end state changed, and tokenizer contexts (an rstudio-ace extension) were invisible to that check.
In R / R Markdown / Quarto documents the bug is masked by an unrelated mechanism: modes with a code-model scope tree rebuild the scope tree after every edit, and that path (
TokenIterator.tokenizeUpToRow) force-retokenizes every row from the edit point to the end of the document. Plain Markdown and YAML documents have no such rebuild and showed the stale highlighting indefinitely -- confirmed by inspecting the tokenizer caches: after inserting a fence at the top of a 160-row.mddocument with rainbow fenced divs enabled, 79 of 81 fence rows still held their old colors.Changes
ace.js/ace-uncompressed.jsbuilds with rstudio/rstudio-ace#13:BackgroundTokenizer.$tokenizeRownow snapshots the incoming context withstructuredClone(so contexts can hold nested objects and arrays, and each row's stored context is an independent snapshot that in-place mutation by a highlight rule cannot corrupt), compares the newly-computed context against the stored one with a structural equality helper (key-order independent, never throws, depth-capped), and invalidates the following row when they differ, exactly as for a changed end state. Review hardening: non-plain objects (Date, Map, Set, RegExp, class instances) compare unequal rather than silently equal, the clone is skipped for missing and empty contexts (the common case for modes that never touch their context), and the helpers are unit-tested in the fork. The rebuilt artifact also drops a stray hand-patched comment left from the Source editor randomly becomes inoperable (dysfunctional text select, multi-line editing, & inputs appear in wrong tab) #13605 investigation (now handled in the fork source).|opener changes only the recorded indent (the opener's end state is unchanged), and an off-screen content row must be re-highlighted. Plain Markdown (the symptom reported in edits can leave stale syntax highlighting below the edited line in Markdown and YAML documents #18472): an unclosed fenced div opener inserted at the top of a tall document deepens every following block, and an off-screen block's fence must take the next nesting-depth color (colors are depth-based since color fenced divs by nesting depth #18467) -- a positive assertion, where the YAML test's is an absence. Both tests pin exact token type/value pairs on both sides of the edit, assert the target row sits below the last rendered row before editing (the off-screen premise the tests' validity rests on -- rendered rows are incidentally re-tokenized by the fold gutter'sgetState()sweeps), and fail against the previous ace build while passing with this one.cloneContext/equalContextshelpers exported fromace/background_tokenizerinRCodeModel.$tokenizeUpToRow, whose parallel per-row cache had both halves of the same bug: a shallowObject.assigncontext copy (so a rule mutating a shared nested object retroactively corrupted the previous row's stored context) and anassumeGoodshort-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). Verified against the GWT testpage suite and a code-model e2e regression set.TokenUtils.$tokenizeUpToRow(the parallel cache used bycpp_code_model.js, touched on main by fix C++ chunk indentation in R Markdown documents #18470), which had the identical shallow-copy and end-state-only-resume pair, and removes the comment there describing the limitation as shared with RCodeModel and the BackgroundTokenizer -- no longer true with this change.The rebuilt artifact was diffed against the committed one before applying the fix to confirm a clean baseline, and again after, to confirm the artifact change is exactly the intended tokenizer change.
Merging note: after #18467 landed, the Markdown regression test was adapted to the depth-based color scheme (a closing fence at depth 0 no longer changes anything; the test now inserts an unclosed opener, which deepens -- and so recolors -- every following block). rstudio/rstudio-ace#13 should be merged into the ace fork alongside this PR so the committed artifacts and the fork source stay in sync.
Fixes #18472.