From 6cbd7eb9ae06398bb4b79621e25ceed880e03b1a Mon Sep 17 00:00:00 2001 From: Ade Attwood Date: Fri, 14 Jun 2024 18:03:07 +0100 Subject: [PATCH] feat(vim): add nvim-treesitter-textobjects I have been wanting to play with this plugin for a long time now. I have a minimal config that will give me most of what I need. Moving parameters and arguments is a big one. Also selecting parameters, this will also include the type definition so its much more powerful than selecting words. Jumping to functions has also been included, right now I am not sure how much that will be used. My muscle memory is very used to and for paging. --- modules/nvim.lua | 1 + .../core/files/vim/plugin/treesitter.lua | 39 +++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/modules/nvim.lua b/modules/nvim.lua index 144dfe6..6d31a53 100644 --- a/modules/nvim.lua +++ b/modules/nvim.lua @@ -39,6 +39,7 @@ local start_plugins = { ["nvim-lint"] = { url = "https://github.com/mfussenegger/nvim-lint.git" }, ["nvim-lspconfig"] = { url = "https://github.com/neovim/nvim-lspconfig.git" }, ["nvim-treesitter"] = { url = "https://github.com/nvim-treesitter/nvim-treesitter.git" }, + ["nvim-treesitter-textobjects"] = { url = "https://github.com/nvim-treesitter/nvim-treesitter-textobjects.git" }, ["vim-surround"] = { url = "https://github.com/tpope/vim-surround.git" }, ["vim-tmux-navigator"] = { url = "https://github.com/christoomey/vim-tmux-navigator.git" }, ["cmp-cmdline"] = { url = "https://github.com/hrsh7th/cmp-cmdline.git", revision = "main" }, diff --git a/site-modules/core/files/vim/plugin/treesitter.lua b/site-modules/core/files/vim/plugin/treesitter.lua index 3027f43..606f6d4 100644 --- a/site-modules/core/files/vim/plugin/treesitter.lua +++ b/site-modules/core/files/vim/plugin/treesitter.lua @@ -1,16 +1,49 @@ require("nvim-treesitter.configs").setup { auto_install = true, playground = { enable = true }, - indent = { enable = false }, + indent = { enable = true }, rainbow = { enable = true }, highlight = { -- `false` will disable the whole extension enable = true, - additional_vim_regex_highlighting = true, + additional_vim_regex_highlighting = false, + }, + textobjects = { + select = { + enable = true, + keymaps = { + ["af"] = "@function.outer", + ["if"] = "@function.inner", + ["aa"] = "@parameter.outer", -- this is also arguments + ["ia"] = "@parameter.inner", -- this is also arguments + ["ab"] = "@block.outer", + ["ib"] = "@block.inner", + ["ac"] = "@comment.outer", + ["ic"] = "@comment.inner", + }, + }, + move = { + enable = true, + set_jumps = true, + goto_next_start = { + ["]f"] = "@function.outer", + }, + goto_previous_start = { + ["[f"] = "@function.outer", + }, + }, + swap = { + enable = true, + swap_next = { + ["L"] = "@parameter.inner", + }, + swap_previous = { + ["H"] = "@parameter.inner", + }, + }, }, } - vim.opt.foldmethod = "expr" vim.opt.foldexpr = "nvim_treesitter#foldexpr()" vim.opt.foldenable = false