Merge pull request #1318 from HeitorAugustoLN/treesitter-migrate
Some checks are pending
Set up binary cache / cachix (default) (push) Waiting to run
Set up binary cache / cachix (maximal) (push) Waiting to run
Set up binary cache / cachix (nix) (push) Waiting to run
Treewide Checks / Validate flake (push) Waiting to run
Treewide Checks / Check formatting (push) Waiting to run
Treewide Checks / Check source tree for typos (push) Waiting to run
Treewide Checks / Validate documentation builds (push) Waiting to run
Treewide Checks / Validate hyperlinks in documentation sources (push) Waiting to run
Treewide Checks / Validate Editorconfig conformance (push) Waiting to run
Build and deploy documentation / Check latest commit (push) Waiting to run
Build and deploy documentation / publish (push) Blocked by required conditions

treesitter: migrate to the new api
This commit is contained in:
raf 2026-01-07 09:15:15 +03:00 committed by GitHub
commit ceaae0eb2e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 82 additions and 187 deletions

View file

@ -4,12 +4,8 @@
- Nixpkgs has merged a fully incompatible rewrite of - Nixpkgs has merged a fully incompatible rewrite of
`vimPlugins.nvim-treesitter`. Namely, it changes from the frozen `master` `vimPlugins.nvim-treesitter`. Namely, it changes from the frozen `master`
branch to the new main branch. This change also affects how grammars are branch to the new main branch. This change removes incremental selections, so
built, and forces us to change a few things around. it is no longer available.
- We must now use `"nvim-treesitter".setup` over the old `.configs`.
Additionally, built grammars **no longer include queries by default**,
therefore queries not managed by nvf will lack their respective syntax
highlighting capabilities.
## Changelog {#sec-release-0-9-changelog} ## Changelog {#sec-release-0-9-changelog}

View file

@ -308,5 +308,39 @@ in {
]) ])
# Migrated via batchRenameOptions. Further batch renames must be below this line. # Migrated via batchRenameOptions. Further batch renames must be below this line.
renamedVimOpts renamedVimOpts
# 2026-01-06
[
(mkRemovedOptionModule ["vim" "treesitter" "highlight" "disable"] ''
Treesitter highlighting is now handled by Neovim natively, and it does not have a disable option.
'')
(mkRemovedOptionModule ["vim" "treesitter" "highlight" "additionalVimRegexHighlighting"] ''
Treesitter highlighting is now handled by Neovim natively, and it does not have a additionalVimRegexHighlighting option.
'')
(mkRemovedOptionModule ["vim" "treesitter" "indent" "disable"] ''
Treesitter indentation is now handled differently, and it does not have a disable option.
'')
(mkRemovedOptionModule ["vim" "treesitter" "incrementalSelection" "enable"] ''
Incremental selection configuration has been removed from nvim-treesitter.
'')
(mkRemovedOptionModule ["vim" "treesitter" "incrementalSelection" "disable"] ''
Incremental selection configuration has been removed from nvim-treesitter.
'')
(mkRemovedOptionModule ["vim" "treesitter" "mappings" "incrementalSelection" "init"] ''
Incremental selection configuration has been removed from nvim-treesitter.
'')
(mkRemovedOptionModule ["vim" "treesitter" "mappings" "incrementalSelection" "incrementByNode"] ''
Incremental selection configuration has been removed from nvim-treesitter.
'')
(
mkRemovedOptionModule ["vim" "treesitter" "mappings" "incrementalSelection" "incrementByScope"]
''
Incremental selection configuration has been removed from nvim-treesitter.
''
)
(mkRemovedOptionModule ["vim" "treesitter" "mappings" "incrementalSelection" "decrementByNode"] ''
Incremental selection configuration has been removed from nvim-treesitter.
'')
]
]; ];
} }

View file

