feat(treesitter): refactored setting up treesitter indent/highlight/folds

This commit is contained in:
Folke Lemaitre
2025-09-18 09:48:43 +02:00
parent 5bf237820d
commit b93303d233
4 changed files with 33 additions and 21 deletions

View File

@@ -68,9 +68,8 @@ opt.fillchars = {
diff = "",
eob = " ",
}
opt.foldexpr = "v:lua.LazyVim.treesitter.foldexpr()" -- treesitter folds
opt.foldlevel = 99
opt.foldmethod = "expr"
opt.foldmethod = "indent"
opt.foldtext = ""
opt.formatexpr = "v:lua.LazyVim.format.formatexpr()"
opt.formatoptions = "jcroqlnt" -- tcqj
@@ -78,7 +77,6 @@ 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.treesitter.indentexpr()" -- treesitter indents
opt.jumpoptions = "view"
opt.laststatus = 3 -- global statusline
opt.linebreak = true -- Wrap lines at convenient points

View File

@@ -148,6 +148,7 @@ return {
LazyVim.lsp.on_supports_method("textDocument/foldingRange", function(client, buffer)
local win = vim.api.nvim_get_current_win()
vim.wo[win][0].foldexpr = "v:lua.vim.lsp.foldexpr()"
vim.wo[win][0].foldmethod = "expr"
end)
end

View File

@@ -22,6 +22,9 @@ return {
---@class lazyvim.TSConfig: TSConfig
opts = {
-- LazyVim config for treesitter
indent = { enable = true },
highlight = { enable = true },
folds = { enable = true },
ensure_installed = {
"bash",
"c",
@@ -80,20 +83,27 @@ return {
end)
end
-- treesitter highlighting
vim.api.nvim_create_autocmd("FileType", {
group = vim.api.nvim_create_augroup("lazyvim_treesitter", { clear = true }),
callback = function(ev)
if LazyVim.treesitter.have(ev.match) then
pcall(vim.treesitter.start)
if not LazyVim.treesitter.have(ev.match) then
return
end
-- check if ftplugins changed foldexpr/indentexpr
for _, option in ipairs({ "foldexpr", "indentexpr" }) do
local expr = "v:lua.LazyVim.treesitter." .. option .. "()"
if vim.opt_global[option]:get() == expr then
vim.opt_local[option] = expr
end
end
-- highlighting
if vim.tbl_get(opts, "highlight", "enable") ~= false then
pcall(vim.treesitter.start)
end
-- indents
if vim.tbl_get(opts, "indent", "enable") ~= false then
vim.bo[ev.buf].indentexpr = "v:lua.LazyVim.treesitter.indentexpr()"
end
-- folds
if vim.tbl_get(opts, "folds", "enable") ~= false then
vim.wo.foldmethod = "expr"
vim.wo.foldexpr = "v:lua.LazyVim.treesitter.foldexpr()"
end
end,
})

View File

@@ -14,20 +14,23 @@ function M.get_installed(update)
return M._installed or {}
end
---@param ft string
function M.have(ft)
local lang = vim.treesitter.language.get_lang(ft)
return lang and M.get_installed()[lang]
---@param what string|number|nil
---@overload fun(buf?:number):boolean
---@overload fun(ft:string):boolean
---@return boolean
function M.have(what)
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
end
function M.foldexpr()
local buf = vim.api.nvim_get_current_buf()
return M.have(vim.bo[buf].filetype) and vim.treesitter.foldexpr() or "0"
return M.have() and vim.treesitter.foldexpr() or "0"
end
function M.indentexpr()
local buf = vim.api.nvim_get_current_buf()
return M.have(vim.bo[buf].filetype) and require("nvim-treesitter").indentexpr() or -1
return M.have() and require("nvim-treesitter").indentexpr() or -1
end
return M