From 525377dee9ac3d19f53e333538e4e85586163ad8 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sat, 8 Feb 2025 15:04:46 +0100 Subject: [PATCH] feat(extras): big rework of default extras --- lua/lazyvim/config/init.lua | 58 +++++++ lua/lazyvim/plugins/coding.lua | 13 -- lua/lazyvim/plugins/editor.lua | 145 ------------------ lua/lazyvim/plugins/extras/editor/fzf.lua | 3 - .../plugins/extras/editor/neo-tree.lua | 128 ++++++++++++++++ .../plugins/extras/editor/snacks_picker.lua | 3 - .../plugins/extras/editor/telescope.lua | 9 -- lua/lazyvim/plugins/extras/lang/sql.lua | 2 +- lua/lazyvim/plugins/xtras.lua | 22 ++- lua/lazyvim/util/extras.lua | 6 +- lua/lazyvim/util/init.lua | 9 -- lua/lazyvim/util/pick.lua | 15 -- lua/lazyvim/util/plugin.lua | 9 ++ 13 files changed, 222 insertions(+), 200 deletions(-) create mode 100644 lua/lazyvim/plugins/extras/editor/neo-tree.lua diff --git a/lua/lazyvim/config/init.lua b/lua/lazyvim/config/init.lua index b826d2db..385d0b13 100644 --- a/lua/lazyvim/config/init.lua +++ b/lua/lazyvim/config/init.lua @@ -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 +function M.get_defaults() + if default_extras then + return default_extras + end + ---@type table + 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 diff --git a/lua/lazyvim/plugins/coding.lua b/lua/lazyvim/plugins/coding.lua index adc76177..0e614e48 100644 --- a/lua/lazyvim/plugins/coding.lua +++ b/lua/lazyvim/plugins/coding.lua @@ -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", diff --git a/lua/lazyvim/plugins/editor.lua b/lua/lazyvim/plugins/editor.lua index 95a1f7b9..4aaf3d1f 100644 --- a/lua/lazyvim/plugins/editor.lua +++ b/lua/lazyvim/plugins/editor.lua @@ -1,131 +1,5 @@ return { - -- file explorer - { - "nvim-neo-tree/neo-tree.nvim", - cmd = "Neotree", - keys = { - { - "fe", - function() - require("neo-tree.command").execute({ toggle = true, dir = LazyVim.root() }) - end, - desc = "Explorer NeoTree (Root Dir)", - }, - { - "fE", - function() - require("neo-tree.command").execute({ toggle = true, dir = vim.uv.cwd() }) - end, - desc = "Explorer NeoTree (cwd)", - }, - { "e", "fe", desc = "Explorer NeoTree (Root Dir)", remap = true }, - { "E", "fE", desc = "Explorer NeoTree (cwd)", remap = true }, - { - "ge", - function() - require("neo-tree.command").execute({ source = "git_status", toggle = true }) - end, - desc = "Git Explorer", - }, - { - "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", - [""] = "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 { { "sT", "TodoTelescope keywords=TODO,FIX,FIXME", 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, - }, } diff --git a/lua/lazyvim/plugins/extras/editor/fzf.lua b/lua/lazyvim/plugins/extras/editor/fzf.lua index d4cd4093..aa26f6e1 100644 --- a/lua/lazyvim/plugins/extras/editor/fzf.lua +++ b/lua/lazyvim/plugins/extras/editor/fzf.lua @@ -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, { diff --git a/lua/lazyvim/plugins/extras/editor/neo-tree.lua b/lua/lazyvim/plugins/extras/editor/neo-tree.lua new file mode 100644 index 00000000..4a3f99bf --- /dev/null +++ b/lua/lazyvim/plugins/extras/editor/neo-tree.lua @@ -0,0 +1,128 @@ +return { + + -- file explorer + { + "nvim-neo-tree/neo-tree.nvim", + cmd = "Neotree", + keys = { + { + "fe", + function() + require("neo-tree.command").execute({ toggle = true, dir = LazyVim.root() }) + end, + desc = "Explorer NeoTree (Root Dir)", + }, + { + "fE", + function() + require("neo-tree.command").execute({ toggle = true, dir = vim.uv.cwd() }) + end, + desc = "Explorer NeoTree (cwd)", + }, + { "e", "fe", desc = "Explorer NeoTree (Root Dir)", remap = true }, + { "E", "fE", desc = "Explorer NeoTree (cwd)", remap = true }, + { + "ge", + function() + require("neo-tree.command").execute({ source = "git_status", toggle = true }) + end, + desc = "Git Explorer", + }, + { + "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", + [""] = "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, + }, +} diff --git a/lua/lazyvim/plugins/extras/editor/snacks_picker.lua b/lua/lazyvim/plugins/extras/editor/snacks_picker.lua index bd160f26..d77c0933 100644 --- a/lua/lazyvim/plugins/extras/editor/snacks_picker.lua +++ b/lua/lazyvim/plugins/extras/editor/snacks_picker.lua @@ -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, { diff --git a/lua/lazyvim/plugins/extras/editor/telescope.lua b/lua/lazyvim/plugins/extras/editor/telescope.lua index 55ccaceb..9fdf7e13 100644 --- a/lua/lazyvim/plugins/extras/editor/telescope.lua +++ b/lua/lazyvim/plugins/extras/editor/telescope.lua @@ -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, { diff --git a/lua/lazyvim/plugins/extras/lang/sql.lua b/lua/lazyvim/plugins/extras/lang/sql.lua index 83de5ee1..8e69374c 100644 --- a/lua/lazyvim/plugins/extras/lang/sql.lua +++ b/lua/lazyvim/plugins/extras/lang/sql.lua @@ -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 diff --git a/lua/lazyvim/plugins/xtras.lua b/lua/lazyvim/plugins/xtras.lua index ebb0dd89..30cab43f 100644 --- a/lua/lazyvim/plugins/xtras.lua +++ b/lua/lazyvim/plugins/xtras.lua @@ -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 diff --git a/lua/lazyvim/util/extras.lua b/lua/lazyvim/util/extras.lua index bd5ca4f3..4984bf54 100644 --- a/lua/lazyvim/util/extras.lua +++ b/lua/lazyvim/util/extras.lua @@ -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, diff --git a/lua/lazyvim/util/init.lua b/lua/lazyvim/util/init.lua index 66e6e18b..4b49f539 100644 --- a/lua/lazyvim/util/init.lua +++ b/lua/lazyvim/util/init.lua @@ -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 diff --git a/lua/lazyvim/util/pick.lua b/lua/lazyvim/util/pick.lua index 2736d7a5..e07150d1 100644 --- a/lua/lazyvim/util/pick.lua +++ b/lua/lazyvim/util/pick.lua @@ -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) diff --git a/lua/lazyvim/util/plugin.lua b/lua/lazyvim/util/plugin.lua index f4a933e9..5d9249fc 100644 --- a/lua/lazyvim/util/plugin.lua +++ b/lua/lazyvim/util/plugin.lua @@ -79,7 +79,16 @@ function M.lazy_file() end function M.fix_imports() + local defaults ---@type table 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."