Skip to content

Commit 7f62388

Browse files
committed
refactor: move pack_spec creation from import to merge phase
- Remove pack_spec creation from import.lua (just registers specs now) - Create pack_specs in merge.resolve_all() with correct merged data - Return sorted vim_packs array from resolve_all() - Fix KeySpec type annotation in utils.lua
1 parent b0e80e8 commit 7f62388

File tree

6 files changed

+70
-51
lines changed

6 files changed

+70
-51
lines changed

README.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,16 @@ A thin layer on top of Neovim's native `vim.pack`, adding support for lazy-loadi
1010
```lua
1111
-- ./lua/plugins/fundo.lua
1212
return {
13-
{
14-
'kevinhwang91/nvim-fundo',
15-
dependencies = { "kevinhwang91/promise-async" },
16-
cond = not vim.g.vscode,
17-
version = 'main',
18-
build = function() require('fundo').install() end,
19-
opts = {},
20-
config = function(_, opts)
21-
vim.o.undofile = true
22-
require('fundo').setup(opts)
23-
end,
24-
},
13+
'kevinhwang91/nvim-fundo',
14+
dependencies = { "kevinhwang91/promise-async" },
15+
cond = not vim.g.vscode,
16+
version = 'main',
17+
build = function() require('fundo').install() end,
18+
opts = {},
19+
config = function(_, opts)
20+
vim.o.undofile = true
21+
require('fundo').setup(opts)
22+
end,
2523
}
2624
```
2725

@@ -46,23 +44,23 @@ vim.pack.add({ 'https://github.com/zuqini/zpack.nvim' })
4644
vim.g.mapleader = " "
4745
vim.g.maplocalleader = "\\"
4846

49-
-- automatically import specs from `/lua/plugins/`
47+
-- automatically import specs from `./lua/plugins/`
5048
require('zpack').setup()
5149

52-
-- or import from a custom directory e.g. `/lua/a/b/plugins/`
50+
-- or import from a custom directory e.g. `./lua/a/b/plugins/`
5351
require('zpack').setup({ { import = 'a.b.plugins' } })
5452

5553
-- or add your specs inline in setup
5654
require('zpack').setup({
5755
{ 'neovim/nvim-lspconfig', config = function() ... end },
58-
{ import = 'plugins.mini' }, -- additionally import from `/lua/plugins/mini/`
56+
{ import = 'plugins.mini' }, -- additionally import from `./lua/plugins/mini/`
5957
})
6058

6159
-- or via the spec field
6260
require('zpack').setup({
6361
spec = {
6462
{ 'neovim/nvim-lspconfig', config = function() ... end },
65-
{ import = 'plugins.mini' }, -- additionally import from `/lua/plugins/mini/`
63+
{ import = 'plugins.mini' },
6664
},
6765
})
6866
```
@@ -127,15 +125,15 @@ Plugin-level settings always take precedence over `defaults`.
127125
Neovim 0.12+ includes a built-in package manager (`vim.pack`) that handles plugin installation, updates, and version management. zpack is a thin layer that adds lazy-loading capabilities and support for a lazy.nvim-like declarative spec while completely leveraging the native infrastructure.
128126

129127
#### Features
130-
- z***pack*** is completely native
131-
- Install and manage your plugins _(including zpack)_ all within `vim.pack`.
132-
- ⚡pack is "batteries included":
133-
- Add plugins using the same lazy.nvim spec provided by plugin authors you know and love, with minimal configuration
134-
- 💤pack powers up `vim.pack` without the bells and whistles
128+
- [z***pack***] is completely native
129+
- Install and manage your plugins _(including zpack)_ all within `vim.pack`
130+
- [🔋pack] is "batteries included"
131+
- Add plugins using the same lazy.nvim spec provided by plugin authors you know and love
132+
- Minimal configurations necessary
133+
- [💤pack] powers up `vim.pack` without the bells and whistles
134+
- Powerful lazy-loading triggers
135135
- Build triggers for installation/updates
136136
- Basic plugin management commands
137-
- Powerful lazy-loading triggers
138-
139137

140138
zpack might be for you if:
141139
- you're a lazy.nvim user, love its declarative spec, and its wide adoption by plugin authors, but you don't need most of its advanced features
@@ -150,6 +148,8 @@ As a thin layer, zpack does not provide:
150148
- UI dashboard for your plugins
151149
- Advanced profiling, dev mode, change-detection, etc.
152150

151+
If you're a lazy.nvim user, see [Migrating from lazy.nvim](#migrating-from-lazynvim)
152+
153153
## Examples
154154
For more examples, refer to my personal config:
155155
- [zpack installation and setup](https://github.com/zuqini/nvim/blob/main/init.lua)
@@ -347,6 +347,7 @@ return {
347347
{ 'nvim-lua/plenary.nvim' },
348348
{ 'nvim-tree/nvim-web-devicons' },
349349
{ 'nvim-lualine/lualine.nvim', opts = { theme = 'auto' } },
350+
{ import = 'plugins.mini' },
350351
}
351352
```
352353

