Skip to content

Commit 0c3d4d4

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 0c3d4d4

File tree

6 files changed

+116
-80
lines changed

6 files changed

+116
-80
lines changed

README.md

Lines changed: 46 additions & 37 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,25 +44,8 @@ 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()
51-
52-
-- or import from a custom directory e.g. `/lua/a/b/plugins/`
53-
require('zpack').setup({ { import = 'a.b.plugins' } })
54-
55-
-- or add your specs inline in setup
56-
require('zpack').setup({
57-
{ 'neovim/nvim-lspconfig', config = function() ... end },
58-
{ import = 'plugins.mini' }, -- additionally import from `/lua/plugins/mini/`
59-
})
60-
61-
-- or via the spec field
62-
require('zpack').setup({
63-
spec = {
64-
{ 'neovim/nvim-lspconfig', config = function() ... end },
65-
{ import = 'plugins.mini' }, -- additionally import from `/lua/plugins/mini/`
66-
},
67-
})
6849
```
6950

7051
### Commands
@@ -122,20 +103,45 @@ require('zpack').setup({
122103

123104
Plugin-level settings always take precedence over `defaults`.
124105

106+
### Importing Specs
107+
108+
```lua
109+
-- automatically import specs from `./lua/plugins/`
110+
require('zpack').setup()
111+
112+
-- or import from a custom directory e.g. `./lua/a/b/plugins/`
113+
require('zpack').setup({ { import = 'a.b.plugins' } })
114+
115+
-- or add your specs inline in setup
116+
require('zpack').setup({
117+
{ 'neovim/nvim-lspconfig', config = function() ... end },
118+
...
119+
{ import = 'plugins.mini' }, -- or additionally import from `./lua/plugins/mini/`
120+
})
121+
122+
-- or via the spec field
123+
require('zpack').setup({
124+
spec = {
125+
{ 'neovim/nvim-lspconfig', config = function() ... end },
126+
...
127+
},
128+
})
129+
```
130+
125131
## Why zpack?
126132

127133
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.
128134

129135
#### 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
136+
- [z***pack***] is completely native
137+
- Install and manage your plugins _(including zpack)_ all within `vim.pack`
138+
- [🔋pack] is "batteries included"
139+
- Add plugins using the same lazy.nvim spec provided by plugin authors you know and love
140+
- Minimal configurations necessary
141+
- [💤pack] powers up `vim.pack` without the bells and whistles
142+
- Powerful lazy-loading triggers
135143
- Build triggers for installation/updates
136144
- Basic plugin management commands
137-
- Powerful lazy-loading triggers
138-
139145

140146
zpack might be for you if:
141147
- 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 +156,8 @@ As a thin layer, zpack does not provide:
150156
- UI dashboard for your plugins
151157
- Advanced profiling, dev mode, change-detection, etc.
152158

159+
If you're a lazy.nvim user, see [Migrating from lazy.nvim](#migrating-from-lazynvim)
160+
153161
## Examples
154162
For more examples, refer to my personal config:
155163
- [zpack installation and setup](https://github.com/zuqini/nvim/blob/main/init.lua)
@@ -347,6 +355,7 @@ return {
347355
{ 'nvim-lua/plenary.nvim' },
348356
{ 'nvim-tree/nvim-web-devicons' },
349357
{ 'nvim-lualine/lualine.nvim', opts = { theme = 'auto' } },
358+
{ import = 'plugins.mini' },
350359
}
351360
```
352361

doc/zpack.txt

Lines changed: 54 additions & 32 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,25 +49,8 @@ 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()
56-
57-
-- or import from a custom directory e.g. `/lua/a/b/plugins/`
58-
require('zpack').setup({ { import = 'a.b.plugins' } })
59-
60-
-- or add your specs inline in setup
61-
require('zpack').setup({
62-
{ 'neovim/nvim-lspconfig', config = function() ... end },
63-
{ import = 'plugins.mini' }, -- additionally import from `/lua/plugins/mini/`
64-
})
65-
66-
-- or via the spec field
67-
require('zpack').setup({
68-
spec = {
69-
{ 'neovim/nvim-lspconfig', config = function() ... end },
70-
{ import = 'plugins.mini' }, -- additionally import from `/lua/plugins/mini/`
71-
},
72-
})
7354
<
7455

7556
------------------------------------------------------------------------------
@@ -162,7 +143,33 @@ Each file returns a spec or list of specs (see |zpack-examples| or
162143
Plugin-level settings always take precedence over `defaults`.
163144

164145
------------------------------------------------------------------------------
165-
4.4 SETUP OPTIONS *zpack-setup-options*
146+
4.4 IMPORTING SPECS *zpack-importing-specs*
147+
148+
>lua
149+
-- automatically import specs from `./lua/plugins/`
150+
require('zpack').setup()
151+
152+
-- or import from a custom directory e.g. `./lua/a/b/plugins/`
153+
require('zpack').setup({ { import = 'a.b.plugins' } })
154+
155+
-- or add your specs inline in setup
156+
require('zpack').setup({
157+
{ 'neovim/nvim-lspconfig', config = function() ... end },
158+
...
159+
{ import = 'plugins.mini' }, -- or additionally import from `./lua/plugins/mini/`
160+
})
161+
162+
-- or via the spec field
163+
require('zpack').setup({
164+
spec = {
165+
{ 'neovim/nvim-lspconfig', config = function() ... end },
166+
...
167+
},
168+
})
169+
<
170+
171+
------------------------------------------------------------------------------
172+
4.5 SETUP OPTIONS *zpack-setup-options*
166173

167174
The `setup()` function accepts the following options:
168175

@@ -216,7 +223,19 @@ performance (table, optional)
216223
Neovim 0.12+ includes a built-in package manager (`vim.pack`) that handles
217224
plugin installation, updates, and version management. zpack is a thin layer
218225
that adds lazy-loading capabilities and support for a lazy.nvim-like
219-
declarative spec while leveraging the native infrastructure.
226+
declarative spec while completely leveraging the native infrastructure.
227+
228+
Features:
229+
- completely native
230+
- Install and manage your plugins (including zpack) all within `vim.pack`
231+
- batteries included
232+
- Add plugins using the same lazy.nvim spec provided by plugin authors
233+
you know and love
234+
- Minimal configurations necessary
235+
- powers up `vim.pack` without the bells and whistles
236+
- Powerful lazy-loading triggers
237+
- Build triggers for installation/updates
238+
- Basic plugin management commands
220239

221240
zpack might be for you if:
222241
- you're a lazy.nvim user, love its declarative spec, and its wide adoption
@@ -235,6 +254,8 @@ As a thin layer, zpack does not provide:
235254
- UI dashboard for your plugins
236255
- Advanced profiling, dev mode, change-detection, etc.
237256

257+
If you're a lazy.nvim user, see |zpack-migrating-lazy|.
258+
238259
Although you can achieve most of these through other means. See
239260
|zpack-migrating-lazy|. If something you need isn't achievable natively or
240261
through zpack, please submit an issue or PR!
@@ -449,6 +470,7 @@ Define multiple plugins in a single file:
449470
{ 'nvim-lua/plenary.nvim' },
450471
{ 'nvim-tree/nvim-web-devicons' },
451472
{ 'nvim-lualine/lualine.nvim', opts = { theme = 'auto' } },
473+
{ import = 'plugins.mini' },
452474
}
453475
<
454476

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)