From 5a6cdb43d630eb31b74ef8f1295000711dc4e6e9 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Thu, 23 Nov 2023 20:15:16 +0200 Subject: [PATCH] Nvim: add treesitter text objects --- home/.config/nvim/lua/plugins/statuscol.lua | 4 +- home/.config/nvim/lua/plugins/treesitter.lua | 160 ++++++++++++++----- 2 files changed, 118 insertions(+), 46 deletions(-) diff --git a/home/.config/nvim/lua/plugins/statuscol.lua b/home/.config/nvim/lua/plugins/statuscol.lua index 1391671a..8e0c947c 100644 --- a/home/.config/nvim/lua/plugins/statuscol.lua +++ b/home/.config/nvim/lua/plugins/statuscol.lua @@ -9,8 +9,8 @@ return { config = function() local gitsigns = require("gitsigns") require("which-key").register({ - ["["] = { c = { gitsigns.prev_hunk, "Previous hunk" } }, - ["]"] = { c = { gitsigns.next_hunk, "Next hunk" } }, + ["["] = { h = { gitsigns.prev_hunk, "Previous hunk" } }, + ["]"] = { h = { gitsigns.next_hunk, "Next hunk" } }, }) local builtin = require("statuscol.builtin") diff --git a/home/.config/nvim/lua/plugins/treesitter.lua b/home/.config/nvim/lua/plugins/treesitter.lua index 7a04584e..35fc5328 100644 --- a/home/.config/nvim/lua/plugins/treesitter.lua +++ b/home/.config/nvim/lua/plugins/treesitter.lua @@ -1,48 +1,120 @@ return { - "nvim-treesitter/nvim-treesitter", - build = function() - require("nvim-treesitter.install").update({ with_sync = true }) - end, - ---@type TSConfig - opts = { - ensure_installed = { - "bash", - "css", - "dockerfile", - "git_config", - "git_rebase", - "gitattributes", - "gitcommit", - "gitignore", - "html", - "http", - "java", - "javascript", - "json", - "json5", - "latex", - "lua", - "make", - "markdown", - "markdown_inline", - "php", - "python", - "rasi", - "regex", - "rst", - "scss", - "toml", - "tsx", - "typescript", - "vim", - "yaml", + { + "nvim-treesitter/nvim-treesitter", + build = ":TSUpdate", + init = function(plugin) + require("nvim-treesitter.install").update({ with_sync = true }) + end, + ---@type TSConfig + ---@diagnostic disable-next-line: missing-fields + opts = { + highlight = { enable = true }, + indent = { enable = true }, + ensure_installed = { + "bash", + "css", + "diff", + "dockerfile", + "git_config", + "git_rebase", + "gitattributes", + "gitcommit", + "gitignore", + "html", + "http", + "java", + "javascript", + "jsdoc", + "json", + "json5", + "jsonc", + "latex", + "lua", + "luadoc", + "luap", + "make", + "markdown", + "markdown_inline", + "php", + "python", + "query", + "rasi", + "regex", + "rst", + "scss", + "toml", + "tsx", + "typescript", + "vim", + "vimdoc", + "yaml", + }, + incremental_selection = { + enable = true, + keymaps = { + init_selection = "", + node_incremental = "", + scope_incremental = false, + node_decremental = "", + }, + }, + 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 }, - indent = { enable = true }, - incremental_selection = { enable = true }, - context_commentstring = { enable = true }, - sync_install = true, - ignore_install = {}, - auto_install = true, + ---@param opts TSConfig + config = function(_, opts) + if type(opts.ensure_installed) == "table" then + ---@type table + local added = {} + opts.ensure_installed = vim.tbl_filter(function(lang) + 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 + 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 + 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 = {}, }, }