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

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

Closed
varkrishna opened this issue May 5, 2024 · 7 comments

Comments

@varkrishna
Copy link

varkrishna commented May 5, 2024

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.

@varkrishna varkrishna changed the title Crash on Updating Text from updateNSView(_ view: JSONEditor, context: Context) Crash on Updating Text from updateNSView(_ view: NSTextView, context: Context) May 5, 2024
@mattmassicotte
Copy link
Contributor

mattmassicotte commented May 6, 2024

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
Copy link
Author

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
Copy link
Contributor

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
Copy link
Author

varkrishna commented May 7, 2024

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
Copy link
Contributor

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
Copy link
Author

varkrishna commented May 10, 2024

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

@mattmassicotte
Copy link
Contributor

Wonderful! Thanks again for filing this issue.

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

2 participants