Closed varkrishna closed 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.
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.
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.
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 } }
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.
Yes @mattmassicotte , the crash I was getting is fixed now after updating to latest code. Thanks a lot.
Wonderful! Thanks again for filing this issue.
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 everytimeThread 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
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.