feat(extras): added extra for the nvim-treesitter rewrite. Some plugins are not compatible and will be disabled.

This commit is contained in:
Folke Lemaitre 2024-05-16 20:52:53 +02:00
parent 66dc9c09d6
commit 20081460b6
2 changed files with 108 additions and 0 deletions

View file

@ -0,0 +1,93 @@
-- backwards compatibility with the old treesitter config for adding custom parsers
local function patch()
local parsers = require("nvim-treesitter.parsers")
parsers.get_parser_configs = setmetatable({}, {
__call = function()
return parsers
end,
})
end
if vim.fn.executable("tree-sitter") == 0 then
LazyVim.error("**treesitter-rewrite** requires the `tree-sitter` executable to be installed")
return {}
end
return {
{
"nvim-treesitter/nvim-treesitter",
version = false, -- last release is way too old and doesn't work on Windows
branch = "main",
build = ":TSUpdate",
lazy = false,
cmd = {},
opts = function()
patch()
return {
highlight = { enable = true },
indent = { enable = true },
ensure_install = {
"bash",
"c",
"diff",
"html",
"javascript",
"jsdoc",
"json",
"jsonc",
"lua",
"luadoc",
"luap",
"markdown",
"markdown_inline",
"python",
"query",
"regex",
"toml",
"tsx",
"typescript",
"vim",
"vimdoc",
"xml",
"yaml",
},
}
end,
init = function() end,
---@param opts TSConfig
config = function(_, opts)
---@return string[]
local function norm(ensure)
return ensure == nil and {} or type(ensure) == "string" and { ensure } or ensure
end
-- ensure_installed is deprecated, but still supported
opts.ensure_install = LazyVim.dedup(vim.list_extend(norm(opts.ensure_install), norm(opts.ensure_installed)))
require("nvim-treesitter").setup(opts)
patch()
-- backwards compatibility with the old treesitter config for indent
if vim.tbl_get(opts, "indent", "enable") then
vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
end
-- backwards compatibility with the old treesitter config for highlight
if vim.tbl_get(opts, "highlight", "enable") then
vim.api.nvim_create_autocmd("FileType", {
callback = function()
pcall(vim.treesitter.start)
end,
})
end
end,
},
{
"nvim-treesitter/nvim-treesitter-textobjects",
enabled = false,
},
{
"windwp/nvim-ts-autotag",
enabled = false,
},
}

View file

@ -170,4 +170,19 @@ function M.safe_keymap_set(mode, lhs, rhs, opts)
end end
end end
---@generic T
---@param list T[]
---@return T[]
function M.dedup(list)
local ret = {}
local seen = {}
for _, v in ipairs(list) do
if not seen[v] then
table.insert(ret, v)
seen[v] = true
end
end
return ret
end
return M return M