From 360c8b155e5ae7001486a69c91f73660bbf2c464 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Fri, 8 Dec 2023 16:06:00 +0100 Subject: [PATCH] refactor: treesitter config --- .../nvim/plugins/lua/nvim-treesitter.lua | 11 +--- modules/programs/nvim/plugins/treesitter.nix | 58 ++++++++++++++++--- 2 files changed, 50 insertions(+), 19 deletions(-) diff --git a/modules/programs/nvim/plugins/lua/nvim-treesitter.lua b/modules/programs/nvim/plugins/lua/nvim-treesitter.lua index e221fdd..4adea21 100644 --- a/modules/programs/nvim/plugins/lua/nvim-treesitter.lua +++ b/modules/programs/nvim/plugins/lua/nvim-treesitter.lua @@ -1,10 +1 @@ -require("nvim-treesitter.configs").setup({ - sync_install = false, - auto_install = false, - highlight = { - enable = true, - }, - context_commentstring = { - enable = true, - }, -}) +require("nvim-treesitter.configs").setup() diff --git a/modules/programs/nvim/plugins/treesitter.nix b/modules/programs/nvim/plugins/treesitter.nix index b76bcbc..29fd25d 100644 --- a/modules/programs/nvim/plugins/treesitter.nix +++ b/modules/programs/nvim/plugins/treesitter.nix @@ -6,22 +6,62 @@ with builtins; { plugin = nvim-treesitter; event = [ "BufReadPost" "BufNewFile" ]; + opts = { + sync_install = false; + auto_install = false; + highlight = { + enable = true; + }; + context_commentstring = { + enable = true; + }; + textobjects = + { + select = + { + enable = true; + + # Automatically jump forward to textobj, similar to targets.vim + lookahead = false; + + keymaps = { + # You can use the capture groups defined in textobjects.scm + "af" = { + query = "@function.outer"; + desc = "Select outer part of a function region"; + }; + "if" = { + query = "@function.inner"; + desc = "Select inner part of a function region"; + }; + "ac" = { + query = "@class.outer"; + desc = "Select outer part of a class region"; + }; + "ic" = { + query = "@class.inner"; + desc = "Select inner part of a class region"; + }; + }; + }; + }; + }; conf = let - parserDir = pkgs.symlinkJoin { - name = "tresitter-grammars-all"; - paths = lib.attrValues (lib.filterAttrs (_: builtins.isAttrs) nvim-treesitter-parsers); - }; + parserDir = pkgs.symlinkJoin + { + name = "tresitter-grammars-all"; + paths = lib.attrValues (lib.filterAttrs (_: builtins.isAttrs) nvim-treesitter-parsers); + }; in - readFile ./lua/nvim-treesitter.lua + '' + '' vim.opt.runtimepath:append("${parserDir}") - - require'nvim-treesitter.configs'.setup { - parser_install_dir = "${parserDir}", - } + local final_opts = vim.tbl_deep_extend("keep", opts, { parser_install_dir = "${parserDir}" }) + require('nvim-treesitter.configs').setup(final_opts) ''; dependencies = [ { plugin = nvim-ts-context-commentstring; } + { plugin = nvim-treesitter-textobjects; } ]; } {