8000 fix(main): take a compiled module named like the plugin, as lazy.nvim does by zuqini · Pull Request #58 · zuqini/zpack.nvim · GitHub
[go: up one dir, main page]

Skip to content
Merged
22 changes: 16 additions & 6 deletions doc/zpack.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1112,14 +1112,15 @@ main (string, optional)
The module zpack calls `setup()` on. When unset, zpack works
it out:
1. For mini.* plugins, the plugin name.
2. A module matching the plugin name without its `vim-` or
`nvim-` prefix and its `.vim` or `.nvim` suffix.
2. A module matching the plugin name. In lower case, both
names drop a `vim-` or `nvim-` prefix, a `.vim` or `.nvim`
suffix and any `-lua` or `.lua`, then compare on their
letters alone. A compiled `.so` or `.dll` counts, but a
matching Lua module wins over it: zee-bar.nvim with
`zeebar/` beside `zee_bar.so` gets `zeebar`.
3. Otherwise, as in lazy.nvim, the one module in the plugin's
`lua/`, if there is only one (`null-ls` for none-ls.nvim).
A compiled `.so` or `.dll` counts as a module here, but
only a Lua module is matched against the name in 2:
foo.nvim with `lua/foo.so` beside another module gets
none, where lazy.nvim takes `foo`.
A compiled module counts here too.

Set it when detection fails.

Expand Down Expand Up @@ -1283,6 +1284,15 @@ dependencies (string[])
specs are resolved. Added for lazy.nvim spec drop-in compatibility
(LazyPlugin.dependencies).

*zpack.Plugin.main*
main (string?)
The spec's `main` (|zpack-Spec.main|), set in every hook,
trigger function and `cond`, as in lazy.nvim. Without one,
nil until zpack detects the module, which it does only for a
plugin with `opts` or `config`, after its `opts` functions
and before `config` (or the automatic `setup()`) first runs.
It stays nil if detection finds none.

==============================================================================
9. EVENTSPEC REFERENCE *zpack-eventspec-reference*
*zpack.EventSpec*
Expand Down
2 changes: 1 addition & 1 deletion lua/zpack/module_loader.lua
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ end
---@return boolean
local function claims_topmod(path, topmod)
for _, entry in ipairs(util.lsdir(path .. "/lua")) do
if util.top_module(entry) == topmod or entry.name == topmod .. ".so" or entry.name == topmod .. ".dll" then
if util.top_module(entry) == topmod then
return true
end
end
42 changes: 30 additions & 12 deletions lua/zpack/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,24 @@ end

---The top-level module an entry of a plugin's lua/ provides.
---@param entry zpack.DirEntry
---@return string?
---@return string? name
---@return boolean? compiled
M.top_module = function(entry)
return entry.name:match("^(.+)%.lua$") or ((entry.type == "directory" or entry.type == "link") and entry.name or nil)
local name = entry.name
local len = #name
if len > 3 and name:sub(-3) == ".so" then
return name:sub(1, -4), true
end
if len > 4 then
local ext = name:sub(-4)
if ext == ".lua" then
return name:sub(1, -5)
end
if ext == ".dll" then
return name:sub(1, -5), true
end
end
return (entry.type == "directory" or entry.type == "link") and name or nil
end

---vim.notify's default handler echoes every level, so a debug note waits for
Expand Down Expand Up @@ -355,25 +370,28 @@ M.resolve_main = function(plugin)
local norm_name = M.normalize_name(name)
local lua_dir = plugin.path .. "/lua"

