Repository navigation
Tree-sitter step 1: vendor runtime lib + add lua API - #10124
Conversation
👍 This makes sense to me. In case it's useful as a reference, the Tree-sitter CLI has logic for automatically compiling Tree-sitter parsers into dynamic libraries and dynamically loading them when you run the Great to see this happening! Let me know if I can help with anything. |
That sounds nice. Maybe we can use it to automate building of parsers for our own binary packages (like nvim.appimage) later on. |
|
Does this main tree-sitter will be doing all its work in the main thread? And if so, would it be hard to push this processing work out into a child process using buffer update events and RPC in the future? |
|
@phodge the idea is is that most parses would be really quick. Thus we benefit from the simplicity of accessing the tree-sitter node API in-process with a thin lua wrapper. I think atom uses a hybrid method, it parses in the main thread up to an operation limit, but then pushes the remaining parsing job to a separate thread if it is too slow. It would be interesting to see if we can do something similar. |
I understand that Tree-sitter is designed to be fast enough for running in the main thread, and that it makes the APIs much simpler if we do it in the main thread. But I think we should be cautious about this approach because these are both reasons why Vim failed us and we had to fork Neovim. Too many things competing for a single CPU core, and no way to distribute the workload because IPC is hard. But tree-sitter in a separate process is probably a problem for the future. If it's not too much to ask, I'd love to see a help text explaining how to use the Lua functions to access the syntax tree. It looks like currently all I can do is turn on tree-sitter for a buffer and then ask TS to highlight the node under the cursor? |
Sure, apart form fixing the build system, the plan is to add docs and tests for part of the API we will expose in this first PR :) |
Yeah, Atom uses this API to set a timeout of ~1 millisecond for main thread parsing. And if that timeout expires, it resumes parsing on a background thread. The syntax trees are persistent, so they can be cheaply copied for use an a background thread. I'm curious how this would translate to Neovim. In Atom's case, it relies on the text buffer having a snapshot API that allows you to safely read from a certain version of the buffer in a background thread, even if the user is mutating the buffer on the main thread. Does the vim buffer have a way to do something like that? |
There is some form of "locking" mechanism to lock parts of the buffer in the backing storage (memline.c), but I don't think it was ever intended for safe multithreaded usage :P We probably have to make a straight copy of the modified part of the buffer in that case. But |
#9943 is exploring ways to make IPC easier. |
9e974ff to
d611726
Compare
808ad4d to
1c600f6
Compare
|
bikeshed: would be nice if "tree sitter" is spelled |
a8607a4 to
ce4d57d
Compare
|
Update: basic tree-sitter test now works on travis (64-bit linux and OS X). windows/appveyor still requires some work... |
e876bae to
46a3f4e
Compare
|
Building the parser and running tests now work on 32-bit Appveyor (MINGW_64 nvim also compiles, but the parser |
272bb26 to
4ae02cb
Compare
|
@justinmk would it be possible to install 64-bit node instead on the 64-bit appveyor builds? Or should we just skip tree-sitter tests on MINGW_64 for now? (We can look into compiling the languages by ourselves later, node is not really required). |
If for some reason you don't want to change the Node installation, you could also download the 64-bit |
|
@maxbrunsfeld Thanks. that would cover all travis/appveyor builds except 32-bit linux. I guess we could bulid that binary ourselves and keep it in the cached dependencies, for now. |
46c5301 to
c08ba6b
Compare
build tree-sitter c parser on ci for testing purposes
5b878bb to
406587a
Compare
406587a to
9fa8509
Compare
|
@bfredl For watchers of this PR, where should we continue following the tree sitter integration work now that this is merged? |
|
I will open the next PR (query API) very soon :) |
|
See #11113. Please continue discussion there or open a new issue for things like build problems. |
#9219 has already grown pretty large. To get something reviewable and mergable in a resonable timeframe, I will try to divide it into smaller PRs. My suggestion for a MVP is the following: vendor the tree-sitter runtime lib into our source-tree, and expose syntax trees as a lua API. More specific features such as syntax highlighting and multi-language parsing will be follow up PRs.
Vendoring the runtime allows us to always build the lua tree-sitter bindings and support code (there will be no
FEAT_TREESITTER), without making the build any more complicated for distributors. The runtime lib without parsers is not comparatively large, about as big as nvim'ssyntax.cin its entirety. Though I expectlibutf8procto be common in distros and "native binary" package solutions already, so we should be able to treat that as an ordinary dependency.On the other hand, the tree-sitter parsers for specific languages will initially not be built by the nvim build system. Instead we expect those who experiment with the MVP to e.g. build the parsers they want themselves for their respective platform, and nvim will provide an interface to load them as dynamic libraries. This means we can discuss and work on a reasonable final distribution strategy in parallel with other tasks (as mentioned above).
CC @maxbrunsfeld if you have any comments of our vendoring strategy.