Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): emit user events for cache updates #500

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 deletions doc/rocks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,25 +135,38 @@ RocksOpts *RocksOpts*
==============================================================================
rocks.nvim |User| |event|s *rocks.user-event*

The following |User| |event|s are available:

RocksInstallPost Invoked after installing or updating a rock
The `data` is of type
|rocks.user-events.data.RocksInstallPost|.

RocksCachePopulated Invoked when the luarocks rocks cache has been populated.
The `data` is a reference to the cached rocks,
of type `table<rock_name, Rock[]>`

RocksRemovableRocksCachePopulated Invoked when the removable rocks cache has been populated.
The `data` is a reference to the rock names,
of type `string[]`

RocksOutdatedRocksCachePopulated Invoked when the outdated rocks cache has been populated.
The `data` is a reference to the outdated rocks,
of type `table<rock_name, OutdatedRock>`.


To create an autocommand for an event:
>lua
vim.api.nvim_create_autocmd("User", {
pattern = "RocksInstallPost",
callback = function(ev)
---@type rocks.user-events.data.RocksInstallPost
local data = ev.data
-- ...
end,
})
<

rocks.user-events.data.RocksInstallPost*rocks.user-events.data.RocksInstallPost*
The following |User| |event|s are available:

RocksInstallPost Invoked after installing or updating a rock
The `data` is of type
|rocks.user-events.data.RocksInstallPost|.


To create an autocommand for an event:
>lua
vim.api.nvim_create_autocmd("User", {
pattern = "RocksInstallPost",
callback = function(ev)
---@type rocks.user-events.data.RocksInstallPost
local data = ev.data
-- ...
end,
})
<

Fields: ~
{spec} (RockSpec)
Expand Down
21 changes: 21 additions & 0 deletions lua/rocks/cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ cache.populate_cached_rocks = nio.create(function()
luarocks.search_all(function(rocks)
if not vim.tbl_isempty(rocks) then
_cached_rocks = rocks
vim.schedule(function()
vim.api.nvim_exec_autocmds("User", {
pattern = "RocksCachePopulated",
modeline = false,
data = _cached_rocks,
})
end)
end
end, {
dev = true,
Expand All @@ -84,6 +91,13 @@ cache.populate_removable_rock_cache = nio.create(function()
return
end
_removable_rock_cache = state.query_removable_rocks()
vim.schedule(function()
vim.api.nvim_exec_autocmds("User", {
pattern = "RocksRemovableRocksCachePopulated",
modeline = false,
data = _removable_rock_cache,
})
end)
end)

---Tries to get the cached removable rocks.
Expand All @@ -107,6 +121,13 @@ cache.populate_outdated_rock_cache = nio.create(function()
return
end
_outdated_rock_cache = state.outdated_rocks()
vim.schedule(function()
vim.api.nvim_exec_autocmds("User", {
pattern = "RocksOutdatedRocksCachePopulated",
modeline = false,
data = _outdated_rock_cache,
})
end)
end)

---Populate all rocks state caches
Expand Down
19 changes: 16 additions & 3 deletions lua/rocks/meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,21 @@
---@brief [[
---The following |User| |event|s are available:
---
---RocksInstallPost Invoked after installing or updating a rock
--- The `data` is of type
--- |rocks.user-events.data.RocksInstallPost|.
---RocksInstallPost Invoked after installing or updating a rock
--- The `data` is of type
--- |rocks.user-events.data.RocksInstallPost|.
---
---RocksCachePopulated Invoked when the luarocks rocks cache has been populated.
--- The `data` is a reference to the cached rocks,
--- of type `table<rock_name, Rock[]>`
---
---RocksRemovableRocksCachePopulated Invoked when the removable rocks cache has been populated.
--- The `data` is a reference to the rock names,
--- of type `string[]`
---
---RocksOutdatedRocksCachePopulated Invoked when the outdated rocks cache has been populated.
--- The `data` is a reference to the outdated rocks,
--- of type `table<rock_name, OutdatedRock>`.
---
---
---To create an autocommand for an event:
Expand All @@ -27,6 +39,7 @@
--- end,
--- })
---<
---@brief ]]

---@class rocks.user-events.data.RocksInstallPost
---@field spec RockSpec
Expand Down
Loading