Skip to content

fix(editor): re-highlight the document after a programmatic text replacement (#1612)#1615

Closed
datlechin wants to merge 1 commit into
mainfrom
fix/1612-highlight-after-format
Closed

fix(editor): re-highlight the document after a programmatic text replacement (#1612)#1615
datlechin wants to merge 1 commit into
mainfrom
fix/1612-highlight-after-format

Conversation

@datlechin
Copy link
Copy Markdown
Member

Fixes #1612.

Problem

Clicking Format replaced the query text and all syntax highlighting disappeared, never recovering. With #1611 merged, Format flows exclusively through the binding push-down (syncBindingText -> setText), which made the loss deterministic.

Root cause, two layers

  1. syncBindingText called the TextView-level textView.setText(...), bypassing TextViewController.setText(_:), which is the documented controller-level path and rebuilds the highlighter for the replacement storage. The old highlighter kept tree-sitter state for the dead document.
  2. Rebuilding alone is not enough: a fresh Highlighter only queries when triggered (an edit, a frame/bounds notification, or invalidate()). Initial load works because the first layout pass fires frameDidChangeNotification; after a mid-session storage swap no trigger is guaranteed, so the rebuilt highlighter sat idle and the document stayed plain.

Fix

  • syncBindingText routes through controller.setText(newValue) so the highlighter is rebuilt for the new storage.
  • setUpHighlighter() now ends with highlighter.invalidate(), making "after setup, the editor is highlighted" an invariant for every caller instead of a timing accident.

Tests

  • test_syncRebuildsHighlighterForReplacementStorage: the highlighter instance is rebuilt on a binding push-down.
  • test_syncQueriesHighlightsForReplacementStorage: a mock provider proves the rebuilt highlighter is re-set-up AND re-queried; this test fails with the route-through change alone.
  • All existing binding-sync and highlighter tests pass.

Note

The issue also asks for a minify counterpart to Format; that is a feature request and should be tracked separately.

@datlechin
Copy link
Copy Markdown
Member Author

Folded into #1616, which now targets main and carries both the fix and the refactor.

@datlechin datlechin closed this Jun 7, 2026
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.

Code highlighting is lost after the code is formatted

1 participant