@ -1,19 +1,13 @@
{ {
config, config,
lib, lib,
options,
... ...
}: let }: let
inherit (lib.modules) mkIf mkMerge; inherit (lib.modules) mkIf;
inherit (lib.lists) optionals; inherit (lib.lists) optionals;
inherit (lib.nvim.binds) mkSetBinding addDescriptionsToMappings; inherit (lib.nvim.dag) entryAfter;
inherit (lib.nvim.lua) toLuaObject;
inherit (lib.nvim.dag) entryBefore entryAfter;
cfg = config.vim.treesitter; cfg = config.vim.treesitter;
mappingDefinitions = options.vim.treesitter.mappings;
mappings = addDescriptionsToMappings cfg.mappings mappingDefinitions;
in { in {
config = mkIf cfg.enable { config = mkIf cfg.enable {
vim = { vim = {
@ -27,68 +21,46 @@ in {
treesitter.grammars = optionals cfg.addDefaultGrammars cfg.defaultGrammars; treesitter.grammars = optionals cfg.addDefaultGrammars cfg.defaultGrammars;
maps = { pluginRC.treesitter-autocommands = entryAfter ["basic"] ''
# HACK: Using mkSetLuaBinding and putting the lua code does not work for some reason: It just selects the whole file. vim.api.nvim_create_augroup("nvf_treesitter", { clear = true })
# This works though, and if it ain't broke, don't fix it.
normal = mkSetBinding mappings.incrementalSelection.init ":lua require('nvim-treesitter.incremental_selection').init_selection()<CR>";
visualOnly = mkMerge [ ${lib.optionalString cfg.highlight.enable ''
(mkSetBinding mappings.incrementalSelection.incrementByNode "<cmd>lua require('nvim-treesitter.incremental_selection').node_incremental()<CR>") -- Enable treesitter highlighting for all filetypes
(mkSetBinding mappings.incrementalSelection.incrementByScope "<cmd>lua require('nvim-treesitter.incremental_selection').scope_incremental()<CR>") vim.api.nvim_create_autocmd("FileType", {
(mkSetBinding mappings.incrementalSelection.decrementByNode "<cmd>lua require('nvim-treesitter.incremental_selection').node_decremental()<CR>") group = "nvf_treesitter",
]; pattern = "*",
}; callback = function()
pcall(vim.treesitter.start)
end,
})
''}
# For some reason treesitter highlighting does not work on start if this is set before syntax on ${lib.optionalString cfg.indent.enable ''
pluginRC.treesitter-fold = mkIf cfg.fold (entryBefore ["basic"] '' -- Enable treesitter highlighting for all filetypes
-- This is required by treesitter-context to handle folds vim.api.nvim_create_autocmd("FileType", {
vim.o.foldmethod = "expr" group = "nvf_treesitter",
vim.o.foldexpr = "nvim_treesitter#foldexpr()" pattern = "*",
callback = function()
vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
end,
})
''}
-- This is optional, but is set rather as a sane default. ${lib.optionalString cfg.fold ''
-- If unset, opened files will be folded by automatically as -- Enable treesitter folding for all filetypes
-- the files are opened vim.api.nvim_create_autocmd("FileType", {
vim.o.foldenable = false group = "nvf_treesitter",
''); pattern = "*",
callback = function()
pluginRC.treesitter = entryAfter ["basic"] '' vim.wo[0][0].foldmethod = "expr"
require('nvim-treesitter.config').setup { vim.wo[0][0].foldexpr = "v:lua.vim.treesitter.foldexpr()"
-- Disable imperative treesitter options that would attempt to fetch -- This is optional, but is set rather as a sane default.
-- grammars into the read-only Nix store. To add additional grammars here -- If unset, opened files will be folded by automatically as
-- you must use the `config.vim.treesitter.grammars` option. -- the files are opened
auto_install = false, vim.o.foldenable = false
sync_install = false, end,
ensure_installed = {}, })
''}
-- Indentation module for Treesitter
indent = {
enable = ${toLuaObject cfg.indent.enable},
disable = ${toLuaObject cfg.indent.disable},
},
-- Highlight module for Treesitter
highlight = {
enable = ${toLuaObject cfg.highlight.enable},
disable = ${toLuaObject cfg.highlight.disable},
additional_vim_regex_highlighting = ${toLuaObject cfg.highlight.additionalVimRegexHighlighting},
},
-- Indentation module for Treesitter
-- Keymaps are set to false here as they are
-- handled by `vim.maps` entries calling lua
-- functions achieving the same functionality.
incremental_selection = {
enable = ${toLuaObject cfg.incrementalSelection.enable},
disable = ${toLuaObject cfg.incrementalSelection.disable},
keymaps = {
init_selection = false,
node_incremental = false,
scope_incremental = false,
node_decremental = false,
},
},
}
''; '';
}; };
}; };

View file

@ -3,21 +3,12 @@
lib, lib,
... ...
}: let }: let
inherit (lib.options) mkOption mkEnableOption literalMD literalExpression; inherit (lib.options) mkOption mkEnableOption literalExpression;
inherit (lib.types) listOf package str either bool; inherit (lib.types) listOf package bool;
inherit (lib.nvim.binds) mkMappingOption;
inherit (lib.nvim.types) luaInline;
in { in {
options.vim.treesitter = { options.vim.treesitter = {
enable = mkEnableOption "treesitter, also enabled automatically through language options"; enable = mkEnableOption "treesitter, also enabled automatically through language options";
mappings.incrementalSelection = {
init = mkMappingOption "Init selection [treesitter]" "gnn";
incrementByNode = mkMappingOption "Increment selection by node [treesitter]" "grn";
incrementByScope = mkMappingOption "Increment selection by scope [treesitter]" "grc";
decrementByNode = mkMappingOption "Decrement selection by node [treesitter]" "grm";
};
fold = mkEnableOption "fold with treesitter"; fold = mkEnableOption "fold with treesitter";
autotagHtml = mkEnableOption "autoclose and rename html tag"; autotagHtml = mkEnableOption "autoclose and rename html tag";
@ -25,14 +16,14 @@ in {
type = listOf package; type = listOf package;
default = []; default = [];
example = literalExpression '' example = literalExpression ''
with pkgs.vimPlugins.nvim-treesitter.grammarPlugins; [ with pkgs.vimPlugins.nvim-treesitter.parsers; [
regex regex
kdl kdl
]; ];
''; '';
description = '' description = ''
List of treesitter grammars to install. For grammars to be installed properly, List of treesitter grammars to install. For grammars to be installed properly,
you must use grammars from `pkgs.vimPlugins.nvim-treesitter.grammarPlugins`. you must use grammars from `pkgs.vimPlugins.nvim-treesitter.parsers` or `pkgs.vimPlugins.nvim-treesitter.grammarPlugins`.
You can use `pkgs.vimPlugins.nvim-treesitter.allGrammars` to install all grammars. You can use `pkgs.vimPlugins.nvim-treesitter.allGrammars` to install all grammars.
For languages already supported by nvf, you may use For languages already supported by nvf, you may use
@ -56,7 +47,7 @@ in {
internal = true; internal = true;
readOnly = true; readOnly = true;
type = listOf package; type = listOf package;
default = with pkgs.vimPlugins.nvim-treesitter.grammarPlugins; [c lua vim vimdoc query]; default = with pkgs.vimPlugins.nvim-treesitter.parsers; [c lua vim vimdoc query];
description = '' description = ''
A list of treesitter grammars that will be installed by default A list of treesitter grammars that will be installed by default
if treesitter has been enabled and {option}`vim.treeesitter.addDefaultGrammars` if treesitter has been enabled and {option}`vim.treeesitter.addDefaultGrammars`
@ -73,105 +64,7 @@ in {
''; '';
}; };
indent = { indent = {enable = mkEnableOption "indentation with treesitter" // {default = true;};};
enable = mkEnableOption "indentation with treesitter" // {default = true;}; highlight = {enable = mkEnableOption "highlighting with treesitter" // {default = true;};};
disable = mkOption {
type = either (listOf str) luaInline;
default = [];
example = literalExpression ''["c" "rust"]'';
description = ''
List of treesitter grammars to disable indentation for.
This option can be either a list, in which case it will be
converted to a Lua table containing grammars to disable
indentation for, or a string containing a **lua function**
that will be read as is.
::: {.warning}
A comma will be added at the end of your function, so you
do not need to add it yourself. Doing so will cause in
syntax errors within your Neovim configuration.
:::
'';
};
};
highlight = {
enable = mkEnableOption "highlighting with treesitter" // {default = true;};
disable = mkOption {
type = either (listOf str) luaInline;
default = [];
example = literalMD ''
```lua
-- Disable slow treesitter highlight for large files
function(lang, buf)
local max_filesize = 1000 * 1024 -- 1MB
local ok, stats = pcall(vim.uv.fs_stat, vim.api.nvim_buf_get_name(buf))
if ok and stats and stats.size > max_filesize then
return true
end
end
```
'';
description = ''
List of treesitter grammars to disable highlighting for.
This option can be either a list, in which case it will be
converted to a Lua table containing grammars to disable
highlighting for, or a string containing a **lua function**
that will be read as is.
::: {.warning}
A comma will be added at the end of your function, so you
do not need to add it yourself. Doing so will cause in
syntax errors within your Neovim configuration.
:::
'';
};
additionalVimRegexHighlighting = mkOption {
type = either bool (listOf str);
default = false;
description = ''
Takes either a boolean or a list of languages.
Setting this to true will run `:h syntax` and tree-sitter at the same time.
You may this to `true` if you depend on 'syntax' being enabled (like for
indentation).
::: {.note}
Using this option may slow down your editor, and you may see some duplicate
highlights.
:::
'';
};
};
incrementalSelection = {
enable = mkEnableOption "incremental selection with treesitter" // {default = true;};
disable = mkOption {
type = either (listOf str) luaInline;
default = [];
example = literalExpression ''["c" "rust" ]'';
description = ''
List of treesitter grammars to disable incremental selection
for.
This option can be either a list, in which case it will be
converted to a Lua table containing grammars to disable
indentation for, or a string containing a **lua function**
that will be read as is.
::: {.warning}
A comma will be added at the end of your function, so you
do not need to add it yourself. Doing so will cause in
syntax errors within your Neovim configuration.
:::
'';
};
};
}; };
} }