Nvim: add treesitter text objects

This commit is contained in:
Marko Korhonen 2023-11-23 20:15:16 +02:00
parent f4c7e18482
commit 5d47465266
Signed by: FunctionalHacker
GPG key ID: A7F78BCB859CD890
2 changed files with 118 additions and 46 deletions

View file

@ -9,8 +9,8 @@ return {
config = function() config = function()
local gitsigns = require("gitsigns") local gitsigns = require("gitsigns")
require("which-key").register({ require("which-key").register({
["["] = { c = { gitsigns.prev_hunk, "Previous hunk" } }, ["["] = { h = { gitsigns.prev_hunk, "Previous hunk" } },
["]"] = { c = { gitsigns.next_hunk, "Next hunk" } }, ["]"] = { h = { gitsigns.next_hunk, "Next hunk" } },
}) })
local builtin = require("statuscol.builtin") local builtin = require("statuscol.builtin")

View file

@ -1,48 +1,120 @@
return { return {
"nvim-treesitter/nvim-treesitter", {
build = function() "nvim-treesitter/nvim-treesitter",
require("nvim-treesitter.install").update({ with_sync = true }) build = ":TSUpdate",
end, init = function(plugin)
---@type TSConfig require("nvim-treesitter.install").update({ with_sync = true })
opts = { end,
ensure_installed = { ---@type TSConfig
"bash", ---@diagnostic disable-next-line: missing-fields
"css", opts = {
"dockerfile", highlight = { enable = true },
"git_config", indent = { enable = true },
"git_rebase", ensure_installed = {
"gitattributes", "bash",
"gitcommit", "css",
"gitignore", "diff",
"html", "dockerfile",
"http", "git_config",
"java", "git_rebase",
"javascript", "gitattributes",
"json", "gitcommit",
"json5", "gitignore",
"latex", "html",
"lua", "http",
"make", "java",
"markdown", "javascript",
"markdown_inline", "jsdoc",
"php", "json",
"python", "json5",
"rasi", "jsonc",
"regex", "latex",
"rst", "lua",
"scss", "luadoc",
"toml", "luap",
"tsx", "make",
"typescript", "markdown",
"vim", "markdown_inline",
"yaml", "php",
"python",
"query",
"rasi",
"regex",
"rst",
"scss",
"toml",
"tsx",
"typescript",
"vim",
"vimdoc",
"yaml",
},
incremental_selection = {
enable = true,
keymaps = {
init_selection = "<C-space>",
node_incremental = "<C-space>",
scope_incremental = false,
node_decremental = "<bs>",
},
},
textobjects = {
move = {
enable = true,
goto_next_start = { ["]f"] = "@function.outer", ["]c"] = "@class.outer" },
goto_next_end = { ["]F"] = "@function.outer", ["]C"] = "@class.outer" },
goto_previous_start = { ["[f"] = "@function.outer", ["[c"] = "@class.outer" },
goto_previous_end = { ["[F"] = "@function.outer", ["[C"] = "@class.outer" },
},
},
}, },
highlight = { enable = true }, ---@param opts TSConfig
indent = { enable = true }, config = function(_, opts)
incremental_selection = { enable = true }, if type(opts.ensure_installed) == "table" then
context_commentstring = { enable = true }, ---@type table<string, boolean>
sync_install = true, local added = {}
ignore_install = {}, opts.ensure_installed = vim.tbl_filter(function(lang)
auto_install = true, if added[lang] then
return false
end
added[lang] = true
return true
end, opts.ensure_installed)
end
require("nvim-treesitter.configs").setup(opts)
end,
dependencies = {
{
"nvim-treesitter/nvim-treesitter-textobjects",
config = function()
-- When in diff mode, we want to use the default
-- vim text objects c & C instead of the treesitter ones.
local move = require("nvim-treesitter.textobjects.move") ---@type table<string,fun(...)>
local configs = require("nvim-treesitter.configs")
for name, fn in pairs(move) do
if name:find("goto") == 1 then
move[name] = function(q, ...)
if vim.wo.diff then
local config = configs.get_module("textobjects.move")[name] ---@type table<string,string>
for key, query in pairs(config or {}) do
if q == query and key:find("[%]%[][cC]") then
vim.cmd("normal! " .. key)
return
end
end
end
return fn(q, ...)
end
end
end
end,
},
},
},
-- Automatically add closing tags for HTML and JSX
{
"windwp/nvim-ts-autotag",
opts = {},
}, },
} }