Tree sitter & LSP: Short Comparison

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so here's the deal with tree sitter generally tree sitter largest sort of idea is that it's a library to build what are effectively syntax trees they're not exactly syntax trees but they're very similar for languages and it's a library where you can write uh sort of these generate these generators of a grammar and then you're able to use them with tree sitter tree sitter provides you with some very powerful building blocks and tools the first is that it's incremental so this is very good for text editors because normally you type a lot of things into the text editor and you move a lot of things around it's better if you don't have to start at the beginning of the file every time you want to parse something it's much better if you can start at like some end part of the file or something like that or just change the bytes that you just typed differently that's the first part that provides tree sitter that is very valuable uh the second one well additionally just like as a general note tree sitter is quite fast so that's like that's good the other note is that tree sitter has what's called sort of like air recovery which is very helpful when you're riding a parser because most of the time like especially when you're editing text 99 of the time when you're writing the text the code is broken right so you're like writing function and until you type the word function it's not the keyword and you know like while you're writing function and then x that's still not valid lua you need to get all the way to here before it's valid but you don't want your whole file to like break just because you're typing something which is sometimes what happens when you use regexes uh but treesitter will make basically its best guess to try and like support where you're at um where where you are and what you're doing so it doesn't just like completely die when you have an error in your file so those are like two of the main reasons that tree sitter is very cool particularly for text editors but the second the sort of like second aspect or facet of it is that it allows you to write queries and sort of ask tree sitter about the code which is really really powerful because it allows you to do things like build text objects based on the actual syntax tree of the code not just like some regex that you think matches so if you want to move two parameters around in a function you can ask what's the parameter i'm on and the parameter earlier in the syntax tree and switch their positions and then when you apply that update it's all done there's there's no like regexes or hoping you're just changing two nodes in the syntax tree so you can start to do very powerful things on the text and you're editing more in like a structural manner rather than sort of like an ad hoc or a like regex manner additionally you can sort of get information about the code using the grammar by writing queries to like find out where something was located now you might say tj that sounds awfully similar to basically like language server protocol you know you're like finding definitions you can find references you can move stuff around you can refactor like isn't that what lsp does and it's like well yes kind of in fact some lsps are built off of tree sitter and in fact the tree the language server i want to build for lua will also be at least utilize tree sitter inside of it tree sitter has much more of a focus on just the one file that you're in it can provide you with a lot of information about the current file that you're editing but it does not try and support sort of like project wide understanding at least in its base library it's just about parsing text files and turning them into syntax trees so um so so you're like language server is gonna have much more sort of like project level understanding but it might take like a really long time to get that understanding like it you know you might ask it a query and it might take a second to reply whereas tree sitter will answer in like sub millisecond timing so there are like some benefits and some pros and cons but like tree sitter will be able to do like highlighting in your buffer generally much better than a language server could do because it's going to be doing this incremental parsing it's going to be working much faster all of the code that's running is like built into this generated c that is uh very cool and fast etc etc etc so like applying highlights in your editor will be much better from tree sitter than it would be from like getting it back and forth from the language server at least uh highlight your entire file for example so i think that's sort of like the general idea of like what tree sitter is and how it compares to lsp does anyone have any questions about that can you please explain about the query schemes sure the way that you can query things is with basically a small lisp like language that's basically looks like scheme and you can write queries that will return things you can read more about the syntax if you want on uh like tree sitters website i guess they have documentation for that but if you want to like look at like for highlights for c what you can do is you can say like hey these are keywords so like just match these strings but if i want to do something like match a node there's pre-proc directives and we can match that and i want to apply keyword now what this does is it returns something that matches this and nvim tree sitter does stuff on top of this to apply highlights but there's nothing too special about what's happening here basically like i want to apply a highlight of string to anything that looks like string literals or system lip strings or you know these other items and so then that's how when you are inside of something
Info
Channel: TJ DeVries
Views: 8,494
Rating: 4.9408865 out of 5
Keywords: twitch, games
Id: c17j09vY5sw
Channel Id: undefined
Length: 6min 55sec (415 seconds)
Published: Tue Sep 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.