modules/treesitter: switch to explicit lib calls

This commit is contained in:
raf 2024-03-15 14:19:11 +03:00
parent d3f6446541
commit 81b9a8a95c
No known key found for this signature in database
GPG key ID: 02D1DD3FA08B6B29
4 changed files with 73 additions and 68 deletions

View file

@ -3,60 +3,64 @@
lib, lib,
... ...
}: let }: let
inherit (lib) addDescriptionsToMappings mkIf optional mkSetBinding mkMerge nvim; inherit (lib.modules) mkIf mkMerge;
inherit (lib.lists) optional;
inherit (lib.nvim.binds) mkSetBinding addDescriptionsToMappings;
inherit (lib.nvim.dag) entryBefore entryAnywhere;
cfg = config.vim.treesitter; cfg = config.vim.treesitter;
usingNvimCmp = config.vim.autocomplete.enable && config.vim.autocomplete.type == "nvim-cmp"; usingNvimCmp = config.vim.autocomplete.enable && config.vim.autocomplete.type == "nvim-cmp";
self = import ./treesitter.nix {inherit lib;}; self = import ./treesitter.nix {inherit lib;};
mappingDefinitions = self.options.vim.treesitter.mappings; mappingDefinitions = self.options.vim.treesitter.mappings;
mappings = addDescriptionsToMappings cfg.mappings mappingDefinitions; mappings = addDescriptionsToMappings cfg.mappings mappingDefinitions;
in { in {
config = mkIf cfg.enable { config = mkIf cfg.enable {
vim.startPlugins = vim = {
["nvim-treesitter"] startPlugins = ["nvim-treesitter"] ++ optional usingNvimCmp "cmp-treesitter";
++ optional usingNvimCmp "cmp-treesitter";
vim.autocomplete.sources = {"treesitter" = "[Treesitter]";}; autocomplete.sources = {"treesitter" = "[Treesitter]";};
# For some reason, using mkSetLuaBinding and putting the lua code does not work. It just selects the whole file. maps = {
# This works though, and if it ain't broke, don't fix it. # HACK: Using mkSetLuaBinding and putting the lua code does not work for some reason: It just selects the whole file.
vim.maps.normal = mkSetBinding mappings.incrementalSelection.init ":lua require('nvim-treesitter.incremental_selection').init_selection()<CR>"; # 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>";
vim.maps.visualOnly = mkMerge [ visualOnly = mkMerge [
(mkSetBinding mappings.incrementalSelection.incrementByNode ":lua require('nvim-treesitter.incremental_selection').node_incremental()<CR>") (mkSetBinding mappings.incrementalSelection.incrementByNode ":lua require('nvim-treesitter.incremental_selection').node_incremental()<CR>")
(mkSetBinding mappings.incrementalSelection.incrementByScope ":lua require('nvim-treesitter.incremental_selection').scope_incremental()<CR>") (mkSetBinding mappings.incrementalSelection.incrementByScope ":lua require('nvim-treesitter.incremental_selection').scope_incremental()<CR>")
(mkSetBinding mappings.incrementalSelection.decrementByNode ":lua require('nvim-treesitter.incremental_selection').node_decremental()<CR>") (mkSetBinding mappings.incrementalSelection.decrementByNode ":lua require('nvim-treesitter.incremental_selection').node_decremental()<CR>")
]; ];
};
# For some reason treesitter highlighting does not work on start if this is set before syntax on # For some reason treesitter highlighting does not work on start if this is set before syntax on
vim.configRC.treesitter-fold = mkIf cfg.fold (nvim.dag.entryBefore ["basic"] '' configRC.treesitter-fold = mkIf cfg.fold (entryBefore ["basic"] ''
set foldmethod=expr set foldmethod=expr
set foldexpr=nvim_treesitter#foldexpr() set foldexpr=nvim_treesitter#foldexpr()
set nofoldenable set nofoldenable
''); '');
vim.luaConfigRC.treesitter = nvim.dag.entryAnywhere '' luaConfigRC.treesitter = entryAnywhere ''
require'nvim-treesitter.configs'.setup { require'nvim-treesitter.configs'.setup {
highlight = { highlight = {
enable = true, enable = true,
disable = {}, disable = {},
},
auto_install = false,
ensure_installed = {},
incremental_selection = {
enable = true,
keymaps = {
init_selection = false,
node_incremental = false,
scope_incremental = false,
node_decremental = false,
}, },
},
} auto_install = false,
''; ensure_installed = {},
incremental_selection = {
enable = true,
keymaps = {
init_selection = false,
node_incremental = false,
scope_incremental = false,
node_decremental = false,
},
},
}
'';
};
}; };
} }

View file

