ChimeHQ / Neon

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

'staleContent' error in highlighter #23

Closed kaunteya closed 1 year ago

kaunteya commented 1 year ago

I am getting an error while updating the font size of textview

self.textView.font = .systemFont(ofSize: size, weight: .regular)

The error says failed to get tokens: staleContent


Tried debugging and noticed that the issue does not occur in the Example project. While comparing the stack side by side by putting breakpoints on the line which prints error, it seems like the guard comparison fails on the following line

https://github.com/ChimeHQ/Neon/blob/4bc0fe43c46e2c08cf522d5db86666086b6e641e/Sources/TreeSitterClient/TreeSitterClient.swift#L350

In debugger startedVersion = 2 and self.version = 3. What could be the probable reason? The code works fine despite the error

kaunteya commented 1 year ago

Will it make sense to call treeSitterClient.willChangeContent and treeSitterClient.didChangeContent only when edited mask contains editedCharacters?

func textStorage(_ textStorage: NSTextStorage, willProcessEditing editedMask: NSTextStorageEditActions, range editedRange: NSRange, changeInLength delta: Int) {
    if editedMask.contains(.editedCharacters) {
        treeSitterClient.willChangeContent(in: editedRange)
    }
}

func textStorage(_ textStorage: NSTextStorage, didProcessEditing editedMask: NSTextStorageEditActions, range editedRange: NSRange, changeInLength delta: Int) {
    guard editedMask.contains(.editedCharacters) else { return }
    let adjustedRange = NSRange(location: editedRange.location, length: editedRange.length - delta)
    let string = textStorage.string

    highlighter.didChangeContent(in: adjustedRange, delta: delta)
    treeSitterClient.didChangeContent(to: string, in: adjustedRange, delta: delta, limit: string.utf16.count)
}
mattmassicotte commented 1 year ago

I think it is safe to ignore edits if they only contain attribute changes. But, I have experimented with this in the past and have run into issues - I just cannot remember why...