dotfiles/home/.config/nvim/lua/plugins/treesitter.lua

154 lines
4.2 KiB
Lua
Raw Normal View History

return {
2023-11-25 14:37:36 +02:00
-- Improved syntax highlighting, text objects and more
--- @type LazyPluginSpec
2023-11-23 20:15:16 +02:00
{
"nvim-treesitter/nvim-treesitter",
build = ":TSUpdate",
2023-11-25 14:37:36 +02:00
init = function()
require("nvim-treesitter.install").update({
with_sync = true,
})
2023-11-23 20:15:16 +02:00
end,
2023-11-25 14:37:36 +02:00
--- @type TSConfig
2023-11-23 20:15:16 +02:00
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 = "<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" },
},
select = {
enable = true,
lookahead = true,
keymaps = {
["af"] = {
query = "@function.outer",
desc = "Select outer part of a function",
},
["if"] = {
query = "@function.inner",
desc = "Select inner part of a function",
},
["ac"] = {
query = "@class.outer",
desc = "Select outer part of a class",
},
["ic"] = {
query = "@class.inner",
desc = "Select inner part of a class",
},
["as"] = {
query = "@scope",
query_group = "locals",
desc = "Select language scope",
},
},
},
2023-11-23 20:15:16 +02:00
},
},
2023-11-25 14:37:36 +02:00
--- @param opts TSConfig
2023-11-23 20:15:16 +02:00
config = function(_, opts)
if type(opts.ensure_installed) == "table" then
2023-11-25 14:37:36 +02:00
--- @type table<string, boolean>
2023-11-23 20:15:16 +02:00
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.
2023-11-25 14:37:36 +02:00
--- @type table<string,fun(...)>
local move = require("nvim-treesitter.textobjects.move")
2023-11-23 20:15:16 +02:00
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
2023-11-25 14:37:36 +02:00
--- @type table<string,string>
local config = configs.get_module("textobjects.move")[name]
2023-11-23 20:15:16 +02:00
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
2023-11-25 14:37:36 +02:00
--- @type LazyPluginSpec
2023-11-23 20:15:16 +02:00
{
"windwp/nvim-ts-autotag",
2023-11-25 14:37:36 +02:00
config = true,
},
}