Nvim-treesitter configuration

From wikinotes
Revision as of 00:52, 7 July 2022 by Will (talk | contribs) (→‎Folds)

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";
}
EOF

Modules

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; },
}
EOF

Folds

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.

Folds

I don't think folds can be customized without changing the plugin sourcecode.
I think all searches are appended, you cannot remove builtin searches.
You can rewrite them though. Here are some examples:

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 Fork Alive

The safest way so far has been to:

git checkout master
git pull github
git checkout -b fold_preferences_N
cd queries
for f in * ; do ; git checkout fold_preferences_old -- "$f/folds.scm" ; done