Nvim-treesitter configuration: Difference between revisions
(23 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
There are two main components in a treesitter config. | |||
* parsers: languages | |||
* modules: language features (syntax-highlighting, indentation, ...) | |||
= Documentation = | |||
<blockquote> | |||
{| class="wikitable" | |||
|- | |||
| github || https://github.com/nvim-treesitter/nvim-treesitter | |||
|- | |||
|} | |||
</blockquote><!-- Documentation --> | |||
= Locations = | = Locations = | ||
<blockquote> | <blockquote> | ||
Line 5: | Line 18: | ||
| <code>~/.config/nvim/init.vim</code> || neovim config | | <code>~/.config/nvim/init.vim</code> || neovim config | ||
|- | |- | ||
| <code>~/.vim/queries/**/*</code> || language-specific module configs | | <code>~/.vim/queries/**/*</code> || language-specific module configs (folds, highlights, indents, ...) | ||
|- | |||
|} | |} | ||
</blockquote><!-- Locations --> | </blockquote><!-- Locations --> | ||
Line 15: | Line 29: | ||
You can set the parsers you'd like to make available within your <code>init.vim</code>. | You can set the parsers you'd like to make available within your <code>init.vim</code>. | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="lua"> | ||
-- ~/.config/nvim/init.vim | |||
lua <<EOF | lua <<EOF | ||
require'nvim-treesitter.configs'.setup { | require'nvim-treesitter.configs'.setup { | ||
-- automatically install these language parsers: | -- automatically install these language parsers: | ||
-- * "maintained" | -- * "maintained" # all parsers with maintainers | ||
-- * "all" | -- * "all" # all available parsers | ||
-- * { "bash", "python", .. } # these specific languages | -- * { "bash", "python", .. } # these specific languages | ||
ensure_installed = "maintained"; | ensure_installed = "maintained"; | ||
} | } | ||
EOF | |||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Parsers --> | </blockquote><!-- Parsers --> | ||
Line 33: | Line 48: | ||
Enable modules within your <code>init.nvim</code> | Enable modules within your <code>init.nvim</code> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="lua"> | ||
-- ~/.config/nvim/init.vim | |||
lua <<EOF | lua <<EOF | ||
Line 40: | Line 55: | ||
highlight = { | highlight = { | ||
enable = true, | enable = true, | ||
-- vim syntax + treesitter syntax? (true/false/list-of-languages) | |||
-- | |||
additional_vim_regex_highlighting = false, | additional_vim_regex_highlighting = false, | ||
}, | }, | ||
incremental_selection = { enable = true; }, | |||
incremental_selection = { | indent = { enable = true; }, | ||
indent = { | |||
} | } | ||
EOF | EOF | ||
Line 82: | Line 89: | ||
</blockquote><!-- Basics --> | </blockquote><!-- Basics --> | ||
== Syntax Highlighting == | |||
<blockquote> | |||
You can override syntax-highlighting.<br> | |||
See https://github.com/nvim-treesitter/nvim-treesitter/issues/3058 | |||
</blockquote><!-- Syntax-Highlighting --> | |||
== Folds == | == Folds == | ||
<blockquote> | <blockquote> | ||
For a long time there wasn't a way of customizing folds without forking the repo.<br> | |||
This can now be set in lua, which is much preferrable because it is easier to keep up to date. | |||
You can | |||
=== Lua configuration === | |||
<blockquote> | |||
You can configure with lua now | |||
<syntaxhighlight lang="lua"> | |||
vim.treesitter.set_query("python", "folds", [[ | |||
(function_definition (block) @fold) | |||
(class_definition (block) @fold) | |||
]]) | |||
</syntaxhighlight> | |||
See [https://github.com/nvim-treesitter/nvim-treesitter/issues/1564 GH issue/solution] | |||
</blockquote><!-- Lua configuration --> | |||
=== Forking Repo (old method) === | |||
<blockquote> | |||
Examples of fold customization | |||
{{ expand | |||
| python | |||
| | |||
<syntaxhighlight lang="scheme"> | <syntaxhighlight lang="scheme"> | ||
; queries/python/folds.scm | ; ~/.vim/bundle/nvim-treesitter/queries/python/folds.scm | ||
(function_definition) @fold | (function_definition) @fold | ||
(class_definition) @fold | (class_definition) @fold | ||
</syntaxhighlight> | </syntaxhighlight> | ||
}} | |||
{{ expand | |||
| ruby | |||
| | |||
<syntaxhighlight lang="scheme"> | |||
(method) @fold | |||
(singleton_method) @fold | |||
(class) @fold | |||
(module) @fold | |||
(singleton_class) @fold | |||
(lambda) @fold | |||
</syntaxhighlight> | |||
}} | |||
{{ expand | |||
| graphql | |||
| | |||
<syntaxhighlight lang="scheme"> | |||
(directive) @fold | |||
(enum_type_definition) @fold | |||
(enum_type_extension) @fold | |||
(input_object_type_definition) @fold | |||
(input_object_type_extension) @fold | |||
(interface_type_definition) @fold | |||
(interface_type_extension) @fold | |||
(named_type) @fold | |||
(object_type_definition) @fold | |||
(object_type_extension) @fold | |||
(scalar_type_definition) @fold | |||
(scalar_type_extension) @fold | |||
(union_type_definition) @fold | |||
(union_type_extension) @fold | |||
</syntaxhighlight> | |||
}} | |||
Keeping the fork alive | |||
<syntaxhighlight lang="bash"> | |||
# Update existing branch (minimum effort) | |||
git-updateremotes | |||
git checkout master | |||
git pull github | |||
git checkout - | |||
# see modified files | |||
git diff --diff-filter=M --name-only master... | |||
# confirm all searches still valid | |||
git diff --diff-filter=M master... | |||
git rebase master | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="bash"> | |||
# Build new branch (safest/fallback) | |||
git-updateremotes | |||
git checkout master | |||
git fetch github/master | |||
git reset --hard github/master | |||
git checkout -b fold_preferences_${LAST} | |||
git log # see files that I changed | |||
for f in `cat ~/files_i_want`; do; git checkout fold_preferences_${LAST} -- "$f" ; done | |||
tig # see changes | |||
nvim ~/.vim/config/plugins/nvim/treesitter/init.vim # update 'Plug ... branch' | |||
:TSUpdate # otherwise WILL fail | |||
# test nvim, and each file within pers-testfiles | |||
</syntaxhighlight> | |||
</blockquote><!-- Forking Repo --> | |||
</blockquote><!-- Folds --> | </blockquote><!-- Folds --> | ||
</blockquote><!-- Customizing Features --> | </blockquote><!-- Customizing Features --> |
Latest revision as of 23:12, 30 July 2023
There are two main components in a treesitter config.
- parsers: languages
- modules: language features (syntax-highlighting, indentation, ...)
Documentation
github https://github.com/nvim-treesitter/nvim-treesitter
Locations
~/.config/nvim/init.vim
neovim config ~/.vim/queries/**/*
language-specific module configs (folds, highlights, indents, ...)
Enabling Features
Parsers
You can set the parsers you'd like to make available within your
init.vim
.-- ~/.config/nvim/init.vim lua <<EOF require'nvim-treesitter.configs'.setup { -- automatically install these language parsers: -- * "maintained" # all parsers with maintainers -- * "all" # all available parsers -- * { "bash", "python", .. } # these specific languages ensure_installed = "maintained"; } EOFModules
Enable modules within your
init.nvim
-- ~/.config/nvim/init.vim lua <<EOF require'nvim-treesitter.configs'.setup { highlight = { enable = true, -- vim syntax + treesitter syntax? (true/false/list-of-languages) additional_vim_regex_highlighting = false, }, incremental_selection = { enable = true; }, indent = { enable = true; }, } EOFFolds
Adjust the foldmethods/foldexprs for languages you'd like to fold using tree-sitter.
set foldmethod=expr set foldexpr=nvim_treesitter#foldexpr()
Customizing Features
NOTE:
I gave up on trying to do this the cleanly.
I forked the project and I'm hosting it at home. Will rebase periodically for new features.Basics
Default configuration of features is set within the queries directory in
.scm
files.
In the code, these are read using nvim_get_runtime_file which layers files from~/.vim
overtop of these files.
Simply override the settings within your .vim directory.Syntax Highlighting
You can override syntax-highlighting.
See https://github.com/nvim-treesitter/nvim-treesitter/issues/3058Folds
For a long time there wasn't a way of customizing folds without forking the repo.
This can now be set in lua, which is much preferrable because it is easier to keep up to date.Lua configuration
You can configure with lua now
vim.treesitter.set_query("python", "folds", [[ (function_definition (block) @fold) (class_definition (block) @fold) ]])Forking Repo (old method)
Examples of fold customization
python
; ~/.vim/bundle/nvim-treesitter/queries/python/folds.scm (function_definition) @fold (class_definition) @fold
ruby
(method) @fold (singleton_method) @fold (class) @fold (module) @fold (singleton_class) @fold (lambda) @fold
graphql
(directive) @fold (enum_type_definition) @fold (enum_type_extension) @fold (input_object_type_definition) @fold (input_object_type_extension) @fold (interface_type_definition) @fold (interface_type_extension) @fold (named_type) @fold (object_type_definition) @fold (object_type_extension) @fold (scalar_type_definition) @fold (scalar_type_extension) @fold (union_type_definition) @fold (union_type_extension) @fold
Keeping the fork alive
# Update existing branch (minimum effort) git-updateremotes git checkout master git pull github git checkout - # see modified files git diff --diff-filter=M --name-only master... # confirm all searches still valid git diff --diff-filter=M master... git rebase master# Build new branch (safest/fallback) git-updateremotes git checkout master git fetch github/master git reset --hard github/master git checkout -b fold_preferences_${LAST} git log # see files that I changed for f in `cat ~/files_i_want`; do; git checkout fold_preferences_${LAST} -- "$f" ; done tig # see changes nvim ~/.vim/config/plugins/nvim/treesitter/init.vim # update 'Plug ... branch' :TSUpdate # otherwise WILL fail # test nvim, and each file within pers-testfiles