doc/zpack.txt

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,16 @@ lazy-loading and the widely adopted lazy.nvim-like declarative spec.
1414
>lua
1515
-- ./lua/plugins/fundo.lua
1616
return {
17-
{
18-
'kevinhwang91/nvim-fundo',
19-
dependencies = { "kevinhwang91/promise-async" },
20-
cond = not vim.g.vscode,
21-
version = 'main',
22-
build = function() require('fundo').install() end,
23-
opts = {},
24-
config = function(_, opts)
25-
vim.o.undofile = true
26-
require('fundo').setup(opts)
27-
end,
28-
},
17+
'kevinhwang91/nvim-fundo',
18+
dependencies = { "kevinhwang91/promise-async" },
19+
cond = not vim.g.vscode,
20+
version = 'main',
21+
build = function() require('fundo').install() end,
22+
opts = {},
23+
config = function(_, opts)
24+
vim.o.undofile = true
25+
require('fundo').setup(opts)
26+
end,
2927
}
3028
<
3129

@@ -51,23 +49,23 @@ Install zpack.nvim via vim.pack.add():
5149
vim.g.mapleader = " "
5250
vim.g.maplocalleader = "\\"
5351

54-
-- automatically import specs from `/lua/plugins/`
52+
-- automatically import specs from `./lua/plugins/`
5553
require('zpack').setup()
5654

57-
-- or import from a custom directory e.g. `/lua/a/b/plugins/`
55+
-- or import from a custom directory e.g. `./lua/a/b/plugins/`
5856
require('zpack').setup({ { import = 'a.b.plugins' } })
5957

6058
-- or add your specs inline in setup
6159
require('zpack').setup({
6260
{ 'neovim/nvim-lspconfig', config = function() ... end },
63-
{ import = 'plugins.mini' }, -- additionally import from `/lua/plugins/mini/`
61+
{ import = 'plugins.mini' }, -- additionally import from `./lua/plugins/mini/`
6462
})
6563

6664
-- or via the spec field
6765
require('zpack').setup({
6866
spec = {
6967
{ 'neovim/nvim-lspconfig', config = function() ... end },
70-
{ import = 'plugins.mini' }, -- additionally import from `/lua/plugins/mini/`
68+
{ import = 'plugins.mini' },
7169
},
7270
})
7371
<
@@ -216,7 +214,19 @@ performance (table, optional)
216214
Neovim 0.12+ includes a built-in package manager (`vim.pack`) that handles
217215
plugin installation, updates, and version management. zpack is a thin layer
218216
that adds lazy-loading capabilities and support for a lazy.nvim-like
219-
declarative spec while leveraging the native infrastructure.
217+
declarative spec while completely leveraging the native infrastructure.
218+
219+
Features:
220+
- completely native
221+
- Install and manage your plugins (including zpack) all within `vim.pack`
222+
- batteries included
223+
- Add plugins using the same lazy.nvim spec provided by plugin authors
224+
you know and love
225+
- Minimal configurations necessary
226+
- powers up `vim.pack` without the bells and whistles
227+
- Powerful lazy-loading triggers
228+
- Build triggers for installation/updates
229+
- Basic plugin management commands
220230

