mirror of
https://github.com/LazyVim/LazyVim.git
synced 2025-12-25 12:14:19 +01:00
feat(treesitter): better health checks for treesitter requirements
This commit is contained in:
@@ -4,6 +4,7 @@ local start = vim.health.start or vim.health.report_start
|
||||
local ok = vim.health.ok or vim.health.report_ok
|
||||
local warn = vim.health.warn or vim.health.report_warn
|
||||
local error = vim.health.error or vim.health.report_error
|
||||
local info = vim.health.info or vim.health.report_info
|
||||
|
||||
function M.check()
|
||||
start("LazyVim")
|
||||
@@ -33,6 +34,20 @@ function M.check()
|
||||
warn(("`%s` is not installed"):format(name))
|
||||
end
|
||||
end
|
||||
|
||||
start("LazyVim nvim-treesitter")
|
||||
local tsok, health = LazyVim.treesitter.check()
|
||||
local keys = vim.tbl_keys(health) ---@type string[]
|
||||
table.sort(keys)
|
||||
for _, k in pairs(keys) do
|
||||
(health[k] and ok or error)(("`%s` is %s"):format(k, health[k] and "installed" or "not installed"))
|
||||
end
|
||||
if not tsok then
|
||||
info(
|
||||
"See the requirements at [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter/tree/main?tab=readme-ov-file#requirements)"
|
||||
)
|
||||
info("Run `:checkhealth nvim-treesitter` for more information.")
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@@ -13,7 +13,7 @@ return {
|
||||
LazyVim.error("Please restart Neovim and run `:TSUpdate` to use the `nvim-treesitter` **main** branch.")
|
||||
return
|
||||
end
|
||||
LazyVim.treesitter.ensure_treesitter_cli(function()
|
||||
LazyVim.treesitter.build(function()
|
||||
TS.update(nil, { summary = true })
|
||||
end)
|
||||
end,
|
||||
@@ -100,7 +100,7 @@ return {
|
||||
return not LazyVim.treesitter.have(lang)
|
||||
end, opts.ensure_installed or {})
|
||||
if #install > 0 then
|
||||
LazyVim.treesitter.ensure_treesitter_cli(function()
|
||||
LazyVim.treesitter.build(function()
|
||||
TS.install(install, { summary = true }):await(function()
|
||||
LazyVim.treesitter.get_installed(true) -- refresh the installed langs
|
||||
end)
|
||||
|
||||
@@ -51,31 +51,68 @@ function M.indentexpr()
|
||||
return M.have(nil, "indents") and require("nvim-treesitter").indentexpr() or -1
|
||||
end
|
||||
|
||||
---@param cb fun()
|
||||
function M.ensure_treesitter_cli(cb)
|
||||
if vim.fn.executable("tree-sitter") == 1 then
|
||||
return cb()
|
||||
---@return boolean ok, lazyvim.util.treesitter.Health health
|
||||
function M.check()
|
||||
local is_win = vim.fn.has("win32") == 1
|
||||
---@param tool string
|
||||
---@param win boolean?
|
||||
local function have(tool, win)
|
||||
return (win == nil or is_win == win) and vim.fn.executable(tool) == 1
|
||||
end
|
||||
|
||||
---@param msg? string
|
||||
local function fail(msg)
|
||||
return LazyVim.error({
|
||||
"**treesitter-nvim** `main` requires the `tree-sitter` CLI executable to be installed.",
|
||||
"Please install it manually from https://github.com/tree-sitter/tree-sitter/tree/master/crates/cli or",
|
||||
"use your system package manager.",
|
||||
"Run `:checkhealth nvim-treesitter` for more information.",
|
||||
msg,
|
||||
})
|
||||
---@class lazyvim.util.treesitter.Health: table<string,boolean>
|
||||
local ret = {
|
||||
["tree-sitter (CLI)"] = have("tree-sitter"),
|
||||
["C compiler"] = vim.env.CC or have("cc", false) or have("cl", true),
|
||||
tar = have("tar"),
|
||||
curl = have("curl"),
|
||||
node = have("node"),
|
||||
}
|
||||
local ok = true
|
||||
for _, v in pairs(ret) do
|
||||
ok = ok and v
|
||||
end
|
||||
return ok, ret
|
||||
end
|
||||
|
||||
---@param cb fun()
|
||||
function M.build(cb)
|
||||
M.ensure_treesitter_cli(function(_, err)
|
||||
local ok, health = M.check()
|
||||
if ok then
|
||||
return cb()
|
||||
else
|
||||
local lines = { "Unmet requirements for **nvim-treesitter** `main`:" }
|
||||
local keys = vim.tbl_keys(health) ---@type string[]
|
||||
table.sort(keys)
|
||||
for _, k in pairs(keys) do
|
||||
lines[#lines + 1] = ("- %s `%s`"):format(health[k] and "✅" or "❌", k)
|
||||
end
|
||||
vim.list_extend(lines, {
|
||||
"",
|
||||
"See the requirements at [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter/tree/main?tab=readme-ov-file#requirements)",
|
||||
"Run `:checkhealth nvim-treesitter` for more information.",
|
||||
})
|
||||
vim.list_extend(lines, err and { "", err } or {})
|
||||
LazyVim.error(lines, { title = "LazyVim Treesitter" })
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
---@param cb fun(ok:boolean, err?:string)
|
||||
function M.ensure_treesitter_cli(cb)
|
||||
if vim.fn.executable("tree-sitter") == 1 then
|
||||
return cb(true)
|
||||
end
|
||||
|
||||
-- try installing with mason
|
||||
if not pcall(require, "mason") then
|
||||
return fail("`mason.nvim` is disabled in your config, so we cannot install it automatically.")
|
||||
return cb(false, "`mason.nvim` is disabled in your config, so we cannot install it automatically.")
|
||||
end
|
||||
|
||||
-- check again since we might have installed it already
|
||||
if vim.fn.executable("tree-sitter") == 1 then
|
||||
return cb()
|
||||
return cb(true)
|
||||
end
|
||||
|
||||
local mr = require("mason-registry")
|
||||
@@ -88,9 +125,9 @@ function M.ensure_treesitter_cli(cb)
|
||||
vim.schedule_wrap(function(success)
|
||||
if success then
|
||||
LazyVim.info("Installed `tree-sitter-cli` with `mason.nvim`.")
|
||||
cb()
|
||||
cb(true)
|
||||
else
|
||||
fail("Failed to install `tree-sitter-cli` with `mason.nvim`.")
|
||||
cb(false, "Failed to install `tree-sitter-cli` with `mason.nvim`.")
|
||||
end
|
||||
end)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user