Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setAttributedString causes loss of selection/cursor position #230

Open
S1D1T1 opened this issue Feb 19, 2025 · 0 comments
Open

setAttributedString causes loss of selection/cursor position #230

S1D1T1 opened this issue Feb 19, 2025 · 0 comments

Comments

@S1D1T1
Copy link

S1D1T1 commented Feb 19, 2025

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

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

No branches or pull requests

1 participant