diff --git a/Sources/MarkdownEngine/TextView/Coordinator/NativeTextViewCoordinator+TextDelegate.swift b/Sources/MarkdownEngine/TextView/Coordinator/NativeTextViewCoordinator+TextDelegate.swift index dfd5855..5a68e32 100644 --- a/Sources/MarkdownEngine/TextView/Coordinator/NativeTextViewCoordinator+TextDelegate.swift +++ b/Sources/MarkdownEngine/TextView/Coordinator/NativeTextViewCoordinator+TextDelegate.swift @@ -299,8 +299,10 @@ extension NativeTextViewCoordinator { self.previousActiveTokenIndices = self.activeTokenIndices self.previousCaretLocation = caretLoc - // Track code blocks for button overlay (reuse tokens) - updateCodeBlockSelection(textView: tv, tokens: tokens) + // Skip during a pending edit — viewRect is stale until textDidChange's restyle runs; otherwise the overlay flashes to the old Y before settling. + if !shouldSkipSelectionRestyle { + updateCodeBlockSelection(textView: tv, tokens: tokens) + } } public func textView(_ textView: NSTextView, shouldChangeTextIn affectedCharRange: NSRange, replacementString: String?) -> Bool { diff --git a/Sources/MarkdownEngine/TextView/NativeTextView/NativeTextView.swift b/Sources/MarkdownEngine/TextView/NativeTextView/NativeTextView.swift index 4ab2978..59ad8da 100644 --- a/Sources/MarkdownEngine/TextView/NativeTextView/NativeTextView.swift +++ b/Sources/MarkdownEngine/TextView/NativeTextView/NativeTextView.swift @@ -58,7 +58,7 @@ final class NativeTextView: NSTextView { } } - // AppKit doesn't fire textDidChange for setMarkedText mutations, so Apple's inline-prediction inserts the completion with base typingAttributes and heading lines flicker to body font; restyle the paragraph here to reapply heading font. + // AppKit skips textDidChange for setMarkedText, so markdown attrs (heading font, code-block bg) don't reach the marked range — restyle the affected paragraph to fix it. override func setMarkedText(_ string: Any, selectedRange: NSRange, replacementRange: NSRange) { super.setMarkedText(string, selectedRange: selectedRange, replacementRange: replacementRange) guard hasMarkedText(), @@ -67,8 +67,6 @@ final class NativeTextView: NSTextView { guard marked.location != NSNotFound, marked.length > 0 else { return } let nsText = self.string as NSString let paragraph = nsText.paragraphRange(for: marked) - let line = nsText.substring(with: nsText.lineRange(for: NSRange(location: paragraph.location, length: 0))) - guard line.hasPrefix("#") else { return } coord.restyleParagraphs([paragraph], in: self) }