feat: add treesitter keybindings

This commit is contained in:
n3oney 2023-05-03 00:15:05 +02:00
parent 201a337a0d
commit 320cb8007c
No known key found for this signature in database
GPG key ID: C786693DE727850E
2 changed files with 30 additions and 17 deletions

View file

@ -1,5 +1,4 @@
{ {
pkgs,
config, config,
lib, lib,
... ...
@ -8,6 +7,11 @@ with lib;
with builtins; let with builtins; let
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;};
mappingDefinitions = self.options.vim.treesitter.mappings;
mappings = addDescriptionsToMappings cfg.mappings mappingDefinitions;
in { in {
config = mkIf cfg.enable { config = mkIf cfg.enable {
vim.startPlugins = vim.startPlugins =
@ -16,6 +20,16 @@ in {
vim.autocomplete.sources = {"treesitter" = "[Treesitter]";}; vim.autocomplete.sources = {"treesitter" = "[Treesitter]";};
# For some reason, using mkSetLuaBinding and putting the lua code does not work. It just selects the whole file.
# This works though, and if it ain't broke, don't fix it.
vim.maps.normal = mkSetBinding mappings.incrementalSelection.init ":lua require('nvim-treesitter.incremental_selection').init_selection()<CR>";
vim.maps.visualOnly = mkMerge [
(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.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"] '' vim.configRC.treesitter-fold = mkIf cfg.fold (nvim.dag.entryBefore ["basic"] ''
set foldmethod=expr set foldmethod=expr
@ -36,10 +50,10 @@ in {
incremental_selection = { incremental_selection = {
enable = true, enable = true,
keymaps = { keymaps = {
init_selection = "gnn", init_selection = false,
node_incremental = "grn", node_incremental = false,
scope_incremental = "grc", scope_incremental = false,
node_decremental = "grm", node_decremental = false,
}, },
}, },
} }

View file

@ -1,14 +1,5 @@
{ {lib, ...}:
pkgs, with lib; {
config,
lib,
...
}:
with lib;
with builtins; let
cfg = config.vim.treesitter;
usingNvimCmp = config.vim.autocomplete.enable && config.vim.autocomplete.type == "nvim-cmp";
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";
@ -16,13 +7,21 @@ in {
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 = with types; listOf package;
default = []; default = [];
description = nvim.nmd.asciiDoc '' description = nvim.nmd.asciiDoc ''
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
''; '';
}; };
}; };