]> git.rmz.io Git - dotfiles.git/blob - nvim/lua/plugins/treesitter.lua
nvim: add git related parsers to treesitter
[dotfiles.git] / nvim / lua / plugins / treesitter.lua
1 return {
2 { "nvim-treesitter/nvim-treesitter",
3 dependencies = { "nvim-treesitter/nvim-treesitter-textobjects" },
4 build = ":TSUpdate",
5 event = { "LazyFile", "VeryLazy" },
6 lazy = vim.fn.argc(-1) == 0, -- load treesitter early when opening a file from the cmdline
7 init = function(plugin)
8 -- PERF: add nvim-treesitter queries to the rtp and it's custom query predicates early
9 -- This is needed because a bunch of plugins no longer `require("nvim-treesitter")`, which
10 -- no longer trigger the **nvim-treesitter** module to be loaded in time.
11 -- Luckily, the only things that those plugins need are the custom queries, which we make available
12 -- during startup.
13 require("lazy.core.loader").add_to_rtp(plugin)
14 require("nvim-treesitter.query_predicates")
15 end,
16 cmd = { "TSUpdateSync", "TSUpdate", "TSInstall" },
17 keys = {
18 { "<c-space>", desc = "Increment Selection" },
19 { "<bs>", desc = "Decrement Selection", mode = "x" },
20 },
21 opts_extend = { "ensure_installed" },
22 ---@type TSConfig
23 ---@diagnostic disable-next-line: missing-fields
24 opts = {
25 highlight = { enable = true },
26 indent = { enable = true },
27 ensure_installed = {
28 "bash",
29 "c",
30 "diff",
31 "git_config",
32 "git_rebase",
33 "gitattributes",
34 "gitcommit",
35 "gitignore",
36 "html",
37 "javascript",
38 "jsdoc",
39 "json",
40 "jsonc",
41 "lua",
42 "luadoc",
43 "luap",
44 "markdown",
45 "markdown_inline",
46 "printf",
47 "python",
48 "query",
49 "regex",
50 "toml",
51 "tsx",
52 "typescript",
53 "vim",
54 "vimdoc",
55 "xml",
56 "yaml",
57 },
58 incremental_selection = {
59 enable = true,
60 keymaps = {
61 init_selection = "<C-space>",
62 node_incremental = "<C-space>",
63 scope_incremental = false,
64 node_decremental = "<bs>",
65 },
66 },
67 textobjects = {
68 move = {
69 enable = true,
70 goto_next_start = { ["]f"] = "@function.outer", ["]c"] = "@class.outer", ["]a"] = "@parameter.inner" },
71 goto_next_end = { ["]F"] = "@function.outer", ["]C"] = "@class.outer", ["]A"] = "@parameter.inner" },
72 goto_previous_start = { ["[f"] = "@function.outer", ["[c"] = "@class.outer", ["[a"] = "@parameter.inner" },
73 goto_previous_end = { ["[F"] = "@function.outer", ["[C"] = "@class.outer", ["[A"] = "@parameter.inner" },
74 },
75 },
76 },
77 ---@param opts TSConfig
78 config = function(_, opts)
79 -- make sure extra langs are not duplicated
80 opts.ensure_installed = rmz.dedup(opts.ensure_installed)
81
82 local configs = require("nvim-treesitter.configs")
83 configs.setup(opts)
84
85 -- When in diff mode, we want to use the default
86 -- vim text objects c & C instead of the treesitter ones.
87 local move = require("nvim-treesitter.textobjects.move") ---@type table<string,fun(...)>
88 for name, fn in pairs(move) do
89 if name:find("goto") == 1 then
90 move[name] = function(q, ...)
91 if not vim.wo.diff then return fn(q, ...) end
92
93 local config = configs.get_module("textobjects.move")[name] ---@type table<string,string>
94 for key, query in pairs(config or {}) do
95 if q == query and key:find("[%]%[][cC]") then
96 vim.cmd("normal! " .. key)
97 return
98 end
99 end
100 end
101 end
102 end
103 end,
104 },
105
106 -- Automatically add closing tags for HTML and JSX
107 {
108 "windwp/nvim-ts-autotag",
109 event = "LazyFile",
110 opts = {},
111 },
112 }
113