-
-
Notifications
You must be signed in to change notification settings - Fork 153
Description
I use RichTextKit for syntax highlighting where the NSAttributedString needs to be updated frequently (e.g., on each keystroke). The current setAttributedString implementation causes the text selection to be lost, with the cursor jumping to the end of the text after every keystroke. This made the editor unusable.
This appears to be a recent regression as this behavior didn't occur in previous versions. I haven't tracked down which update broke it.
(Granted, resetting the NSAttributedString on every keystroke may not have been a foreseen use case - but the system is more than performant enough for it. It's completely smooth. And it used to work)
To continue supporting syntax highlighting I have this workaround, which saves & restores the selection when setting the NSAttributedString:
extension RichTextContext {
func setAttributedStringKeepingSelection(to string: NSMutableAttributedString) {
let oldRange = selectedRange
actionPublisher.send(.setAttributedString(string))
actionPublisher.send(.selectRange(oldRange))
}
}
It works perfectly - typing and syntax highlighting are smooth again. I suggest building that functionality into the base case.
note: it did not work to simply save/restore the selectedRange in my App code, before/after calling setAttributedString
, the action publishing system created timing issues which overrode my setting of the selectedRange