diff --git a/lua/lazyvim/plugins/treesitter.lua b/lua/lazyvim/plugins/treesitter.lua index 751a72f3..5721979d 100644 --- a/lua/lazyvim/plugins/treesitter.lua +++ b/lua/lazyvim/plugins/treesitter.lua @@ -94,12 +94,12 @@ return { end -- indents - if vim.tbl_get(opts, "indent", "enable") ~= false then + if vim.tbl_get(opts, "indent", "enable") ~= false and LazyVim.treesitter.have(ev.match, "indents") then LazyVim.set_default("indentexpr", "v:lua.LazyVim.treesitter.indentexpr()") end -- folds - if vim.tbl_get(opts, "folds", "enable") ~= false then + if vim.tbl_get(opts, "folds", "enable") ~= false and LazyVim.treesitter.have(ev.match, "folds") then if LazyVim.set_default("foldmethod", "expr") then LazyVim.set_default("foldexpr", "v:lua.LazyVim.treesitter.foldexpr()") end diff --git a/lua/lazyvim/util/treesitter.lua b/lua/lazyvim/util/treesitter.lua index 5df6aa53..b1d647e8 100644 --- a/lua/lazyvim/util/treesitter.lua +++ b/lua/lazyvim/util/treesitter.lua @@ -1,36 +1,54 @@ ---@class lazyvim.util.treesitter local M = {} -M._installed = nil ---@type table? +M._installed = nil ---@type table? +M._queries = {} ---@type table ---@param update boolean? function M.get_installed(update) if update then - M._installed = {} + M._installed, M._queries = {}, {} for _, lang in ipairs(require("nvim-treesitter").get_installed("parsers")) do - M._installed[lang] = lang + M._installed[lang] = true end end return M._installed or {} end +---@param lang string +---@param query string +function M.have_query(lang, query) + local key = lang .. ":" .. query + if M._queries[key] == nil then + M._queries[key] = vim.treesitter.query.get(lang, query) ~= nil + end + return M._queries[key] +end + ---@param what string|number|nil +---@param query? string ---@overload fun(buf?:number):boolean ---@overload fun(ft:string):boolean ---@return boolean -function M.have(what) +function M.have(what, query) what = what or vim.api.nvim_get_current_buf() what = type(what) == "number" and vim.bo[what].filetype or what --[[@as string]] local lang = vim.treesitter.language.get_lang(what) - return lang ~= nil and M.get_installed()[lang] ~= nil + if lang == nil or M.get_installed()[lang] == nil then + return false + end + if query and not M.have_query(lang, query) then + return false + end + return true end function M.foldexpr() - return M.have() and vim.treesitter.foldexpr() or "0" + return M.have(nil, "folds") and vim.treesitter.foldexpr() or "0" end function M.indentexpr() - return M.have() and require("nvim-treesitter").indentexpr() or -1 + return M.have(nil, "indents") and require("nvim-treesitter").indentexpr() or -1 end ---@param cb fun()