fix: prevent syntax highlighting from disappearing after initial display#562
Merged
fix: prevent syntax highlighting from disappearing after initial display#562
Conversation
…lay (#556) The root cause was a cascading re-highlight cycle during makeNSView: 1. textView.string = text fired textDidChange (delegate was already set) 2. textDidChange wrote parent.text → updateContent → cachedHighlightResult = nil, contentVersion++ 3. Synchronous highlight applied colors successfully 4. SwiftUI re-render → updateNSView → updateContentIfNeeded saw textChanged = true (from contentVersion bump) AND languageChanged = true (lastLanguage was "") 5. textView.string = text (same text!) stripped all highlight attributes 6. Re-highlight attempted but could fail or be delayed Three fixes: - Defer textView.delegate assignment in makeNSView until AFTER text and highlighting are configured, preventing textDidChange from firing during setup - Initialize Coordinator.lastLanguage/lastFileName from parent in init, eliminating the false languageChanged detection on first updateNSView - Guard textView.string = text with textView.string != text to avoid stripping attributes when contentVersion bumps but text is identical - Skip cached highlight result when language changed (stale grammar matches)
Contributor
✅ Code Coverage: 73.3%Threshold: 70% Coverage is above the minimum threshold. Generated by CI — see job summary for detailed file-level breakdown. |
This was referenced Mar 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #556 — syntax highlighting shows correctly when a file is first opened, then disappears after a short time.
Root cause: A cascading re-highlight cycle during
makeNSView:textView.string = textfiredtextDidChange(delegate was already set before text assignment)textDidChangewroteparent.text→updateContent→cachedHighlightResult = nil,contentVersion++updateNSView→updateContentIfNeededsawtextChanged = true(from contentVersion bump) ANDlanguageChanged = true(lastLanguagewas"")textView.string = text(same text!) stripped all highlight attributes sinceisRichText = falseFixes applied:
makeNSViewuntil after text and highlighting are configured, preventingtextDidChangefrom firing during initial setuplastLanguage/lastFileNamefrom parent inCoordinator.init, eliminating falselanguageChangedon firstupdateNSViewtextView.string = textwithtextView.string != textto avoid stripping attributes whencontentVersionbumps but text content is identicalTest plan
HighlightPersistenceTests(8 tests) covering:lastLanguage/lastFileNamefrom parentupdateNSViewskips unnecessary update when language matchestextDidChangecancelPendingHighlightincrements generationCodeEditorStaticTests.coordinatorInitialStatefor new init behavior