nvim: add treesitter textobjects

This commit is contained in:
2026-04-17 23:27:10 +02:00
parent ac0480b08e
commit e06b7d94ac
2 changed files with 142 additions and 74 deletions

View File

@@ -15,70 +15,134 @@ vim.api.nvim_create_autocmd("PackChanged", {
})
vim.pack.add({
{ src = "https://github.com/nvim-treesitter/nvim-treesitter-textobjects", version = "master" },
{ src = "https://github.com/nvim-treesitter/nvim-treesitter", version = "master" },
{ src = "https://github.com/nvim-treesitter/nvim-treesitter-textobjects", version = "main" },
{ src = "https://github.com/nvim-treesitter/nvim-treesitter", version = "main" },
})
local ok, configs = pcall(require, "nvim-treesitter.configs")
local ok = pcall(require, "nvim-treesitter")
if ok then
configs.setup({
highlight = {
enable = true,
},
textobjects = {
select = {
enable = true,
lookahead = true,
keymaps = {
["aa"] = "@parameter.outer",
["ia"] = "@parameter.inner",
["af"] = "@function.outer",
["if"] = "@function.inner",
["ac"] = "@class.outer",
["ic"] = "@class.inner",
["al"] = "@call.outer",
["il"] = "@call.inner",
["ao"] = "@loop.outer",
["io"] = "@loop.inner",
["ad"] = "@conditional.outer",
["id"] = "@conditional.inner",
["ar"] = "@return.outer",
["ir"] = "@return.inner",
["as"] = "@statement.outer",
["ag"] = "@assignment.outer",
["ig"] = "@assignment.inner",
},
},
move = {
enable = true,
set_jumps = true,
goto_next_start = {
["]m"] = "@function.outer",
["]]"] = "@class.outer",
},
goto_next_end = {
["]M"] = "@function.outer",
["]["] = "@class.outer",
},
goto_previous_start = {
["[m"] = "@function.outer",
["[["] = "@class.outer",
},
goto_previous_end = {
["[M"] = "@function.outer",
["[]"] = "@class.outer",
},
},
swap = {
enable = true,
swap_next = {
["<leader>cp"] = "@parameter.inner",
},
swap_previous = {
["<leader>cP"] = "@parameter.inner",
},
},
},
vim.api.nvim_create_autocmd('FileType', {
pattern = { 'odin', 'go' },
callback = function() vim.treesitter.start() end,
})
else
print("plugin nvim-treesitter.configs missing")
end
local textobjects
ok, textobjects = pcall(require, "nvim-treesitter-textobjects")
if ok then
textobjects.setup {
move = {
set_jumps = true,
},
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,
},
}
local function key_select(key, object)
vim.keymap.set({ "x", "o" }, key, function()
require "nvim-treesitter-textobjects.select".select_textobject(object, "textobjects")
end)
end
key_select("aa", "@parameter.outer")
key_select("ia", "@parameter.inner")
key_select("af", "@function.outer")
key_select("if", "@function.inner")
key_select("ac", "@class.outer")
key_select("ic", "@class.inner")
key_select("al", "@call.outer")
key_select("il", "@call.inner")
key_select("ao", "@loop.outer")
key_select("io", "@loop.inner")
key_select("ad", "@conditional.outer")
key_select("id", "@conditional.inner")
key_select("ar", "@return.outer")
key_select("ir", "@return.inner")
key_select("as", "@statement.outer")
key_select("ag", "@assignment.outer")
key_select("ig", "@assignment.inner")
vim.keymap.set({ "x", "o" }, "ax", function()
require "nvim-treesitter-textobjects.select".select_textobject("@local.scope", "locals")
end)
vim.keymap.set("n", "<leader>cp", function()
require("nvim-treesitter-textobjects.swap").swap_next "@parameter.inner"
end)
vim.keymap.set("n", "<leader>cP", function()
require("nvim-treesitter-textobjects.swap").swap_previous "@parameter.inner"
end)
vim.keymap.set({ "n", "x", "o" }, "]m", function()
require("nvim-treesitter-textobjects.move").goto_next_start("@function.outer", "textobjects")
end)
vim.keymap.set({ "n", "x", "o" }, "]]", function()
require("nvim-treesitter-textobjects.move").goto_next_start("@class.outer", "textobjects")
end)
-- You can also pass a list to group multiple queries.
vim.keymap.set({ "n", "x", "o" }, "]o", function()
require("nvim-treesitter-textobjects.move").goto_next_start({ "@loop.inner", "@loop.outer" },
"textobjects")
end)
-- You can also use captures from other query groups like `locals.scm` or `folds.scm`
vim.keymap.set({ "n", "x", "o" }, "]s", function()
require("nvim-treesitter-textobjects.move").goto_next_start("@local.scope", "locals")
end)
vim.keymap.set({ "n", "x", "o" }, "]z", function()
require("nvim-treesitter-textobjects.move").goto_next_start("@fold", "folds")
end)
vim.keymap.set({ "n", "x", "o" }, "]M", function()
require("nvim-treesitter-textobjects.move").goto_next_end("@function.outer", "textobjects")
end)
vim.keymap.set({ "n", "x", "o" }, "][", function()
require("nvim-treesitter-textobjects.move").goto_next_end("@class.outer", "textobjects")
end)
vim.keymap.set({ "n", "x", "o" }, "[m", function()
require("nvim-treesitter-textobjects.move").goto_previous_start("@function.outer", "textobjects")
end)
vim.keymap.set({ "n", "x", "o" }, "[[", function()
require("nvim-treesitter-textobjects.move").goto_previous_start("@class.outer", "textobjects")
end)
vim.keymap.set({ "n", "x", "o" }, "[M", function()
require("nvim-treesitter-textobjects.move").goto_previous_end("@function.outer", "textobjects")
end)
vim.keymap.set({ "n", "x", "o" }, "[]", function()
require("nvim-treesitter-textobjects.move").goto_previous_end("@class.outer", "textobjects")
end)
-- Go to either the start or the end, whichever is closer.
-- Use if you want more granular movements
vim.keymap.set({ "n", "x", "o" }, "]d", function()
require("nvim-treesitter-textobjects.move").goto_next("@conditional.outer", "textobjects")
end)
vim.keymap.set({ "n", "x", "o" }, "[d", function()
require("nvim-treesitter-textobjects.move").goto_previous("@conditional.outer", "textobjects")
end)
else
print("plugin nvim-treesitter-textobjects missing")
end