221231
zpack might be for you if:
222232
- you're a lazy.nvim user, love its declarative spec, and its wide adoption
@@ -235,6 +245,8 @@ As a thin layer, zpack does not provide:
235245
- UI dashboard for your plugins
236246
- Advanced profiling, dev mode, change-detection, etc.
237247

248+
If you're a lazy.nvim user, see |zpack-migrating-lazy|.
249+
238250
Although you can achieve most of these through other means. See
239251
|zpack-migrating-lazy|. If something you need isn't achievable natively or
240252
through zpack, please submit an issue or PR!
@@ -449,6 +461,7 @@ Define multiple plugins in a single file:
449461
{ 'nvim-lua/plenary.nvim' },
450462
{ 'nvim-tree/nvim-web-devicons' },
451463
{ 'nvim-lualine/lualine.nvim', opts = { theme = 'auto' } },
464+
{ import = 'plugins.mini' },
452465
}
453466
<
454467

lua/zpack/import.lua

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,6 @@ M.import_specs = function(spec_item_or_list, ctx)
169169
table.insert(state.spec_registry[src].specs, spec)
170170
else
171171
state.spec_registry[src] = { specs = { spec }, load_status = "pending" }
172-
local pack_spec = { src = src, version = utils.normalize_version(spec), name = spec.name }
173-
table.insert(ctx.vim_packs, pack_spec)
174-
state.src_to_pack_spec[src] = pack_spec
175172
end
176173

177174
if spec.dependencies then

lua/zpack/init.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ local M = {}
1616
local function create_context(opts)
1717
opts = opts or {}
1818
return {
19-
vim_packs = {},
2019
src_with_init = {},
2120
registered_startup_packs = {},
2221
registered_lazy_packs = {},
@@ -70,7 +69,7 @@ local process_all = function(ctx)
7069
local state = require('zpack.state')
7170

7271
vim.api.nvim_clear_autocmds({ group = state.lazy_build_group })
73-
require('zpack.merge').resolve_all()
72+
ctx.vim_packs = require('zpack.merge').resolve_all()
7473
hooks.setup_build_tracking()
7574
require('zpack.registration').register_all(ctx)
7675

lua/zpack/merge.lua

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,22 +231,31 @@ function M.resolve_opts(specs, plugin)
231231
end
232232

233233
---Pre-compute merged_spec for all entries in the registry
234-
---Also updates pack_spec.version based on merged specs
234+
---Creates pack_specs with merged data and returns sorted vim_packs array
235+
---@return vim.pack.Spec[]
235236
function M.resolve_all()
236237
local state = require('zpack.state')
237238
local utils = require('zpack.utils')
238239

240+
local vim_packs = {}
241+
239242
for src, entry in pairs(state.spec_registry) do
240243
if entry.specs and #entry.specs > 0 then
241244
entry.sorted_specs = M.sort_specs(entry.specs)
242245
entry.merged_spec = M.merge_spec_array(entry.sorted_specs)
243246

244-
local pack_spec = state.src_to_pack_spec[src]
245-
if pack_spec then
246-
pack_spec.version = utils.normalize_version(entry.merged_spec)
247-
end
247+
local pack_spec = {
248+
src = src,
249+
version = utils.normalize_version(entry.merged_spec),
250+
name = entry.merged_spec.name,
251+
}
252+
table.insert(vim_packs, pack_spec)
253+
state.src_to_pack_spec[src] = pack_spec
248254
end
249255
end
256+
257+
table.sort(vim_packs, utils.compare_priority)
258+
return vim_packs
250259
end
251260

252261
return M

lua/zpack/utils.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ M.normalize_keys = function(keys)
9595
-- Normalize to always be an array
9696
local key_list = (type(keys) == "string" or (keys[1] and type(keys[1]) == "string"))
9797
and { keys }
98-
or keys --[[@as string[]|KeySpec[] ]]
98+
or keys --[[@as string[]|zpack.KeySpec[] ]]
9999

100100
local result = {}
101101
for _, key in ipairs(key_list) do

0 commit comments

Comments
 (0)