refactor(treesitter): cleanup

This commit is contained in:
Folke Lemaitre 2025-09-17 15:33:37 +02:00
parent aab503fda6
commit 72e1ee5b0d
No known key found for this signature in database
GPG key ID: 9B52594D560070AB
5 changed files with 61 additions and 38 deletions

View file

@ -68,7 +68,7 @@ opt.fillchars = {
diff = "",
eob = " ",
}
opt.foldexpr = "v:lua.LazyVim.ui.foldexpr()" -- treesitter folds
opt.foldexpr = "v:lua.LazyVim.treesitter.foldexpr()" -- treesitter folds
opt.foldlevel = 99
opt.foldmethod = "expr"
opt.foldtext = ""
@ -78,7 +78,7 @@ opt.grepformat = "%f:%l:%c:%m"
opt.grepprg = "rg --vimgrep"
opt.ignorecase = true -- Ignore case
opt.inccommand = "nosplit" -- preview incremental substitute
opt.indentexpr = "v:lua.LazyVim.ui.indentexpr()" -- treesitter indents
opt.indentexpr = "v:lua.LazyVim.treesitter.indentexpr()" -- treesitter indents
opt.jumpoptions = "view"
opt.laststatus = 3 -- global statusline
opt.linebreak = true -- Wrap lines at convenient points

View file

@ -19,7 +19,9 @@ return {
event = { "LazyFile", "VeryLazy" },
cmd = { "TSUpdate", "TSInstall", "TSLog", "TSUninstall" },
opts_extend = { "ensure_installed" },
---@class lazyvim.TSConfig: TSConfig
opts = {
-- LazyVim config for treesitter
ensure_installed = {
"bash",
"c",
@ -47,43 +49,39 @@ return {
"yaml",
},
},
---@param plugin LazyPlugin
---@param opts TSConfig
config = function(plugin, opts)
if vim.fn.executable("tree-sitter") == 0 then
LazyVim.error({
---@param opts lazyvim.TSConfig
config = function(_, opts)
local TS = require("nvim-treesitter")
-- some quick sanity checks
if not TS.get_installed then
return LazyVim.error("Please use `:Lazy` and update `nvim-treesitter`")
elseif vim.fn.executable("tree-sitter") == 0 then
return LazyVim.error({
"**treesitter-main** requires the `tree-sitter` CLI executable to be installed.",
"Run `:checkhealth nvim-treesitter` for more information.",
})
return
end
if type(opts.ensure_installed) ~= "table" then
LazyVim.error("`nvim-treesitter` opts.ensure_installed must be a table")
elseif type(opts.ensure_installed) ~= "table" then
return LazyVim.error("`nvim-treesitter` opts.ensure_installed must be a table")
end
local TS = require("nvim-treesitter")
if not TS.get_installed then
LazyVim.error("Please use `:Lazy` and update `nvim-treesitter`")
return
end
-- setup treesitter
TS.setup(opts)
local needed = LazyVim.dedup(opts.ensure_installed --[[@as string[] ]])
LazyVim.ui.installed = TS.get_installed("parsers")
-- install missing parsers
local install = vim.tbl_filter(function(lang)
return not LazyVim.ui.have(lang)
end, needed)
return not LazyVim.treesitter.have(lang)
end, opts.ensure_installed or {})
if #install > 0 then
TS.install(install, { summary = true }):await(function()
LazyVim.ui.installed = TS.get_installed("parsers")
LazyVim.treesitter.get_installed(true) -- refresh the installed langs
end)
end
-- treesitter highlighting
vim.api.nvim_create_autocmd("FileType", {
callback = function(ev)
if LazyVim.ui.have(ev.match) then
if LazyVim.treesitter.have(ev.match) then
pcall(vim.treesitter.start)
end
end,

View file

@ -1,3 +1,4 @@
---@class lazyvim.util.deprecated
local M = {}
M.moved = {
@ -13,6 +14,7 @@ M.moved = {
ui = {
statuscolumn = { "Snacks.statuscolumn" },
bufremove = { "Snacks.bufdelete" },
foldexpr = { "LazyVim.treesitter.foldexpr", stacktrace = false },
fg = {
"{ fg = Snacks.util.color(...) }",
fn = function(...)
@ -28,7 +30,7 @@ function M.decorate(name, mod)
if not M.moved[name] then
return mod
end
setmetatable(mod, {
return setmetatable(mod, {
__call = function(_, ...)
local to = M.moved[name].__call[1]
LazyVim.deprecate("LazyVim." .. name, to)
@ -38,7 +40,9 @@ function M.decorate(name, mod)
__index = function(_, k)
if M.moved[name][k] then
local to = M.moved[name][k][1]
LazyVim.deprecate("LazyVim." .. name .. "." .. k, to)
LazyVim.deprecate("LazyVim." .. name .. "." .. k, to, {
stacktrace = M.moved[name][k].stacktrace,
})
if M.moved[name][k].fn then
return M.moved[name][k].fn
end
@ -55,6 +59,10 @@ function M.lazygit()
return Snacks.lazygit
end
function M.ui()
return M.decorate("ui", {})
end
function M.toggle()
LazyVim.deprecate("LazyVim.toggle", "Snacks.toggle")
return {

View file

@ -2,7 +2,7 @@ local LazyUtil = require("lazy.core.util")
---@class lazyvim.util: LazyUtilCore
---@field config LazyVimConfig
---@field ui lazyvim.util.ui
---@field treesitter lazyvim.util.treesitter
---@field lsp lazyvim.util.lsp
---@field root lazyvim.util.root
---@field terminal lazyvim.util.terminal
@ -16,14 +16,16 @@ local LazyUtil = require("lazy.core.util")
---@field mini lazyvim.util.mini
---@field pick lazyvim.util.pick
---@field cmp lazyvim.util.cmp
---@field deprecated lazyvim.util.deprecated
local M = {}
M.deprecated = require("lazyvim.util.deprecated")
setmetatable(M, {
__index = function(t, k)
if LazyUtil[k] then
return LazyUtil[k]
end
if k == "lazygit" or k == "toggle" then -- HACK: special case for lazygit
if M.deprecated[k] then
return M.deprecated[k]()
end
---@diagnostic disable-next-line: no-unknown
@ -125,13 +127,17 @@ function M.opts(name)
return Plugin.values(plugin, "opts", false)
end
function M.deprecate(old, new)
M.warn(("`%s` is deprecated. Please use `%s` instead"):format(old, new), {
title = "LazyVim",
once = true,
stacktrace = true,
stacklevel = 6,
})
---@param opts? LazyNotifyOpts
function M.deprecate(old, new, opts)
M.warn(
("`%s` is deprecated. Please use `%s` instead"):format(old, new),
vim.tbl_extend("force", {
title = "LazyVim",
once = true,
stacktrace = true,
stacklevel = 6,
}, opts or {})
)
end
-- delay notifications till vim.notify was replaced or after 500ms

View file

@ -1,12 +1,23 @@
---@class lazyvim.util.ui
---@class lazyvim.util.treesitter
local M = {}
M.installed = {} ---@type string[]
M._installed = nil ---@type table<string,string>?
---@param force boolean?
function M.get_installed(force)
if not M._installed or force then
M._installed = {}
for _, lang in ipairs(require("nvim-treesitter").get_installed("parsers")) do
M._installed[lang] = lang
end
end
return M._installed
end
---@param ft string
function M.have(ft)
local lang = vim.treesitter.language.get_lang(ft)
return vim.tbl_contains(M.installed, lang)
return lang and M.get_installed()[lang]
end
function M.foldexpr()