2023-02-28 10:01:56 +01:00
|
|
|
{
|
|
|
|
config,
|
2024-04-23 15:08:55 +02:00
|
|
|
pkgs,
|
2023-02-28 10:01:56 +01:00
|
|
|
lib,
|
|
|
|
...
|
2023-11-07 01:50:27 +01:00
|
|
|
}: let
|
2024-03-15 12:19:11 +01:00
|
|
|
inherit (lib.modules) mkIf mkMerge;
|
2024-04-23 19:20:49 +02:00
|
|
|
inherit (lib.lists) optional optionals;
|
2024-03-15 12:19:11 +01:00
|
|
|
inherit (lib.nvim.binds) mkSetBinding addDescriptionsToMappings;
|
2024-04-23 15:48:39 +02:00
|
|
|
inherit (lib.nvim.lua) toLuaObject;
|
2024-04-24 22:34:39 +02:00
|
|
|
inherit (lib.nvim.dag) entryBefore entryAfter;
|
2023-11-07 01:50:27 +01:00
|
|
|
|
2023-02-28 10:01:56 +01:00
|
|
|
cfg = config.vim.treesitter;
|
2023-04-17 22:27:27 +02:00
|
|
|
usingNvimCmp = config.vim.autocomplete.enable && config.vim.autocomplete.type == "nvim-cmp";
|
2023-05-03 00:15:05 +02:00
|
|
|
|
2024-04-23 15:08:55 +02:00
|
|
|
self = import ./treesitter.nix {inherit pkgs lib;};
|
2023-05-03 00:15:05 +02:00
|
|
|
mappingDefinitions = self.options.vim.treesitter.mappings;
|
|
|
|
mappings = addDescriptionsToMappings cfg.mappings mappingDefinitions;
|
2023-02-28 10:01:56 +01:00
|
|
|
in {
|
2023-04-17 22:27:27 +02:00
|
|
|
config = mkIf cfg.enable {
|
2024-03-15 12:19:11 +01:00
|
|
|
vim = {
|
|
|
|
startPlugins = ["nvim-treesitter"] ++ optional usingNvimCmp "cmp-treesitter";
|
|
|
|
|
|
|
|
autocomplete.sources = {"treesitter" = "[Treesitter]";};
|
2024-04-23 19:20:49 +02:00
|
|
|
treesitter.grammars = optionals cfg.addDefaultGrammars cfg.defaultGrammars;
|
2024-03-15 12:19:11 +01:00
|
|
|
|
|
|
|
maps = {
|
|
|
|
# HACK: Using mkSetLuaBinding and putting the lua code does not work for some reason: It just selects the whole file.
|
|
|
|
# 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 [
|
2024-05-02 12:37:44 +02:00
|
|
|
(mkSetBinding mappings.incrementalSelection.incrementByNode "<cmd>lua require('nvim-treesitter.incremental_selection').node_incremental()<CR>")
|
|
|
|
(mkSetBinding mappings.incrementalSelection.incrementByScope "<cmd>lua require('nvim-treesitter.incremental_selection').scope_incremental()<CR>")
|
|
|
|
(mkSetBinding mappings.incrementalSelection.decrementByNode "<cmd>lua require('nvim-treesitter.incremental_selection').node_decremental()<CR>")
|
2024-03-15 12:19:11 +01:00
|
|
|
];
|
|
|
|
};
|
2024-04-23 19:20:49 +02:00
|
|
|
|
2024-04-25 00:36:58 +02:00
|
|
|
# For some reason treesitter highlighting does not work on start if this is set before syntax on
|
2024-07-13 17:02:45 +02:00
|
|
|
# HACK: is there a way to convert the foldexpr line to lua?
|
|
|
|
luaConfigRC.treesitter-fold = mkIf cfg.fold (entryBefore ["basic"] ''
|
|
|
|
-- This is required by treesitter-context to handle folds
|
|
|
|
vim.o.foldmethod = "expr"
|
|
|
|
vim.cmd("set foldexpr=nvim_treesitter#foldexpr())"
|
|
|
|
|
|
|
|
-- This is optional, but is set rather as a sane default.
|
|
|
|
-- If unset, opened files will be folded by automatically as
|
|
|
|
-- the files are opened
|
|
|
|
vim.o.foldenable = false
|
2024-03-15 12:19:11 +01:00
|
|
|
'');
|
|
|
|
|
2024-04-24 22:34:39 +02:00
|
|
|
luaConfigRC.treesitter = entryAfter ["basic"] ''
|
2024-04-23 19:20:49 +02:00
|
|
|
require('nvim-treesitter.configs').setup {
|
2024-04-23 15:08:55 +02:00
|
|
|
-- Disable imperative treesitter options that would attempt to fetch
|
2024-04-23 15:24:00 +02:00
|
|
|
-- grammars into the read-only Nix store. To add additional grammars here
|
2024-04-23 15:08:55 +02:00
|
|
|
-- you must use the `config.vim.treesitter.grammars` option.
|
|
|
|
auto_install = false,
|
|
|
|
sync_install = false,
|
|
|
|
ensure_installed = {},
|
|
|
|
|
2024-04-23 19:20:49 +02:00
|
|
|
-- Indentation module for Treesitter
|
|
|
|
indent = {
|
2024-04-28 20:04:28 +02:00
|
|
|
enable = ${toLuaObject cfg.indent.enable},
|
|
|
|
disable = ${toLuaObject cfg.indent.disable},
|
2024-04-23 19:20:49 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
-- Highlight module for Treesitter
|
2024-03-15 12:19:11 +01:00
|
|
|
highlight = {
|
2024-04-28 20:04:28 +02:00
|
|
|
enable = ${toLuaObject cfg.highlight.enable},
|
2024-04-23 15:48:39 +02:00
|
|
|
disable = ${toLuaObject cfg.highlight.disable},
|
2024-04-28 20:04:28 +02:00
|
|
|
additional_vim_regex_highlighting = ${toLuaObject cfg.highlight.additionalVimRegexHighlighting},
|
2024-03-15 12:19:11 +01:00
|
|
|
},
|
|
|
|
|
2024-04-23 19:20:49 +02:00
|
|
|
-- Indentation module for Treesitter
|
2024-04-28 19:19:25 +02:00
|
|
|
-- Keymaps are set to false here as they are
|
|
|
|
-- handled by `vim.maps` entries calling lua
|
|
|
|
-- functions achieving the same functionality.
|
2024-03-15 12:19:11 +01:00
|
|
|
incremental_selection = {
|
2024-04-28 20:04:28 +02:00
|
|
|
enable = ${toLuaObject cfg.incrementalSelection.enable},
|
|
|
|
disable = ${toLuaObject cfg.incrementalSelection.disable},
|
2024-03-15 12:19:11 +01:00
|
|
|
keymaps = {
|
|
|
|
init_selection = false,
|
|
|
|
node_incremental = false,
|
|
|
|
scope_incremental = false,
|
|
|
|
node_decremental = false,
|
2024-04-28 20:04:28 +02:00
|
|
|
|
2024-03-15 12:19:11 +01:00
|
|
|
},
|
2023-02-28 10:01:56 +01:00
|
|
|
},
|
2024-03-15 12:19:11 +01:00
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
2023-04-17 22:27:27 +02:00
|
|
|
};
|
2023-02-28 10:01:56 +01:00
|
|
|
}
|