ChimeHQ / Neon

A Swift library for efficient, flexible content-based text styling
BSD 3-Clause "New" or "Revised" License
325 stars 18 forks source link

Crash on Updating Text from updateNSView(_ view: NSTextView, context: Context) #44

Closed varkrishna closed 6 months ago

varkrishna commented 6 months ago

I am using NSTextView in SwiftUI and using TextViewHighlighter for Syntax Highlighting. It works well if we dont update the text from func updateNSView(_ view: JSONEditor, context: Context) but if we do then I am getting a crash everytime Thread 1: Assertion failed: range must not exceed limit in RangeMutation at line number 24 (assert(range.max <= l, "range must not exceed limit"))

I am pasting the screenshot of call stack

Screenshot 2024-05-05 at 7 15 29 PM Screenshot 2024-05-05 at 7 20 14 PM

Just let me know if you need anything else from my end to fix this or any idea how to fix this would be a great help.

mattmassicotte commented 6 months ago

Thank you for reporting this! As a very short-term fix, I think you may be able to work around this with a two-step operation:

view.text = ""
view.text = text

I'm going to try to set up a test so I can figure out what's going on.

Update: Nope, that fix will not work. I think I undesrtand what's wrong. But I have to think harder about what a solution could be. I'm travelling right now, but will get back to you later in the week.

varkrishna commented 6 months ago

Hi @mattmassicotte , Thanks for quick reply, really appreciate. Is it possible to explain what's going on, may be I will change my approach till the time this is fixed permanently.

mattmassicotte commented 6 months ago

The issue is RangeProcessor.didChangeContent has a serious bug. This crash will happen if a range is ever changed that is past its maximum processed location. I think this situation can only occur with programmatic text mutation, like what you are doing here. But, I wouldn't be surprised if other paths could lead to this.

To avoid this, you'd have to make sure that the entire content was always processed. And TextViewHighlighter doesn't provide any good API to achieve that. Perhaps by also programmically scrolling to the end first? It's hacky and I don't know if it will work reliably.

varkrishna commented 6 months ago

Yes, you are right. Programmatically scrolling to bottom to make sure that whole text is processed, preventing the crash. Thanks.

Also , if I have a check like below before updating the text, directly updating the text still leading to crash.

func updateNSView(_ view: JSONEditor, context: Context) { view.updateFont(newValue: font) if view.text != text { view.text = text } }

mattmassicotte commented 6 months ago

I think I have this fixed now. Could you please update to the latest main and try again? And thanks so much for your patience here.

varkrishna commented 6 months ago

Yes @mattmassicotte , the crash I was getting is fixed now after updating to latest code. Thanks a lot.

mattmassicotte commented 6 months ago

Wonderful! Thanks again for filing this issue.