Skip to content

Commit

Permalink
fix(plugin): improved dir/dev merging. Fixes #993
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Oct 15, 2023
1 parent 0c53d46 commit 3dc413d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 11 deletions.
38 changes: 27 additions & 11 deletions lua/lazy/core/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ function Spec:add(plugin, results)
end
end

---@type string?
local dir

if plugin.dir then
plugin.dir = Util.norm(plugin.dir)
dir = Util.norm(plugin.dir)
-- local plugin
plugin.name = plugin.name or Spec.get_name(plugin.dir)
elseif plugin.url then
Expand All @@ -99,16 +102,6 @@ function Spec:add(plugin, results)
end
end
end
-- dev plugins
if
plugin.dev
and (not Config.options.dev.fallback or vim.fn.isdirectory(Config.options.dev.path .. "/" .. plugin.name) == 1)
then
plugin.dir = Config.options.dev.path .. "/" .. plugin.name
else
-- remote plugin
plugin.dir = Config.options.root .. "/" .. plugin.name
end
elseif is_ref then
plugin.name = plugin[1]
else
Expand All @@ -121,6 +114,17 @@ function Spec:add(plugin, results)
return
end

-- dev plugins
if
plugin.dev
and (not Config.options.dev.fallback or vim.fn.isdirectory(Config.options.dev.path .. "/" .. plugin.name) == 1)
then
dir = Config.options.dev.path .. "/" .. plugin.name
elseif plugin.dev == false then
-- explicitely select the default path
dir = Config.options.root .. "/" .. plugin.name
end

if type(plugin.config) == "table" then
self:warn(
"{" .. plugin.name .. "}: setting a table to `Plugin.config` is deprecated. Please use `Plugin.opts` instead"
Expand All @@ -134,12 +138,15 @@ function Spec:add(plugin, results)

M.last_fid = M.last_fid + 1
plugin._ = {
dir = dir,
fid = M.last_fid,
fpid = fpid,
dep = fpid ~= nil,
module = self.importing,
}
self.fragments[plugin._.fid] = plugin
-- remote plugin
plugin.dir = plugin._.dir or (plugin.name and (Config.options.root .. "/" .. plugin.name)) or nil

if fpid then
local parent = self.fragments[fpid]
Expand Down Expand Up @@ -328,11 +335,14 @@ end

function Spec:report(level)
level = level or vim.log.levels.ERROR
local count = 0
for _, notif in ipairs(self.notifs) do
if notif.level >= level then
Util.notify(notif.msg, { level = notif.level })
count = count + 1
end
end
return count
end

---@param spec LazySpec|LazySpecImport
Expand Down Expand Up @@ -448,6 +458,12 @@ function Spec:merge(old, new)
Util.extend(new.dependencies, old.dependencies)
end

local new_dir = new._.dir or old._.dir or (new.name and (Config.options.root .. "/" .. new.name)) or nil
if new_dir ~= new.dir then
self:warn("Plugin `" .. new.name .. "` changed `dir`:\n- from: `" .. new.dir .. "`\n- to: `" .. new_dir .. "`")
end
new.dir = new_dir

new._.super = old
setmetatable(new, { __index = old })

Expand Down
1 change: 1 addition & 0 deletions lua/lazy/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
---@field cond? boolean
---@field super? LazyPlugin
---@field module? string
---@field dir? string Explicit dir or dev set for this plugin

---@alias PluginOpts table|fun(self:LazyPlugin, opts:table):table?

Expand Down
37 changes: 37 additions & 0 deletions tests/core/plugin_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,43 @@ describe("plugin spec url/name", function()
end
end)

describe("plugin spec dir", function()
local tests = {
{
"~/projects/gitsigns.nvim",
{ "lewis6991/gitsigns.nvim", opts = {}, dev = true },
{ "lewis6991/gitsigns.nvim" },
},
{
"~/projects/gitsigns.nvim",
{ "lewis6991/gitsigns.nvim", opts = {}, dev = true },
{ "gitsigns.nvim" },
},
{
"~/projects/gitsigns.nvim",
{ "lewis6991/gitsigns.nvim", opts = {} },
{ "lewis6991/gitsigns.nvim", dev = true },
},
{
"~/projects/gitsigns.nvim",
{ "lewis6991/gitsigns.nvim", opts = {} },
{ "gitsigns.nvim", dev = true },
},
}

for _, test in ipairs(tests) do
local dir = vim.fn.expand(test[1])
local input = vim.list_slice(test, 2)
it("parses dir " .. vim.inspect(input):gsub("%s+", " "), function()
local spec = Plugin.Spec.new(input)
local plugins = vim.tbl_values(spec.plugins)
assert(spec:report() == 0)
assert.equal(1, #plugins)
assert.same(dir, plugins[1].dir)
end)
end
end)

describe("plugin spec opt", function()
it("handles dependencies", function()
Config.options.defaults.lazy = false
Expand Down

0 comments on commit 3dc413d

Please sign in to comment.