feat(extras): big rework of default extras

This commit is contained in:
Folke Lemaitre 2025-02-08 15:04:46 +01:00
parent 0416376733
commit 525377dee9
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
13 changed files with 222 additions and 200 deletions

View file

@ -144,8 +144,10 @@ M.json = {
extras = {}, ---@type string[]
},
}
M.json_loaded = false
function M.json.load()
M.json_loaded = true
local f = io.open(M.json.path, "r")
if f then
local data = f:read("*a")
@ -322,6 +324,62 @@ function M.init()
M.json.load()
end
---@alias LazyVimDefault {name: string, extra: string, enabled?: boolean, origin?: "global" | "default" | "extra" }
local default_extras ---@type table<string, LazyVimDefault>
function M.get_defaults()
if default_extras then
return default_extras
end
---@type table<string, LazyVimDefault[]>
local checks = {
picker = {
{ name = "fzf", extra = "editor.fzf" },
{ name = "snacks", extra = "editor.snacks_picker" },
{ name = "telescope", extra = "editor.telescope" },
},
cmp = {
{ name = "blink.cmp", extra = "coding.blink", enabled = vim.fn.has("nvim-0.10") == 1 },
{ name = "nvim-cmp", extra = "coding.nvim-cmp" },
},
explorer = {
{ name = "neo-tree", extra = "editor.neo-tree" },
{ name = "snacks", extra = "editor.snacks_explorer" },
},
}
default_extras = {}
for name, check in pairs(checks) do
local valid = {} ---@type string[]
for _, extra in ipairs(check) do
if extra.enabled ~= false then
valid[#valid + 1] = extra.name
end
end
local origin = "default"
local use = vim.g["lazyvim_" .. name]
use = vim.tbl_contains(valid, use or "auto") and use or nil
origin = use and "global" or origin
for _, extra in ipairs(use and {} or check) do
if extra.enabled ~= false and LazyVim.has_extra(extra.extra) then
use = extra.name
break
end
end
origin = use and "extra" or origin
use = use or valid[1]
for _, extra in ipairs(check) do
local import = "lazyvim.plugins.extras." .. extra.extra
extra = vim.deepcopy(extra)
extra.enabled = extra.name == use
if extra.enabled then
extra.origin = origin
end
default_extras[import] = extra
end
end
return default_extras
end
setmetatable(M, {
__index = function(_, key)
if options == nil then

View file

@ -1,17 +1,4 @@
return {
{
import = "lazyvim.plugins.extras.coding.nvim-cmp",
enabled = function()
return LazyVim.cmp_engine() == "nvim-cmp"
end,
},
{
import = "lazyvim.plugins.extras.coding.blink",
enabled = function()
return LazyVim.cmp_engine() == "blink.cmp"
end,
},
-- auto pairs
{
"echasnovski/mini.pairs",

View file

@ -1,131 +1,5 @@
return {
-- file explorer
{
"nvim-neo-tree/neo-tree.nvim",
cmd = "Neotree",
keys = {
{
"<leader>fe",
function()
require("neo-tree.command").execute({ toggle = true, dir = LazyVim.root() })
end,
desc = "Explorer NeoTree (Root Dir)",
},
{
"<leader>fE",
function()
require("neo-tree.command").execute({ toggle = true, dir = vim.uv.cwd() })
end,
desc = "Explorer NeoTree (cwd)",
},
{ "<leader>e", "<leader>fe", desc = "Explorer NeoTree (Root Dir)", remap = true },
{ "<leader>E", "<leader>fE", desc = "Explorer NeoTree (cwd)", remap = true },
{
"<leader>ge",
function()
require("neo-tree.command").execute({ source = "git_status", toggle = true })
end,
desc = "Git Explorer",
},
{
"<leader>be",
function()
require("neo-tree.command").execute({ source = "buffers", toggle = true })
end,
desc = "Buffer Explorer",
},
},
deactivate = function()
vim.cmd([[Neotree close]])
end,
init = function()
-- FIX: use `autocmd` for lazy-loading neo-tree instead of directly requiring it,
-- because `cwd` is not set up properly.
vim.api.nvim_create_autocmd("BufEnter", {
group = vim.api.nvim_create_augroup("Neotree_start_directory", { clear = true }),
desc = "Start Neo-tree with directory",
once = true,
callback = function()
if package.loaded["neo-tree"] then
return
else
local stats = vim.uv.fs_stat(vim.fn.argv(0))
if stats and stats.type == "directory" then
require("neo-tree")
end
end
end,
})
end,
opts = {
sources = { "filesystem", "buffers", "git_status" },
open_files_do_not_replace_types = { "terminal", "Trouble", "trouble", "qf", "Outline" },
filesystem = {
bind_to_cwd = false,
follow_current_file = { enabled = true },
use_libuv_file_watcher = true,
},
window = {
mappings = {
["l"] = "open",
["h"] = "close_node",
["<space>"] = "none",
["Y"] = {
function(state)
local node = state.tree:get_node()
local path = node:get_id()
vim.fn.setreg("+", path, "c")
end,
desc = "Copy Path to Clipboard",
},
["O"] = {
function(state)
require("lazy.util").open(state.tree:get_node().path, { system = true })
end,
desc = "Open with System Application",
},
["P"] = { "toggle_preview", config = { use_float = false } },
},
},
default_component_configs = {
indent = {
with_expanders = true, -- if nil and file nesting is enabled, will enable expanders
expander_collapsed = "",
expander_expanded = "",
expander_highlight = "NeoTreeExpander",
},
git_status = {
symbols = {
unstaged = "󰄱",
staged = "󰱒",
},
},
},
},
config = function(_, opts)
local function on_move(data)
Snacks.rename.on_rename_file(data.source, data.destination)
end
local events = require("neo-tree.events")
opts.event_handlers = opts.event_handlers or {}
vim.list_extend(opts.event_handlers, {
{ event = events.FILE_MOVED, handler = on_move },
{ event = events.FILE_RENAMED, handler = on_move },
})
require("neo-tree").setup(opts)
vim.api.nvim_create_autocmd("TermClose", {
pattern = "*lazygit",
callback = function()
if package.loaded["neo-tree.sources.git_status"] then
require("neo-tree.sources.git_status").refresh()
end
end,
})
end,
},
-- search/replace in multiple files
{
"MagicDuck/grug-far.nvim",
@ -384,23 +258,4 @@ return {
{ "<leader>sT", "<cmd>TodoTelescope keywords=TODO,FIX,FIXME<cr>", desc = "Todo/Fix/Fixme" },
},
},
{
import = "lazyvim.plugins.extras.editor.fzf",
enabled = function()
return LazyVim.pick.want() == "fzf"
end,
},
{
import = "lazyvim.plugins.extras.editor.telescope",
enabled = function()
return LazyVim.pick.want() == "telescope"
end,
},
{
import = "lazyvim.plugins.extras.editor.snacks_picker",
enabled = function()
return LazyVim.pick.want() == "snacks"
end,
},
}

View file

@ -286,9 +286,6 @@ return {
{
"neovim/nvim-lspconfig",
opts = function()
if LazyVim.pick.want() ~= "fzf" then
return
end
local Keys = require("lazyvim.plugins.lsp.keymaps").get()
-- stylua: ignore
vim.list_extend(Keys, {

View file

@ -0,0 +1,128 @@
return {
-- file explorer
{
"nvim-neo-tree/neo-tree.nvim",
cmd = "Neotree",
keys = {
{
"<leader>fe",
function()
require("neo-tree.command").execute({ toggle = true, dir = LazyVim.root() })
end,
desc = "Explorer NeoTree (Root Dir)",
},
{
"<leader>fE",
function()
require("neo-tree.command").execute({ toggle = true, dir = vim.uv.cwd() })
end,
desc = "Explorer NeoTree (cwd)",
},
{ "<leader>e", "<leader>fe", desc = "Explorer NeoTree (Root Dir)", remap = true },
{ "<leader>E", "<leader>fE", desc = "Explorer NeoTree (cwd)", remap = true },
{
"<leader>ge",
function()
require("neo-tree.command").execute({ source = "git_status", toggle = true })
end,
desc = "Git Explorer",
},
{
"<leader>be",
function()
require("neo-tree.command").execute({ source = "buffers", toggle = true })
end,
desc = "Buffer Explorer",
},
},
deactivate = function()
vim.cmd([[Neotree close]])
end,
init = function()
-- FIX: use `autocmd` for lazy-loading neo-tree instead of directly requiring it,
-- because `cwd` is not set up properly.
vim.api.nvim_create_autocmd("BufEnter", {
group = vim.api.nvim_create_augroup("Neotree_start_directory", { clear = true }),
desc = "Start Neo-tree with directory",
once = true,
callback = function()
if package.loaded["neo-tree"] then
return
else
local stats = vim.uv.fs_stat(vim.fn.argv(0))
if stats and stats.type == "directory" then
require("neo-tree")
end
end
end,
})
end,
opts = {
sources = { "filesystem", "buffers", "git_status" },
open_files_do_not_replace_types = { "terminal", "Trouble", "trouble", "qf", "Outline" },
filesystem = {
bind_to_cwd = false,
follow_current_file = { enabled = true },
use_libuv_file_watcher = true,
},
window = {
mappings = {
["l"] = "open",
["h"] = "close_node",
["<space>"] = "none",
["Y"] = {
function(state)
local node = state.tree:get_node()
local path = node:get_id()
vim.fn.setreg("+", path, "c")
end,
desc = "Copy Path to Clipboard",
},
["O"] = {
function(state)
require("lazy.util").open(state.tree:get_node().path, { system = true })
end,
desc = "Open with System Application",
},
["P"] = { "toggle_preview", config = { use_float = false } },
},
},
default_component_configs = {
indent = {
with_expanders = true, -- if nil and file nesting is enabled, will enable expanders
expander_collapsed = "",
expander_expanded = "",
expander_highlight = "NeoTreeExpander",
},
git_status = {
symbols = {
unstaged = "󰄱",
staged = "󰱒",
},
},
},
},
config = function(_, opts)
local function on_move(data)
Snacks.rename.on_rename_file(data.source, data.destination)
end
local events = require("neo-tree.events")
opts.event_handlers = opts.event_handlers or {}
vim.list_extend(opts.event_handlers, {
{ event = events.FILE_MOVED, handler = on_move },
{ event = events.FILE_RENAMED, handler = on_move },
})
require("neo-tree").setup(opts)
vim.api.nvim_create_autocmd("TermClose", {
pattern = "*lazygit",
callback = function()
if package.loaded["neo-tree.sources.git_status"] then
require("neo-tree.sources.git_status").refresh()
end
end,
})
end,
},
}

View file

@ -136,9 +136,6 @@ return {
{
"neovim/nvim-lspconfig",
opts = function()
if LazyVim.pick.want() ~= "snacks" then
return
end
local Keys = require("lazyvim.plugins.lsp.keymaps").get()
-- stylua: ignore
vim.list_extend(Keys, {

View file

@ -61,9 +61,6 @@ return {
{
"nvim-telescope/telescope.nvim",
cmd = "Telescope",
enabled = function()
return LazyVim.pick.want() == "telescope"
end,
version = false, -- telescope did only one release, so use HEAD for now
dependencies = {
{
@ -267,9 +264,6 @@ return {
{
"stevearc/dressing.nvim",
lazy = true,
enabled = function()
return LazyVim.pick.want() == "telescope"
end,
init = function()
---@diagnostic disable-next-line: duplicate-set-field
vim.ui.select = function(...)
@ -287,9 +281,6 @@ return {
{
"neovim/nvim-lspconfig",
opts = function()
if LazyVim.pick.want() ~= "telescope" then
return
end
local Keys = require("lazyvim.plugins.lsp.keymaps").get()
-- stylua: ignore
vim.list_extend(Keys, {

View file

@ -48,7 +48,7 @@ return {
vim.api.nvim_create_autocmd("FileType", {
pattern = sql_ft,
callback = function()
if LazyVim.cmp_engine() == "nvim-cmp" then
if LazyVim.has_extra("coding.nvim-cmp") then
local cmp = require("cmp")
-- global sources

View file

@ -7,6 +7,7 @@ local prios = {
["lazyvim.plugins.extras.lang.typescript"] = 5,
["lazyvim.plugins.extras.coding.blink"] = 5,
["lazyvim.plugins.extras.formatting.prettier"] = 10,
-- default core extra priority is 20
-- default priority is 50
["lazyvim.plugins.extras.editor.aerial"] = 100,
["lazyvim.plugins.extras.editor.outline"] = 100,
@ -16,8 +17,27 @@ if vim.g.xtras_prios then
prios = vim.tbl_deep_extend("force", prios, vim.g.xtras_prios or {})
end
local extras = {} ---@type string[]
local defaults = LazyVim.config.get_defaults()
-- Add extras from LazyExtras that are not disabled
for _, extra in ipairs(LazyVim.config.json.data.extras) do
local def = defaults[extra]
if not (def and def.enabled == false) then
extras[#extras + 1] = extra
end
end
-- Add default extras
for name, extra in pairs(defaults) do
if extra.enabled then
prios[name] = prios[name] or 20
extras[#extras + 1] = name
end
end
---@type string[]
local extras = LazyVim.dedup(LazyVim.config.json.data.extras)
extras = LazyVim.dedup(extras)
local version = vim.version()
local v = version.major .. "_" .. version.minor

View file

@ -248,6 +248,9 @@ end
---@param extra LazyExtra
function X:extra(extra)
local defaults = LazyVim.config.get_defaults()
local def = defaults[extra.module]
local origin = def and (def.origin or "user") or nil
if not extra.managed then
---@type LazyExtra[]
local parents = {}
@ -263,11 +266,12 @@ function X:extra(extra)
self:diagnostic({
message = "Required by " .. table.concat(pp, ", "),
})
elseif vim.tbl_contains(LazyVim.plugin.core_imports, extra.module) then
elseif vim.tbl_contains(LazyVim.plugin.core_imports, extra.module) or origin == "default" then
self:diagnostic({
message = "This extra is included by default",
})
else
dd(origin)
self:diagnostic({
message = "Not managed by LazyExtras (config)",
severity = vim.diagnostic.severity.WARN,

View file

@ -286,13 +286,4 @@ function M.memoize(fn)
end
end
---@return "nvim-cmp" | "blink.cmp"
function M.cmp_engine()
vim.g.lazyvim_cmp = vim.g.lazyvim_cmp or "auto"
if vim.g.lazyvim_cmp == "auto" then
return LazyVim.has_extra("coding.nvim-cmp") and "nvim-cmp" or "blink.cmp"
end
return vim.g.lazyvim_cmp
end
return M

View file

@ -28,10 +28,6 @@ function M.register(picker)
return true
end
if M.picker and M.picker.name ~= M.want() then
M.picker = nil
end
if M.picker and M.picker.name ~= picker.name then
LazyVim.warn(
"`LazyVim.pick`: picker already set to `" .. M.picker.name .. "`,\nignoring new picker `" .. picker.name .. "`"
@ -42,17 +38,6 @@ function M.register(picker)
return true
end
---@return "telescope" | "fzf" | "snacks"
function M.want()
vim.g.lazyvim_picker = vim.g.lazyvim_picker or "auto"
if vim.g.lazyvim_picker == "auto" then
return LazyVim.has_extra("editor.snacks_picker") and "snacks"
or LazyVim.has_extra("editor.telescope") and "telescope"
or "fzf"
end
return vim.g.lazyvim_picker
end
---@param command? string
---@param opts? lazyvim.util.pick.Opts
function M.open(command, opts)

View file

@ -79,7 +79,16 @@ function M.lazy_file()
end
function M.fix_imports()
local defaults ---@type table<string, LazyVimDefault>
Plugin.Spec.import = LazyVim.inject.args(Plugin.Spec.import, function(_, spec)
if LazyVim.config.json_loaded then
-- extra disabled by defaults?
defaults = defaults or LazyVim.config.get_defaults()
local def = defaults[spec.import]
if def and def.enabled == false then
return false
end
end
local dep = M.deprecated_extras[spec and spec.import]
if dep then
dep = dep .. "\n" .. "Please remove the extra from `lazyvim.json` to hide this warning."