Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

setAttributedString causes loss of selection/cursor position #230

Open
@S1D1T1

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        AltStyle によって変換されたページ (->オリジナル) /