local mods, seen = {}, {}
local mods, seen, named_compiled = {}, {}, nil
for _, dir_entry in ipairs(M.lsdir(lua_dir)) do
local compiled = dir_entry.name:match("^(.+)%.so$") or dir_entry.name:match("^(.+)%.dll$")
local mod = not compiled and M.top_module(dir_entry) or nil
local mod, compiled = M.top_module(dir_entry)
if mod and M.normalize_name(mod) == norm_name then
mods = { mod }
break
if not compiled then
plugin.main = mod
return mod
end
named_compiled = named_compiled or mod
end
mod = mod or compiled
if mod and not seen[mod] then
seen[mod] = true
mods[#mods + 1] = mod
end
end

-- A plugin's only module is its main whatever its name: none-ls.nvim's `null-ls`.
if #mods == 1 then
plugin.main = mods[1]
return mods[1]
-- No Lua module is named like the plugin: take a compiled one that is, else
-- the plugin's only module whatever its name: none-ls.nvim's `null-ls`.
local main = named_compiled or (#mods == 1 and mods[1]) or nil
if main then
plugin.main = main
return main
end

state.resolve_main_not_found[cache_key] = true
Expand Down
19 changes: 19 additions & 0 deletions tests/opts_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,25 @@ describe("plugin.main in config hooks", function()
assert.are.same({ init = 'nil', config = 'falsy' }, seen)
end)

it("the spec's `main` wins over a module detection would find", function()
helpers.write_plugin_files('picked', { 'decoy.lua' })
local called = {}
package.loaded['decoy'] = { setup = function() called[#called + 1] = 'decoy' end }
package.loaded['chosen'] = { setup = function() called[#called + 1] = 'chosen' end }

require('zpack').setup({
spec = {
{ 'test/picked', main = 'chosen', opts = {} },
},
defaults = { confirm = false },
})

helpers.flush_pending()
package.loaded['decoy'] = nil
package.loaded['chosen'] = nil
assert.are.same({ 'chosen' }, called)
end)

it("plugin.main is nil when not detected", function()
local received_main = "not_set"

Expand Down
17 changes: 10 additions & 7 deletions tests/utils_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -257,19 +257,22 @@ describe("resolve_main", function()

it("passes over a compiled module listed before the plugin's Lua one", function()
local utils = require('zpack.utils')
helpers.write_plugin_files('zee-bar', { 'zeebar/init.lua', 'zee_bar.so' })
local path = vim.fn.stdpath('data') .. '/site/pack/zpack/opt/zee-bar'
local original_lsdir = utils.lsdir
utils.lsdir = function(dir)
local entries = vim.deepcopy(original_lsdir(dir))
table.sort(entries, function(a, b) return a.name < b.name end)
return entries
end

local main = utils.resolve_main({ spec = { src = 'https://github.com/test/zee-bar', name = 'zee-bar' }, path = path }, {})
local mains = {}
for name, compiled in pairs({ ['zee-bar'] = 'zee_bar.so', ['zee-bar.nvim'] = 'zee_bar.dll' }) do
helpers.write_plugin_files(name, { 'zeebar/init.lua', compiled })
local path = vim.fn.stdpath('data') .. '/site/pack/zpack/opt/' .. name
mains[compiled] = utils.resolve_main({ spec = { src = 'https://github.com/test/' .. name, name = name }, path = path }, {})
end
utils.lsdir = original_lsdir

assert.are.equal('zeebar', main)
assert.are.same({ ['zee_bar.so'] = 'zeebar', ['zee_bar.dll'] = 'zeebar' }, mains)
end)

local function main_of(name, files)
Expand Down Expand Up @@ -300,9 +303,9 @@ describe("resolve_main", function()
assert.are.equal('native', main_of('compiled-only.nvim', { 'native.so' }))
end)

it("leaves no `main` when only a compiled module is named like the plugin beside a Lua one", function()
assert.is_nil(main_of('foo.nvim', { 'foo.so', 'bar.lua' }))
assert.is_nil(main_of('foo-dll.nvim', { 'foo_dll.dll', 'bar.lua' }))
it("takes a compiled module named like the plugin beside oth 5A26 er modules, as lazy.nvim does", function()
assert.are.equal('foo', main_of('foo.nvim', { 'foo.so', 'bar.lua' }))
assert.are.equal('foo_dll', main_of('foo-dll.nvim', { 'foo_dll.dll', 'bar.lua', 'baz/init.lua' }))
end)

it("reads a symlinked compiled module by its module name, as lazy.nvim does", function()
Expand Down
Loading
0