]> git.rmz.io Git - dotfiles.git/blob - nvim/lua/plugins/treesitter.lua
96e8c1a415a60bf5b328e7c9f55063ea29efbab8
[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 "html",
32 "javascript",
33 "jsdoc",
34 "json",
35 "jsonc",
36 "lua",
37 "luadoc",
38 "luap",
39 "markdown",
40 "markdown_inline",
41 "printf",
42 "python",
43 "query",
44 "regex",
45 "toml",
46 "tsx",
47 "typescript",
48 "vim",
49 "vimdoc",
50 "xml",
51 "yaml",
52 },
53 incremental_selection = {
54 enable = true,
55 keymaps = {
56 init_selection = "<C-space>",
57 node_incremental = "<C-space>",
58 scope_incremental = false,
59 node_decremental = "<bs>",
60 },
61 },
62 textobjects = {
63 move = {
64 enable = true,
65 goto_next_start = { ["]f"] = "@function.outer", ["]c"] = "@class.outer", ["]a"] = "@parameter.inner" },
66 goto_next_end = { ["]F"] = "@function.outer", ["]C"] = "@class.outer", ["]A"] = "@parameter.inner" },
67 goto_previous_start = { ["[f"] = "@function.outer", ["[c"] = "@class.outer", ["[a"] = "@parameter.inner" },
68 goto_previous_end = { ["[F"] = "@function.outer", ["[C"] = "@class.outer", ["[A"] = "@parameter.inner" },
69 },
70 },
71 },
72 ---@param opts TSConfig
73 config = function(_, opts)
74 -- make sure extra langs are not duplicated
75 opts.ensure_installed = rmz.dedup(opts.ensure_installed)
76
77 local configs = require("nvim-treesitter.configs")
78 configs.setup(opts)
79
80 -- When in diff mode, we want to use the default
81 -- vim text objects c & C instead of the treesitter ones.
82 local move = require("nvim-treesitter.textobjects.move") ---@type table<string,fun(...)>
83 for name, fn in pairs(move) do
84 if name:find("goto") == 1 then
85 move[name] = function(q, ...)
86 if not vim.wo.diff then return fn(q, ...) end
87
88 local config = configs.get_module("textobjects.move")[name] ---@type table<string,string>
89 for key, query in pairs(config or {}) do
90 if q == query and key:find("[%]%[][cC]") then
91 vim.cmd("normal! " .. key)
92 return
93 end
94 end
95 end
96 end
97 end
98 end,
99 },
100
101 -- Automatically add closing tags for HTML and JSX
102 {
103 "windwp/nvim-ts-autotag",
104 event = "LazyFile",
105 opts = {},
106 },
107 }
108