From 413b9d5fa91c5bd5088b28c6d1764caa88542cb1 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Wed, 15 Oct 2025 08:37:11 +0200 Subject: [PATCH] feat(treesitter): better health checks for treesitter requirements --- lua/lazyvim/health.lua | 15 +++++++ lua/lazyvim/plugins/treesitter.lua | 4 +- lua/lazyvim/util/treesitter.lua | 71 +++++++++++++++++++++++------- 3 files changed, 71 insertions(+), 19 deletions(-) diff --git a/lua/lazyvim/health.lua b/lua/lazyvim/health.lua index d1632e75..7af6ad94 100644 --- a/lua/lazyvim/health.lua +++ b/lua/lazyvim/health.lua @@ -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 diff --git a/lua/lazyvim/plugins/treesitter.lua b/lua/lazyvim/plugins/treesitter.lua index 6256904d..5ab47721 100644 --- a/lua/lazyvim/plugins/treesitter.lua +++ b/lua/lazyvim/plugins/treesitter.lua @@ -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) diff --git a/lua/lazyvim/util/treesitter.lua b/lua/lazyvim/util/treesitter.lua index b1d647e8..d5c845fd 100644 --- a/lua/lazyvim/util/treesitter.lua +++ b/lua/lazyvim/util/treesitter.lua @@ -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 + 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) )