Tree-sitter.kak -- A tree-sitter server

Over the past few days I put together tree-sitter.kak, a plugin that keeps a tree-sitter server with parsed ASTs in memory. Structural selections and highlighting are the main motivating examples. This is currently just a proof-of-concept, but it already supports highlighting rust files with minimal performance issues.

I wrote this mostly because I wanted to have tree-sitter handle the syntax highlighting, but I opted for something more general. Here’s a demo of the highlighting functionality:

asciicast

9 Likes

This is pretty neat. I’ve always felt that syntax highlighting should not be a re-implemented feature by Kakoune, but somehow shared for leverage.

Thanks! While this highlighting works, it’s really not a great implementation. A %sh{..} expansion is run on effectively every key press, because it needs to check if the buffer was modified.

I think for structural selections though, this shouldn’t be necessary, as the buffer can just be reparsed when issueing AST queries.

Does it work much worse if you just reparse on InsertIdle rather than InsertChar and InsertCharDelete?

Nice work! It’s fun to me because around a week ago I have started exactly the same thing, named kak-tree-sitter. We have a slightly similar approach but I try to move away from the « static language » — because I do think it would be better that people can compile the server once, and add their runtime files so that updating the grammars / query is a free operation.

I’ll follow up on your advancements. Currently, my little thing is at a different stage. I don’t have the hooks etc. and I’m in a phase of optimizing and benchmarking, but I think we are going to eventually end up with exactly the same thing. Not sure what to think now haha!

1 Like

Hello, I see that you wrote:

https://phaazon.net/blog/editors-in-2022

and other interesting thoughts about editors in 2021, 2020 … In the latest version (2022) you explained that you were moving away from Emacs and mentioned Helix but not Kakoune. Any thoughts about Kakoune that you would like to share?

Edit: I meant this in reply to phaazon

1 Like

Yes, I’m mostly using Kakoune now and plan to write another blog article about all that. But so far I still need to figure out whether I like the design of Kakoune. I have some concerns, especially about « collective smartness » if that makes sense. I.e. I had to make some design to stream buffer modifications for my kak-tree-sitter (probably similar to the OP’s tree-sitter.kak, and I know that kak-lsp must be doing the same… which seems like a lot of similar work that can’t be shared efficiently.

1 Like

I’ll have to take a look into your implementation! Yeah right now the languages are statically configured, but I’ve started the process of making the queries configurable at runtime. Making the grammar configurable might also be possible, but I’m not familiar enough with how tree-sitter loads the grammars to confirm that.

Also I agree with your later comment that multiple plugins might want to access buffer contents, and it’s unfortunate that it isn’t shared among them.

I don’t think you need the InsertChar, InsertDelete or BufReload hooks,
the combination of InsertIdle and NormalIdle should be enough I think

Yeah I went a bit wild with the hooks and didn’t remove them. I think I agree, but wouldn’t BufReload still be required? If the buffer reloads I don’t think neither InsertIdle nor NormalIdle hooks fire.

right BufReload is independent