mirror of
https://github.com/LazyVim/LazyVim.git
synced 2026-07-21 20:11:06 +00:00
refactor(treesitter): cleanup
This commit is contained in:
parent
aab503fda6
commit
72e1ee5b0d
5 changed files with 61 additions and 38 deletions
|
|
@ -68,7 +68,7 @@ opt.fillchars = {
|
||||||
diff = "╱",
|
diff = "╱",
|
||||||
eob = " ",
|
eob = " ",
|
||||||
}
|
}
|
||||||
opt.foldexpr = "v:lua.LazyVim.ui.foldexpr()" -- treesitter folds
|
opt.foldexpr = "v:lua.LazyVim.treesitter.foldexpr()" -- treesitter folds
|
||||||
opt.foldlevel = 99
|
opt.foldlevel = 99
|
||||||
opt.foldmethod = "expr"
|
opt.foldmethod = "expr"
|
||||||
opt.foldtext = ""
|
opt.foldtext = ""
|
||||||
|
|
@ -78,7 +78,7 @@ 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.ui.indentexpr()" -- treesitter indents
|
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
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,9 @@ return {
|
||||||
event = { "LazyFile", "VeryLazy" },
|
event = { "LazyFile", "VeryLazy" },
|
||||||
cmd = { "TSUpdate", "TSInstall", "TSLog", "TSUninstall" },
|
cmd = { "TSUpdate", "TSInstall", "TSLog", "TSUninstall" },
|
||||||
opts_extend = { "ensure_installed" },
|
opts_extend = { "ensure_installed" },
|
||||||
|
---@class lazyvim.TSConfig: TSConfig
|
||||||
opts = {
|
opts = {
|
||||||
|
-- LazyVim config for treesitter
|
||||||
ensure_installed = {
|
ensure_installed = {
|
||||||
"bash",
|
"bash",
|
||||||
"c",
|
"c",
|
||||||
|
|
@ -47,43 +49,39 @@ return {
|
||||||
"yaml",
|
"yaml",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
---@param plugin LazyPlugin
|
---@param opts lazyvim.TSConfig
|
||||||
---@param opts TSConfig
|
config = function(_, opts)
|
||||||
config = function(plugin, opts)
|
local TS = require("nvim-treesitter")
|
||||||
if vim.fn.executable("tree-sitter") == 0 then
|
|
||||||
LazyVim.error({
|
-- some quick sanity checks
|
||||||
|
if not TS.get_installed then
|
||||||
|
return LazyVim.error("Please use `:Lazy` and update `nvim-treesitter`")
|
||||||
|
elseif vim.fn.executable("tree-sitter") == 0 then
|
||||||
|
return LazyVim.error({
|
||||||
"**treesitter-main** requires the `tree-sitter` CLI executable to be installed.",
|
"**treesitter-main** requires the `tree-sitter` CLI executable to be installed.",
|
||||||
"Run `:checkhealth nvim-treesitter` for more information.",
|
"Run `:checkhealth nvim-treesitter` for more information.",
|
||||||
})
|
})
|
||||||
return
|
elseif type(opts.ensure_installed) ~= "table" then
|
||||||
end
|
return LazyVim.error("`nvim-treesitter` opts.ensure_installed must be a table")
|
||||||
if type(opts.ensure_installed) ~= "table" then
|
|
||||||
LazyVim.error("`nvim-treesitter` opts.ensure_installed must be a table")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local TS = require("nvim-treesitter")
|
-- setup treesitter
|
||||||
if not TS.get_installed then
|
|
||||||
LazyVim.error("Please use `:Lazy` and update `nvim-treesitter`")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
TS.setup(opts)
|
TS.setup(opts)
|
||||||
|
|
||||||
local needed = LazyVim.dedup(opts.ensure_installed --[[@as string[] ]])
|
-- install missing parsers
|
||||||
LazyVim.ui.installed = TS.get_installed("parsers")
|
|
||||||
|
|
||||||
local install = vim.tbl_filter(function(lang)
|
local install = vim.tbl_filter(function(lang)
|
||||||
return not LazyVim.ui.have(lang)
|
return not LazyVim.treesitter.have(lang)
|
||||||
end, needed)
|
end, opts.ensure_installed or {})
|
||||||
|
|
||||||
if #install > 0 then
|
if #install > 0 then
|
||||||
TS.install(install, { summary = true }):await(function()
|
TS.install(install, { summary = true }):await(function()
|
||||||
LazyVim.ui.installed = TS.get_installed("parsers")
|
LazyVim.treesitter.get_installed(true) -- refresh the installed langs
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- treesitter highlighting
|
||||||
vim.api.nvim_create_autocmd("FileType", {
|
vim.api.nvim_create_autocmd("FileType", {
|
||||||
callback = function(ev)
|
callback = function(ev)
|
||||||
if LazyVim.ui.have(ev.match) then
|
if LazyVim.treesitter.have(ev.match) then
|
||||||
pcall(vim.treesitter.start)
|
pcall(vim.treesitter.start)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
---@class lazyvim.util.deprecated
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
M.moved = {
|
M.moved = {
|
||||||
|
|
@ -13,6 +14,7 @@ M.moved = {
|
||||||
ui = {
|
ui = {
|
||||||
statuscolumn = { "Snacks.statuscolumn" },
|
statuscolumn = { "Snacks.statuscolumn" },
|
||||||
bufremove = { "Snacks.bufdelete" },
|
bufremove = { "Snacks.bufdelete" },
|
||||||
|
foldexpr = { "LazyVim.treesitter.foldexpr", stacktrace = false },
|
||||||
fg = {
|
fg = {
|
||||||
"{ fg = Snacks.util.color(...) }",
|
"{ fg = Snacks.util.color(...) }",
|
||||||
fn = function(...)
|
fn = function(...)
|
||||||
|
|
@ -28,7 +30,7 @@ function M.decorate(name, mod)
|
||||||
if not M.moved[name] then
|
if not M.moved[name] then
|
||||||
return mod
|
return mod
|
||||||
end
|
end
|
||||||
setmetatable(mod, {
|
return setmetatable(mod, {
|
||||||
__call = function(_, ...)
|
__call = function(_, ...)
|
||||||
local to = M.moved[name].__call[1]
|
local to = M.moved[name].__call[1]
|
||||||
LazyVim.deprecate("LazyVim." .. name, to)
|
LazyVim.deprecate("LazyVim." .. name, to)
|
||||||
|
|
@ -38,7 +40,9 @@ function M.decorate(name, mod)
|
||||||
__index = function(_, k)
|
__index = function(_, k)
|
||||||
if M.moved[name][k] then
|
if M.moved[name][k] then
|
||||||
local to = M.moved[name][k][1]
|
local to = M.moved[name][k][1]
|
||||||
LazyVim.deprecate("LazyVim." .. name .. "." .. k, to)
|
LazyVim.deprecate("LazyVim." .. name .. "." .. k, to, {
|
||||||
|
stacktrace = M.moved[name][k].stacktrace,
|
||||||
|
})
|
||||||
if M.moved[name][k].fn then
|
if M.moved[name][k].fn then
|
||||||
return M.moved[name][k].fn
|
return M.moved[name][k].fn
|
||||||
end
|
end
|
||||||
|
|
@ -55,6 +59,10 @@ function M.lazygit()
|
||||||
return Snacks.lazygit
|
return Snacks.lazygit
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M.ui()
|
||||||
|
return M.decorate("ui", {})
|
||||||
|
end
|
||||||
|
|
||||||
function M.toggle()
|
function M.toggle()
|
||||||
LazyVim.deprecate("LazyVim.toggle", "Snacks.toggle")
|
LazyVim.deprecate("LazyVim.toggle", "Snacks.toggle")
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ local LazyUtil = require("lazy.core.util")
|
||||||
|
|
||||||
---@class lazyvim.util: LazyUtilCore
|
---@class lazyvim.util: LazyUtilCore
|
||||||
---@field config LazyVimConfig
|
---@field config LazyVimConfig
|
||||||
---@field ui lazyvim.util.ui
|
---@field treesitter lazyvim.util.treesitter
|
||||||
---@field lsp lazyvim.util.lsp
|
---@field lsp lazyvim.util.lsp
|
||||||
---@field root lazyvim.util.root
|
---@field root lazyvim.util.root
|
||||||
---@field terminal lazyvim.util.terminal
|
---@field terminal lazyvim.util.terminal
|
||||||
|
|
@ -16,14 +16,16 @@ local LazyUtil = require("lazy.core.util")
|
||||||
---@field mini lazyvim.util.mini
|
---@field mini lazyvim.util.mini
|
||||||
---@field pick lazyvim.util.pick
|
---@field pick lazyvim.util.pick
|
||||||
---@field cmp lazyvim.util.cmp
|
---@field cmp lazyvim.util.cmp
|
||||||
|
---@field deprecated lazyvim.util.deprecated
|
||||||
local M = {}
|
local M = {}
|
||||||
|
M.deprecated = require("lazyvim.util.deprecated")
|
||||||
|
|
||||||
setmetatable(M, {
|
setmetatable(M, {
|
||||||
__index = function(t, k)
|
__index = function(t, k)
|
||||||
if LazyUtil[k] then
|
if LazyUtil[k] then
|
||||||
return LazyUtil[k]
|
return LazyUtil[k]
|
||||||
end
|
end
|
||||||
if k == "lazygit" or k == "toggle" then -- HACK: special case for lazygit
|
if M.deprecated[k] then
|
||||||
return M.deprecated[k]()
|
return M.deprecated[k]()
|
||||||
end
|
end
|
||||||
---@diagnostic disable-next-line: no-unknown
|
---@diagnostic disable-next-line: no-unknown
|
||||||
|
|
@ -125,13 +127,17 @@ function M.opts(name)
|
||||||
return Plugin.values(plugin, "opts", false)
|
return Plugin.values(plugin, "opts", false)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.deprecate(old, new)
|
---@param opts? LazyNotifyOpts
|
||||||
M.warn(("`%s` is deprecated. Please use `%s` instead"):format(old, new), {
|
function M.deprecate(old, new, opts)
|
||||||
title = "LazyVim",
|
M.warn(
|
||||||
once = true,
|
("`%s` is deprecated. Please use `%s` instead"):format(old, new),
|
||||||
stacktrace = true,
|
vim.tbl_extend("force", {
|
||||||
stacklevel = 6,
|
title = "LazyVim",
|
||||||
})
|
once = true,
|
||||||
|
stacktrace = true,
|
||||||
|
stacklevel = 6,
|
||||||
|
}, opts or {})
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- delay notifications till vim.notify was replaced or after 500ms
|
-- delay notifications till vim.notify was replaced or after 500ms
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,23 @@
|
||||||
---@class lazyvim.util.ui
|
---@class lazyvim.util.treesitter
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
M.installed = {} ---@type string[]
|
M._installed = nil ---@type table<string,string>?
|
||||||
|
|
||||||
|
---@param force boolean?
|
||||||
|
function M.get_installed(force)
|
||||||
|
if not M._installed or force then
|
||||||
|
M._installed = {}
|
||||||
|
for _, lang in ipairs(require("nvim-treesitter").get_installed("parsers")) do
|
||||||
|
M._installed[lang] = lang
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return M._installed
|
||||||
|
end
|
||||||
|
|
||||||
---@param ft string
|
---@param ft string
|
||||||
function M.have(ft)
|
function M.have(ft)
|
||||||
local lang = vim.treesitter.language.get_lang(ft)
|
local lang = vim.treesitter.language.get_lang(ft)
|
||||||
return vim.tbl_contains(M.installed, lang)
|
return lang and M.get_installed()[lang]
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.foldexpr()
|
function M.foldexpr()
|
||||||
Loading…
Add table
Reference in a new issue