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): install function for installing rocks with a callback #217

Merged
merged 2 commits into from
Mar 23, 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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ you can easily install them with rocks.nvim: `:Rocks install tree-sitter-<lang>`
They come bundled with queries, so once installed,
all you need to do is run `vim.treesitter.start()` to enable syntax highlighting[^3].

Or, you can use our [`rocks-treesitter.nvim`](https://github.com/nvim-neorocks/rocks-treesitter.nvim)
module, which can automatically install parsers and enable syntax highlighting for you.

[^3]: You can put this in a `ftplugin/<filetype>.lua`, for example.
[nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter) is
still required for tree-sitter based folding, indentation, etc.,
Expand Down Expand Up @@ -324,6 +327,8 @@ Following are some examples:
Adds an API for safely loading plugin configurations.
- [`rocks-dev.nvim`](https://github.com/nvim-neorocks/rocks-dev.nvim):
Adds an API for developing and testing luarocks plugins locally.
- [`rocks-treesitter.nvim`](https://github.com/nvim-neorocks/rocks-treesitter.nvim)
Automatic highlighting and installation of tree-sitter parsers.

To extend `rocks.nvim`, simply install a module with `:Rocks install`,
and you're good to go!
Expand Down
10 changes: 10 additions & 0 deletions doc/rocks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ api.source_runtime_dir({dir}) *api.source_runtime_dir*
{dir} (string) The runtime directory to source


*api.install*
api.install({rock_name}, {version?}, {callback?})
Invoke ':Rocks install' with a callback

Parameters: ~
{rock_name} (rock_name) #The rock name
{version?} (string) The version of the rock to use
{callback?} (fun(rock:Rock)) Invoked upon successful completion


==============================================================================
rocks.nvim API hooks *rocks.api.hooks*

Expand Down
8 changes: 8 additions & 0 deletions lua/rocks/api/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,12 @@ function api.source_runtime_dir(dir)
runtime.source_rtp_dir(dir)
end

---Invoke ':Rocks install' with a callback
---@param rock_name rock_name #The rock name
---@param version? string The version of the rock to use
---@param callback? fun(rock: Rock) Invoked upon successful completion
function api.install(rock_name, version, callback)
operations.add({}, rock_name, version, callback)
end

return api
7 changes: 6 additions & 1 deletion lua/rocks/operations/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,8 @@ end
---@param arg_list string[] #Argument list, potentially used by external handlers
---@param rock_name rock_name #The rock name
---@param version? string #The version of the rock to use
operations.add = function(arg_list, rock_name, version)
---@param callback? fun(rock: Rock)
operations.add = function(arg_list, rock_name, version, callback)
local progress_handle = progress.handle.create({
title = "Installing",
lsp_client = { name = constants.ROCKS_NVIM },
Expand Down Expand Up @@ -540,6 +541,7 @@ operations.add = function(arg_list, rock_name, version)
progress_handle:cancel()
return
end
---@cast installed_rock Rock
progress_handle:report({
title = "Installation successful",
message = ("%s -> %s"):format(installed_rock.name, installed_rock.version),
Expand Down Expand Up @@ -569,6 +571,9 @@ operations.add = function(arg_list, rock_name, version)
fs.write_file(config.config_path, "w", tostring(user_rocks))
if success then
progress_handle:finish()
if callback then
callback(installed_rock)
end
else
progress_handle:cancel()
end
Expand Down
Loading