plugins/treesitter: allow highlight to be fine-grained

This commit is contained in:
raf 2024-04-23 16:48:39 +03:00
parent 49b705b6aa
commit 5e08ed42e7
No known key found for this signature in database
GPG key ID: 02D1DD3FA08B6B29
2 changed files with 42 additions and 4 deletions

View file

@ -6,7 +6,9 @@
}: let
inherit (lib.modules) mkIf mkMerge;
inherit (lib.lists) optional;
inherit (lib.trivial) boolToString;
inherit (lib.nvim.binds) mkSetBinding addDescriptionsToMappings;
inherit (lib.nvim.lua) toLuaObject;
inherit (lib.nvim.dag) entryBefore entryAnywhere;
cfg = config.vim.treesitter;
@ -50,8 +52,8 @@ in {
ensure_installed = {},
highlight = {
enable = true,
disable = {},
enable = ${boolToString cfg.highlight.enable},
disable = ${toLuaObject cfg.highlight.disable},
},
incremental_selection = {

View file

@ -3,9 +3,10 @@
lib,
...
}: let
inherit (lib.options) mkOption mkEnableOption;
inherit (lib.options) mkOption mkEnableOption literalMD;
inherit (lib.nvim.binds) mkMappingOption;
inherit (lib.types) listOf package;
inherit (lib.types) listOf package str either;
inherit (lib.nvim.types) luaInline;
inherit (pkgs.vimPlugins.nvim-treesitter) builtGrammars;
in {
@ -33,6 +34,41 @@ in {
'';
};
highlight = {
enable = mkEnableOption "highlighting with treesitter";
disable = mkOption {
type = either (luaInline (listOf str));
default = [];
example = literalMD ''
```lua
-- Disable slow treesitter highlight for large files
disable = function(lang, buf)
local max_filesize = 1000 * 1024 -- 1MB
local ok, stats = pcall(vim.loop.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.
:::
'';
};
};
defaultGrammars = mkOption {
internal = true;
readOnly = true;