Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/s1n7ax/youtube-neovim-treesitter-query
Presentation related to my Youtube tutorial
https://github.com/s1n7ax/youtube-neovim-treesitter-query
neovim query treesitter treesitter-api tutorial youtube
Last synced: 2 months ago
JSON representation
Presentation related to my Youtube tutorial
- Host: GitHub
- URL: https://github.com/s1n7ax/youtube-neovim-treesitter-query
- Owner: s1n7ax
- License: mit
- Created: 2021-09-30T18:49:08.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-10-03T09:51:15.000Z (over 3 years ago)
- Last Synced: 2024-08-07T18:34:51.171Z (6 months ago)
- Topics: neovim, query, treesitter, treesitter-api, tutorial, youtube
- Language: Lua
- Homepage: https://www.youtube.com/channel/UCjKCYQ21t4GMWWDQZI2jTYg
- Size: 2.93 KB
- Stars: 11
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Youtube Neovim Treesitter Query
This is a presentation created for Youtube video tutorial on Treesitter Query[Click here to watch the video](https://youtu.be/86sgKa0jeO4)
# Neovim Treesitter Query
* Neovim Treesitter Playground
* Language Tree & Syntax Tree
* Parse Query, Predicates & Directives
* Captures and Matches# Treesitter Playground
## How to install
[Nvim treesitter playground GitHub](https://github.com/nvim-treesitter/playground)
### Packer
* Put the following configuration to packer package list
* Close Neovim and reopen
* Run `:PackerInstall` and `:PackerCompile````lua
use {
'nvim-treesitter/playground',
requires = { 'nvim-treesitter/nvim-treesitter' },
cmd = 'TSPlaygroundToggle',
config = function()
R'nvim-treesitter.configs'.setup({})
end,
}
```# Language Tree & Syntax Tree
* Get language tree for a given buffer
```lua
local language_tree = vim.treesitter.get_parser()
```
* Build syntax tree
```lua
local syntax_tree = language_tree:parse()
```
* Root node of the syntax tree
```lua
local root = syntax_tree[1]:root()
```# Parse Query, Predicates & Directives
## Help
* `:h lua-treesitter-query`
* `:h parse_query`
* `:h lua-treesitter-predicates`
* `:h lua-treesitter-directives`## Create a query
```lua
local query = vim.treesitter.parse_query( 'java', '(method_declaration)')for id, match, metadata in query:iter_matches(root, , root:start(), root:end_()) do
print(vim.inspect(getmetatable(match[1])))
end
```## Create a capture
```lua
local query = vim.treesitter.parse_query('java', '(method_declaration) @method')
```## Add predicate
```lua
local query = vim.treesitter.parse_query( 'java', [[
(method_declaration
(modifiers
(marker_annotation
name: (identifier) @annotation (#eq? @annotation "Test"))))
]])
```## Get node text
```lua
local q = R 'vim.treesitter.query'local query = vim.treesitter.parse_query( 'java', [[
(method_declaration
(modifiers
(marker_annotation
name: (identifier) @annotation (#eq? @annotation "Test")))
name: (identifier) @method-name)
]])for id, match, metadata in query:iter_matches(root, , root:start(), root:end_()) do
print(q.get_node_text(match[2], ))
end
```### Get offset of the node
```lua
local query = vim.treesitter.parse_query('java', [[
(method_declaration
(modifiers
(marker_annotation
name: (identifier) @annotation (#eq? @annotation "Test")))
name: (identifier) @method-name (#offset! @method-name))
]])for id, match, metadata in query:iter_matches(root, , root:start(), root:end_()) do
print(vim.inspect(metadata))
end
```