|
|
local M = {}
|
|
|
|
|
|
M.install = { src = "https://github.com/nvim-treesitter/nvim-treesitter-textobjects", version = "main" }
|
|
|
|
|
|
M.setup = function()
|
|
|
-- configuration
|
|
|
require("nvim-treesitter-textobjects").setup {
|
|
|
select = {
|
|
|
-- Automatically jump forward to textobj, similar to targets.vim
|
|
|
lookahead = true,
|
|
|
-- 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 '<c-v>') or a table
|
|
|
-- mapping query_strings to modes.
|
|
|
selection_modes = {
|
|
|
['@parameter.outer'] = 'v', -- charwise
|
|
|
['@function.outer'] = 'V', -- linewise
|
|
|
-- ['@class.outer'] = '<c-v>', -- blockwise
|
|
|
},
|
|
|
-- 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 of false
|
|
|
include_surrounding_whitespace = false,
|
|
|
},
|
|
|
move = {
|
|
|
-- whether to set jumps in the jumplist
|
|
|
set_jumps = true,
|
|
|
},
|
|
|
}
|
|
|
-- Select textobjects
|
|
|
local select = require('nvim-treesitter-textobjects.select')
|
|
|
vim.keymap.set({'n', 'x', 'o'}, 'af', function() select.select_textobject('@function.outer', 'textobjects') end)
|
|
|
vim.keymap.set({'n', 'x', 'o'}, 'if', function() select.select_textobject('@function.inner', 'textobjects') end)
|
|
|
vim.keymap.set({'n', 'x', 'o'}, 'ac', function() select.select_textobject('@conditional.outer', 'textobjects') end)
|
|
|
vim.keymap.set({'n', 'x', 'o'}, 'ic', function() select.select_textobject('@conditional.inner', 'textobjects') end)
|
|
|
vim.keymap.set({'n', 'x', 'o'}, 'al', function() select.select_textobject('@loop.outer', 'textobjects') end)
|
|
|
vim.keymap.set({'n', 'x', 'o'}, 'il', function() select.select_textobject('@loop.inner', 'textobjects') end)
|
|
|
|
|
|
-- Move textobjects
|
|
|
local move = require('nvim-treesitter-textobjects.move')
|
|
|
vim.keymap.set({'n', 'x', 'o'}, ']f', function() move.goto_next_start('@function.outer', 'textobjects') end)
|
|
|
vim.keymap.set({'n', 'x', 'o'}, ']c', function() move.goto_next_start('@conditional.outer', 'textobjects') end)
|
|
|
vim.keymap.set({'n', 'x', 'o'}, ']o', function() move.goto_next_start('@loop.outer', 'textobjects') end)
|
|
|
vim.keymap.set({'n', 'x', 'o'}, ']b', function() move.goto_next_start('@block.outer', 'textobjects') end)
|
|
|
|
|
|
vim.keymap.set({'n', 'x', 'o'}, ']F', function() move.goto_next_end('@function.outer', 'textobjects') end)
|
|
|
vim.keymap.set({'n', 'x', 'o'}, ']C', function() move.goto_next_end('@conditional.outer', 'textobjects') end)
|
|
|
vim.keymap.set({'n', 'x', 'o'}, ']O', function() move.goto_next_end('@loop.outer', 'textobjects') end)
|
|
|
|
|
|
vim.keymap.set({'n', 'x', 'o'}, '[f', function() move.goto_previous_start('@function.outer', 'textobjects') end)
|
|
|
vim.keymap.set({'n', 'x', 'o'}, '[c', function() move.goto_previous_start('@conditional.outer', 'textobjects') end)
|
|
|
vim.keymap.set({'n', 'x', 'o'}, '[o', function() move.goto_previous_start('@loop.outer', 'textobjects') end)
|
|
|
vim.keymap.set({'n', 'x', 'o'}, '[b', function() move.goto_previous_start('@block.outer', 'textobjects') end)
|
|
|
|
|
|
vim.keymap.set({'n', 'x', 'o'}, '[F', function() move.goto_previous_end('@function.outer', 'textobjects') end)
|
|
|
vim.keymap.set({'n', 'x', 'o'}, '[C', function() move.goto_previous_end('@conditional.outer', 'textobjects') end)
|
|
|
vim.keymap.set({'n', 'x', 'o'}, '[O', function() move.goto_previous_end('@loop.outer', 'textobjects') end)
|
|
|
|
|
|
-- Make movements repeatable with ; and ,
|
|
|
local ts_repeat_move = require('nvim-treesitter-textobjects.repeatable_move')
|
|
|
vim.keymap.set({'n', 'x', 'o'}, ';', ts_repeat_move.repeat_last_move_next)
|
|
|
vim.keymap.set({'n', 'x', 'o'}, ',', ts_repeat_move.repeat_last_move_previous)
|
|
|
|
|
|
-- Make builtin f, F, t, T also repeatable
|
|
|
vim.keymap.set({'n', 'x', 'o'}, 'f', ts_repeat_move.builtin_f_expr, { expr = true })
|
|
|
vim.keymap.set({'n', 'x', 'o'}, 'F', ts_repeat_move.builtin_F_expr, { expr = true })
|
|
|
vim.keymap.set({'n', 'x', 'o'}, 't', ts_repeat_move.builtin_t_expr, { expr = true })
|
|
|
vim.keymap.set({'n', 'x', 'o'}, 'T', ts_repeat_move.builtin_T_expr, { expr = true })
|
|
|
end
|
|
|
|
|
|
return M
|