mirror of
https://github.com/LazyVim/LazyVim.git
synced 2026-07-22 04:21:08 +00:00
feat(treesitter): refactored setting up treesitter indent/highlight/folds
This commit is contained in:
parent
5bf237820d
commit
b93303d233
4 changed files with 33 additions and 21 deletions
|
|
@ -68,9 +68,8 @@ opt.fillchars = {
|
||||||
diff = "╱",
|
diff = "╱",
|
||||||
eob = " ",
|
eob = " ",
|
||||||
}
|
}
|
||||||
opt.foldexpr = "v:lua.LazyVim.treesitter.foldexpr()" -- treesitter folds
|
|
||||||
opt.foldlevel = 99
|
opt.foldlevel = 99
|
||||||
opt.foldmethod = "expr"
|
opt.foldmethod = "indent"
|
||||||
opt.foldtext = ""
|
opt.foldtext = ""
|
||||||
opt.formatexpr = "v:lua.LazyVim.format.formatexpr()"
|
opt.formatexpr = "v:lua.LazyVim.format.formatexpr()"
|
||||||
opt.formatoptions = "jcroqlnt" -- tcqj
|
opt.formatoptions = "jcroqlnt" -- tcqj
|
||||||
|
|
@ -78,7 +77,6 @@ opt.grepformat = "%f:%l:%c:%m"
|
||||||
opt.grepprg = "rg --vimgrep"
|
opt.grepprg = "rg --vimgrep"
|
||||||
opt.ignorecase = true -- Ignore case
|
opt.ignorecase = true -- Ignore case
|
||||||
opt.inccommand = "nosplit" -- preview incremental substitute
|
opt.inccommand = "nosplit" -- preview incremental substitute
|
||||||
opt.indentexpr = "v:lua.LazyVim.treesitter.indentexpr()" -- treesitter indents
|
|
||||||
opt.jumpoptions = "view"
|
opt.jumpoptions = "view"
|
||||||
opt.laststatus = 3 -- global statusline
|
opt.laststatus = 3 -- global statusline
|
||||||
opt.linebreak = true -- Wrap lines at convenient points
|
opt.linebreak = true -- Wrap lines at convenient points
|
||||||
|
|
|
||||||
|
|
@ -148,6 +148,7 @@ return {
|
||||||
LazyVim.lsp.on_supports_method("textDocument/foldingRange", function(client, buffer)
|
LazyVim.lsp.on_supports_method("textDocument/foldingRange", function(client, buffer)
|
||||||
local win = vim.api.nvim_get_current_win()
|
local win = vim.api.nvim_get_current_win()
|
||||||
vim.wo[win][0].foldexpr = "v:lua.vim.lsp.foldexpr()"
|
vim.wo[win][0].foldexpr = "v:lua.vim.lsp.foldexpr()"
|
||||||
|
vim.wo[win][0].foldmethod = "expr"
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,9 @@ return {
|
||||||
---@class lazyvim.TSConfig: TSConfig
|
---@class lazyvim.TSConfig: TSConfig
|
||||||
opts = {
|
opts = {
|
||||||
-- LazyVim config for treesitter
|
-- LazyVim config for treesitter
|
||||||
|
indent = { enable = true },
|
||||||
|
highlight = { enable = true },
|
||||||
|
folds = { enable = true },
|
||||||
ensure_installed = {
|
ensure_installed = {
|
||||||
"bash",
|
"bash",
|
||||||
"c",
|
"c",
|
||||||
|
|
@ -80,20 +83,27 @@ return {
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- treesitter highlighting
|
|
||||||
vim.api.nvim_create_autocmd("FileType", {
|
vim.api.nvim_create_autocmd("FileType", {
|
||||||
group = vim.api.nvim_create_augroup("lazyvim_treesitter", { clear = true }),
|
group = vim.api.nvim_create_augroup("lazyvim_treesitter", { clear = true }),
|
||||||
callback = function(ev)
|
callback = function(ev)
|
||||||
if LazyVim.treesitter.have(ev.match) then
|
if not LazyVim.treesitter.have(ev.match) then
|
||||||
pcall(vim.treesitter.start)
|
return
|
||||||
|
end
|
||||||
|
|
||||||
-- check if ftplugins changed foldexpr/indentexpr
|
-- highlighting
|
||||||
for _, option in ipairs({ "foldexpr", "indentexpr" }) do
|
if vim.tbl_get(opts, "highlight", "enable") ~= false then
|
||||||
local expr = "v:lua.LazyVim.treesitter." .. option .. "()"
|
pcall(vim.treesitter.start)
|
||||||
if vim.opt_global[option]:get() == expr then
|
end
|
||||||
vim.opt_local[option] = expr
|
|
||||||
end
|
-- indents
|
||||||
end
|
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
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -14,20 +14,23 @@ function M.get_installed(update)
|
||||||
return M._installed or {}
|
return M._installed or {}
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param ft string
|
---@param what string|number|nil
|
||||||
function M.have(ft)
|
---@overload fun(buf?:number):boolean
|
||||||
local lang = vim.treesitter.language.get_lang(ft)
|
---@overload fun(ft:string):boolean
|
||||||
return lang and M.get_installed()[lang]
|
---@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
|
end
|
||||||
|
|
||||||
function M.foldexpr()
|
function M.foldexpr()
|
||||||
local buf = vim.api.nvim_get_current_buf()
|
return M.have() and vim.treesitter.foldexpr() or "0"
|
||||||
return M.have(vim.bo[buf].filetype) and vim.treesitter.foldexpr() or "0"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.indentexpr()
|
function M.indentexpr()
|
||||||
local buf = vim.api.nvim_get_current_buf()
|
return M.have() and require("nvim-treesitter").indentexpr() or -1
|
||||||
return M.have(vim.bo[buf].filetype) and require("nvim-treesitter").indentexpr() or -1
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue