mirror of
https://github.com/LazyVim/LazyVim.git
synced 2025-12-25 12:14:19 +01:00
fix(treesitter): check if queries for indent/fold exists before enabling it. Fixes #6474
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -1,36 +1,54 @@
|
||||
---@class lazyvim.util.treesitter
|
||||
local M = {}
|
||||
|
||||
M._installed = nil ---@type table<string,string>?
|
||||
M._installed = nil ---@type table<string,boolean>?
|
||||
M._queries = {} ---@type table<string,boolean>
|
||||
|
||||
---@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()
|
||||
|
||||
Reference in New Issue
Block a user