From f3ce98466e4fd0534ee9a810522de994f419ae74 Mon Sep 17 00:00:00 2001 From: Kevin MacMartin Date: Tue, 5 Mar 2024 12:46:26 -0500 Subject: [PATCH] Add nvim-treesitter-textobjects with some friendly keybindings --- .gitmodules | 3 ++ readme.md | 1 + vim/bundle/nvim-treesitter-textobjects | 1 + vim/config/plugins/nvim-treesitter.lua | 53 +++++++++++++++++++++++++- 4 files changed, 57 insertions(+), 1 deletion(-) create mode 160000 vim/bundle/nvim-treesitter-textobjects diff --git a/.gitmodules b/.gitmodules index f2f367e..ab7d5a3 100644 --- a/.gitmodules +++ b/.gitmodules @@ -91,3 +91,6 @@ [submodule "vim/bundle/cmp-async-path"] path = vim/bundle/cmp-async-path url = https://codeberg.org/FelipeLema/cmp-async-path +[submodule "vim/bundle/nvim-treesitter-textobjects"] + path = vim/bundle/nvim-treesitter-textobjects + url = https://github.com/nvim-treesitter/nvim-treesitter-textobjects diff --git a/readme.md b/readme.md index b91a4d8..38b2581 100644 --- a/readme.md +++ b/readme.md @@ -130,6 +130,7 @@ For a complete list of mappings specific to **darkcloud-nvimconfig**, check the * [nvim-snippy](https://github.com/dcampos/nvim-snippy): Snippet plugin for Neovim written in Lua * [vim-snippets](https://github.com/honza/vim-snippets): Snippets files for various programming languages * [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter): Treesitter configurations and abstraction layer for Neovim + * [nvim-treesitter-textobjects](https://github.com/nvim-treesitter/nvim-treesitter-textobjects): Syntax aware text-objects, select, move, swap, and peek support * [ReplaceWithRegister](https://github.com/vim-scripts/ReplaceWithRegister): Replace text with the contents of a register (for paste+replace without writing over the buffer) * [splitjoin.vim](https://github.com/AndrewRadev/splitjoin.vim): Simplifies the transition between multiline and single-line code * [tabular](https://github.com/godlygeek/tabular): Vim script for text filtering and alignment diff --git a/vim/bundle/nvim-treesitter-textobjects b/vim/bundle/nvim-treesitter-textobjects new file mode 160000 index 0000000..b7a0bfa --- /dev/null +++ b/vim/bundle/nvim-treesitter-textobjects @@ -0,0 +1 @@ +Subproject commit b7a0bfa3e93697ca5b61b15df633404bf8f45883 diff --git a/vim/config/plugins/nvim-treesitter.lua b/vim/config/plugins/nvim-treesitter.lua index 5eb5951..74d3ad5 100644 --- a/vim/config/plugins/nvim-treesitter.lua +++ b/vim/config/plugins/nvim-treesitter.lua @@ -19,6 +19,57 @@ if (vim.g.enabletreesitter == 1) then enable = true, disable = {}, additional_vim_regex_highlighting = false, - } + }, + + textobjects = { + select = { + enable = true, + lookahead = true, -- Automatically jump forward to textobj + + keymaps = { + [ "ac" ] = { query = "@class.outer", desc = "Select outer part of a class region" }, + [ "ic" ] = { query = "@class.inner", desc = "Select inner part of a class region" }, + [ "af" ] = { query = "@function.outer", desc = "Select outer part of a function region" }, + [ "if" ] = { query = "@function.inner", desc = "Select inner part of a function region" }, + [ "as" ] = { query = "@scope", query_group = "locals", desc = "Select language scope" }, + }, + + selection_modes = { + [ "@parameter.outer" ] = "v", + [ "@parameter.inner" ] = "v", + [ "@class.inner" ] = "V", + [ "@class.outer" ] = "V", + [ "@function.outer" ] = "V", + [ "@function.inner" ] = "V", + }, + + include_surrounding_whitespace = false, + }, + + move = { + enable = true, + set_jumps = true, -- whether to set jumps in the jumplist + + goto_next_start = { + [ "]c" ] = { query = "@class.outer", desc = "Next class start" }, + [ "]f" ] = { query = "@function.outer", desc = "Next function start" }, + }, + + goto_next_end = { + [ "]C" ] = { query = "@class.outer", desc = "Next class end" }, + [ "]F" ] = { query = "@function.outer", desc = "Next function end" }, + }, + + goto_previous_start = { + [ "[c" ] = { query = "@class.outer", desc = "Prevoius class start" }, + [ "[f" ] = { query = "@function.outer", desc = "Previous function start" }, + }, + + goto_previous_end = { + [ "[C" ] = { query = "@class.outer", desc = "Previous class end" }, + [ "[F" ] = { query = "@function.outer", desc = "Previous function end" }, + }, + }, + }, } end