From ab059bbb6d440322af791b93024d1711e4d13b28 Mon Sep 17 00:00:00 2001 From: jdhao Date: Sun, 16 Feb 2025 20:15:41 +0100 Subject: [PATCH] use treesitter-textobjects instead (#380) Note that we should keep the branch of nvim-treesitter and nvim-treesitter-textobjects the same. `main` is targeted at nightly neovim and `master` is for stable neovim. See also https://github.com/nvim-treesitter/nvim-treesitter/issues/4767 --- lua/config/treesitter-textobjects.lua | 44 +++++++++++++++++++++++++++ lua/plugin_specs.lua | 12 +++++--- 2 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 lua/config/treesitter-textobjects.lua diff --git a/lua/config/treesitter-textobjects.lua b/lua/config/treesitter-textobjects.lua new file mode 100644 index 0000000..32e9e5f --- /dev/null +++ b/lua/config/treesitter-textobjects.lua @@ -0,0 +1,44 @@ +require("nvim-treesitter.configs").setup { + textobjects = { + select = { + enable = true, + + -- Automatically jump forward to textobj, similar to targets.vim + lookahead = true, + + keymaps = { + -- You can use the capture groups defined in textobjects.scm + ["af"] = "@function.outer", + ["if"] = "@function.inner", + ["ac"] = "@class.outer", + -- You can optionally set descriptions to the mappings (used in the desc parameter of + -- nvim_buf_set_keymap) which plugins like which-key display + ["ic"] = { query = "@class.inner", desc = "Select inner part of a class region" }, + }, + -- You can choose the select mode (default is charwise 'v') + -- + -- Can also be a function which gets passed a table with the keys + -- * query_string: eg '@function.inner' + -- * method: eg 'v' or 'o' + -- and should return the mode ('v', 'V', or '') or a table + -- mapping query_strings to modes. + selection_modes = { + ["@function.inner"] = "V", -- linewise + ["@function.outer"] = "V", -- linewise + ["@class.outer"] = "V", -- linewise + ["@class.inner"] = "V", -- linewise + ["@parameter.outer"] = "v", -- charwise + }, + -- If you set this to `true` (default is `false`) then any textobject is + -- extended to include preceding or succeeding whitespace. Succeeding + -- whitespace has priority in order to act similarly to eg the built-in + -- `ap`. + -- + -- Can also be a function which gets passed a table with the keys + -- * query_string: eg '@function.inner' + -- * selection_mode: eg 'v' + -- and should return true or false + include_surrounding_whitespace = false, + }, + }, +} diff --git a/lua/plugin_specs.lua b/lua/plugin_specs.lua index 3a07173..9342b6d 100644 --- a/lua/plugin_specs.lua +++ b/lua/plugin_specs.lua @@ -61,10 +61,14 @@ local plugin_specs = { require("config.treesitter") end, }, - - -- Python-related text object - { "jeetsukumaran/vim-pythonsense", ft = { "python" } }, - + { + "nvim-treesitter/nvim-treesitter-textobjects", + event = "VeryLazy", + branch = "master", + config = function() + require("config.treesitter-textobjects") + end, + }, { "machakann/vim-swap", event = "VeryLazy" }, -- IDE for Lisp