Skip to content

Commit

Permalink
Do not allow 3 or more specs for the same plugin
Browse files Browse the repository at this point in the history
Workaround for lazy.nvim but folke/lazy.nvim#1328
  • Loading branch information
Supremist committed Mar 4, 2024
1 parent fabba7c commit 2245c97
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 24 deletions.
43 changes: 43 additions & 0 deletions lua/core/plugin_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
local M = {
_spec_loaded = {},
}


-- Fill second apperance of plugin spec.
-- First apperance is in the "installed.lua" file
-- Second apperance may be in separate file dedicated for each plugin.
-- It is filled with keys from "config.keymaps", changed default for "optional"
function M.spec(plugins)
local plugin_keymaps = require("config.keymaps").plugins
if type(plugins[1]) == "string" then
plugins = { plugins }
end
for _, plugin in pairs(plugins) do
local full_name = plugin[1]
local name_parts = vim.split(full_name, "/")
local name = name_parts[#name_parts]
local keys = plugin_keymaps[full_name] or plugin_keymaps[name]
if keys then
plugin.keys = require("core.tbl").array_append(plugin.keys or {}, keys:to_lazy())
end
if plugin.optional == nil then
plugin.optional = true
end
M._spec_loaded[name] = true
end
return plugins
end

-- If plugin spec has no second apperance, we need to load it's keymaps as new spec
function M.load_missing_keymaps()
local spec = {}
for name, val in pairs(require("config.keymaps").plugins) do
if not M._spec_loaded[name] then
table.insert(spec, {name, optional = true, keys = val:to_lazy()})
end
end
return spec
end

return M

8 changes: 8 additions & 0 deletions lua/core/tbl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ function M.deep_update(dst, src)
return dst
end

function M.array_append(dst, src)
local sz = #dst
for i = 1, #src do
dst[sz+i] = src[i]
end
return dst
end

function M.findKey(tbl, val)
for key, value in pairs(tbl) do
if value == val then
Expand Down
2 changes: 1 addition & 1 deletion lua/plugins/installed.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ return {
},

-- prettier cmdline, better :messages, replcae :h more-prompt
{ "folke/noice.nvim", dev = true,
{ "folke/noice.nvim", --dev = true,
dependencies = {
"MunifTanjim/nui.nvim",
-- "rcarriga/nvim-notify",
Expand Down
9 changes: 1 addition & 8 deletions lua/plugins/keymap_loader.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
-- This file is loaded by lazy.nvim.
-- It extracts keymaps for plugins from "config.keymaps" and converts it to LazyKeySpec format
local spec = {}

for name, val in pairs(require("config.keymaps").plugins) do
table.insert(spec, {name, keys = val:to_lazy()})
end
return spec
return require("core.plugin_spec").load_missing_keymaps()

2 changes: 1 addition & 1 deletion lua/plugins/spec/coding.lua
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,4 @@ function cmp_patch.mapping.complete_or_select(dir, opts)
end
end

return M
return require("core.plugin_spec").spec(M)
8 changes: 6 additions & 2 deletions lua/plugins/spec/colorscheme.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
return {
return require("core.plugin_spec").spec({
{
"ellisonleao/gruvbox.nvim",
optional = false,
-- lazy = false,
-- priority = 1000,
config = function()
Expand All @@ -17,6 +18,7 @@ return {
-- },
{
"marko-cerovac/material.nvim",
optional = false,
-- lazy = false,
-- priority = 1000,
opts = {
Expand Down Expand Up @@ -50,6 +52,7 @@ return {

{ "NvChad/nvim-colorizer.lua",
lazy = false,
optional = false,
opts = {
filetypes = { "*" },
}
Expand All @@ -59,6 +62,7 @@ return {
lazy = false,
priority = 1000,
reloadable = true,
optional = false,
opts = function(plug, opts)
return require("core.mod").reload("theme.nightfox_override").get_options("carbonfox")
end,
Expand All @@ -67,4 +71,4 @@ return {
vim.cmd([[colorscheme carbonfox]])
end,
},
}
})
4 changes: 2 additions & 2 deletions lua/plugins/spec/editor.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local optional = require("core.mod").optional

return {
return require("core.plugin_spec").spec({

-- file explorer
{ "neo-tree.nvim",
Expand Down Expand Up @@ -185,4 +185,4 @@ return {
require("undotree").setup(opts)
end,
}
}
})
4 changes: 2 additions & 2 deletions lua/plugins/spec/flash.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local optional = require("core.mod").optional
-- at the end of each match, letting you quickly jump to a Mific
-- location.
local Flash = require("core.mod").patch("flash")
local M = { "flash.nvim",
local M = require("core.plugin_spec").spec({ "flash.nvim",
event = "VeryLazy",
reloadable = true,
opts = {
Expand Down Expand Up @@ -48,7 +48,7 @@ local M = { "flash.nvim",
end
end
},
}
})

function Flash.jump(opts)
opts = opts or {}
Expand Down
4 changes: 2 additions & 2 deletions lua/plugins/spec/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ function lsp.config(_, opts)
end


return {
return require("core.plugin_spec").spec({
clangd,
lsp,
}
})
4 changes: 2 additions & 2 deletions lua/plugins/spec/markdown.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
return {
return require("core.plugin_spec").spec({
{ "markdown-preview.nvim",
optional = true,
cmd = { "MarkdownPreviewToggle", "MarkdownPreview", "MarkdownPreviewStop" },
Expand All @@ -9,4 +9,4 @@ return {
end,
ft = { "markdown" },
},
}
})
4 changes: 2 additions & 2 deletions lua/plugins/spec/treesitter.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
return {
return require("core.plugin_spec").spec({
-- Treesitter is a new parser generator tool that we can
-- use in Neovim to power faster and more accurate
-- syntax highlighting.
Expand Down Expand Up @@ -64,4 +64,4 @@ return {
require("nvim-treesitter.configs").setup(opts)
end,
},
}
})
4 changes: 2 additions & 2 deletions lua/plugins/spec/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local rainbow_dim_highlights = {
"RainbowDimCyan",
}

return {
return require("core.plugin_spec").spec({
{ "rainbow-delimiters.nvim",
event = "VeryLazy",
reloadable = true,
Expand Down Expand Up @@ -215,4 +215,4 @@ return {
}
end,
},
}
})

0 comments on commit 2245c97

Please sign in to comment.