@ -1,12 +1,16 @@
{ {
pkgs,
config, config,
lib, lib,
... ...
}: let }: let
inherit (lib) mkEnableOption mkOption types mkIf nvim boolToString; inherit (lib.options) mkOption mkEnableOption;
inherit (lib.types) int bool str nullOr enum;
inherit (lib.modules) mkIf;
inherit (lib.trivial) boolToString;
inherit (lib.nvim.lua) nullString;
inherit (lib.nvim.dag) entryAnywhere;
treesitter = config.vim.treesitter; inherit (config.vim) treesitter;
cfg = treesitter.context; cfg = treesitter.context;
in { in {
options.vim.treesitter.context = { options.vim.treesitter.context = {
@ -14,37 +18,37 @@ in {
maxLines = mkOption { maxLines = mkOption {
description = "How many lines the window should span. Values &lt;=0 mean no limit."; description = "How many lines the window should span. Values &lt;=0 mean no limit.";
type = types.int; type = int;
default = 0; default = 0;
}; };
minWindowHeight = mkOption { minWindowHeight = mkOption {
description = "Minimum editor window height to enable context. Values &lt;= 0 mean no limit."; description = "Minimum editor window height to enable context. Values &lt;= 0 mean no limit.";
type = types.int; type = int;
default = 0; default = 0;
}; };
lineNumbers = mkOption { lineNumbers = mkOption {
description = ""; description = "";
type = types.bool; type = bool;
default = true; default = true;
}; };
multilineThreshold = mkOption { multilineThreshold = mkOption {
description = "Maximum number of lines to collapse for a single context line."; description = "Maximum number of lines to collapse for a single context line.";
type = types.int; type = int;
default = 20; default = 20;
}; };
trimScope = mkOption { trimScope = mkOption {
description = "Which context lines to discard if [](#opt-vim.treesitter.context.maxLines) is exceeded."; description = "Which context lines to discard if [](#opt-vim.treesitter.context.maxLines) is exceeded.";
type = types.enum ["inner" "outer"]; type = enum ["inner" "outer"];
default = "outer"; default = "outer";
}; };
mode = mkOption { mode = mkOption {
description = "Line used to calculate context."; description = "Line used to calculate context.";
type = types.enum ["cursor" "topline"]; type = enum ["cursor" "topline"];
default = "cursor"; default = "cursor";
}; };
@ -54,13 +58,13 @@ in {
When separator is set, the context will only show up when there are at least 2 lines above cursorline. When separator is set, the context will only show up when there are at least 2 lines above cursorline.
''; '';
type = with types; nullOr str; type = nullOr str;
default = null; default = null;
}; };
zindex = mkOption { zindex = mkOption {
description = "The Z-index of the context window."; description = "The Z-index of the context window.";
type = types.int; type = int;
default = 20; default = 20;
}; };
}; };
@ -68,7 +72,7 @@ in {
config = mkIf (treesitter.enable && cfg.enable) { config = mkIf (treesitter.enable && cfg.enable) {
vim.startPlugins = ["nvim-treesitter-context"]; vim.startPlugins = ["nvim-treesitter-context"];
vim.luaConfigRC.treesitter-context = nvim.dag.entryAnywhere '' vim.luaConfigRC.treesitter-context = entryAnywhere ''
require'treesitter-context'.setup { require'treesitter-context'.setup {
enable = true, enable = true,
max_lines = ${toString cfg.maxLines}, max_lines = ${toString cfg.maxLines},
@ -77,7 +81,7 @@ in {
multiline_threshold = ${toString cfg.multilineThreshold}, multiline_threshold = ${toString cfg.multilineThreshold},
trim_scope = '${cfg.trimScope}', trim_scope = '${cfg.trimScope}',
mode = '${cfg.mode}', mode = '${cfg.mode}',
separator = ${nvim.lua.nullString cfg.separator}, separator = ${nullString cfg.separator},
max_lines = ${toString cfg.zindex}, max_lines = ${toString cfg.zindex},
} }
''; '';

View file

@ -1,4 +1,4 @@
{...}: { {
imports = [ imports = [
./treesitter.nix ./treesitter.nix
./context.nix ./context.nix

View file

@ -1,29 +1,26 @@
{lib, ...}: let {lib, ...}: let
inherit (lib) mkEnableOption mkMappingOption mkOption types; inherit (lib.options) mkOption mkEnableOption;
inherit (lib.nvim.binds) mkMappingOption;
inherit (lib.types) listOf package;
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";
fold = mkEnableOption "fold with treesitter"; fold = mkEnableOption "fold with treesitter";
autotagHtml = mkEnableOption "autoclose and rename html tag"; autotagHtml = mkEnableOption "autoclose and rename html tag";
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";
};
};
grammars = mkOption { grammars = mkOption {
type = with types; listOf package; type = listOf package;
default = []; default = [];
description = '' description = ''
List of treesitter grammars to install. For supported languages List of treesitter grammars to install. For supported languages
use the `vim.language.<lang>.treesitter` option use the `vim.language.<lang>.treesitter` option
''; '';
}; };
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";
};
}; };
} }