CodeEditApp / CodeEditSourceEditor

A code editor view written in Swift powered by tree-sitter.
https://codeeditapp.github.io/CodeEditSourceEditor/documentation/codeeditsourceeditor
MIT License
511 stars 78 forks source link

Incremental Highlighting #59

Closed thecoolwinter closed 1 year ago

thecoolwinter commented 1 year ago

Description

This PR adds incremental highlighting to the text editor. When an edit occurs, it applies the edit in tree-sitter and calculates what other indices may need re-highlighting. This lets us take full advantage of tree-sitter and make our editor much more efficient for large code files.

Todo

Related Issues

27 🚀 Performance: Incremental Highlighting

45

55

21

Checklist

thecoolwinter commented 1 year ago

This isn't quite ready for review, I've noticed that opening files leaks ~30-50Mb of memory and want to make sure it's not due to this code. The app will also allocate massive amounts of memory when files are opened, and it scales linearly with the size of the file. Opening

austincondiff commented 1 year ago

@thecoolwinter if the memory leak is coming from STTextView, can this be reviewed and merged?

thecoolwinter commented 1 year ago

@austincondiff working with Martin it looks like I was mistaken about there being a mem leak. It turns out the textView.addAttributes method takes longer with longer texts, and that was a red herring when combined with the memory usage of the editor. Looking at other syntax editors it looks like the memory usage is normal and we can ignore it as it does get released quickly soon after it's allocated.

What I'm struggling with now is finding a way to apply attributes that doesn't scale with document length and is fast enough to let us scroll the editor smoothly while applying edits. Tree-Sitter can absolutely keep up with scrolling, it's just TextKit that's slowing it all down right now.

That being said it's perfectly functional for smaller documents.

(I've also got to fix those lint errors)

thecoolwinter commented 1 year ago

This PR is ready for review. I replaced textView.addAttributes by modifying the textStorage object directly. When measuring highlight speed on a package-lock.json file this led to a speedup from ~100ms to ~10ms for each new line highlighted when scrolling. This is still not the best performance, but feels just fine for editing and can be improved in the future.

austincondiff commented 1 year ago

A 10x performance increase, not bad! 👏

I will review this soon.