fix: auto-uppercase SQL keywords crash on space key (#667)#669
Merged
fix: auto-uppercase SQL keywords crash on space key (#667)#669
Conversation
Root cause: uppercaseKeywordIfNeeded called textStorage.replaceCharacters inside the didReplaceContentsIn delegate callback (mid beginEditing/endEditing), causing re-entrant NSTextStorage mutation crash. Second attempt using textView.replaceCharacters crashed in CEUndoManager.inverseMutation. Fix: defer mutation to DispatchQueue.main.async with direct textStorage manipulation using NSAttributedString (preserves typingAttributes). Validates range and word content after deferral for IME safety. Also: - Extract pure logic into KeywordUppercaseHelper for testability - Add $$ dollar-quoting detection (PostgreSQL) - Add # line comment detection (MySQL) - Remove duplicate DEFAULT and IF from SQLKeywords - Add 46 unit tests covering all edge cases
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
Fix crash when typing space after lowercase SQL keyword with "Auto-uppercase keywords" enabled.
Closes #667
Root Cause
uppercaseKeywordIfNeededwas called inside thedidReplaceContentsIndelegate callback, which fires betweentextStorage.beginEditing()andendEditing(). Any text mutation inside this callback causes a re-entrant NSTextStorage crash.First fix attempt using
textView.replaceCharacters(public API) also crashed becauseCEUndoManager.registerMutationcallsinverseMutationwhich asserts during the deferred edit.Fix
Defer the uppercase mutation to
DispatchQueue.main.async(runs afterendEditingcompletes), then mutatetextStoragedirectly withNSAttributedString(preservestypingAttributesfor correct font/color). SkipsCEUndoManagersince auto-formatting is not a user edit.Validates range and word content after deferral to handle IME composition and concurrent edits safely.
Additional Fixes
KeywordUppercaseHelper(testable, reusable)$$dollar-quoting detection in protected context scanner#line comment detection in protected context scannerDEFAULTandIFentries inSQLKeywordsTest Coverage (46 tests)
isWordBoundary: 9 tests (all delimiter types + negative cases)isWordCharacter: 5 tests (letters, digits, underscore, special chars)isInsideProtectedContext: 14 tests (quotes, comments, dollar-quoting, escapes, edge cases)keywordBeforePosition: 18 tests (keyword detection, protected contexts, identifiers, all major SQL keywords)Test plan
selectwith auto-uppercase enabled: no crash, uppercased toSELECTSELECT: no change (already uppercase)'select: NOT uppercased (inside string)-- select: NOT uppercased (inside comment)$$ select: NOT uppercased (inside dollar-quote)# select: NOT uppercased (inside hash comment)select_count: NOT uppercased (identifier, not keyword)