Skip to content

Commit

Permalink
feat(packadd): Fall back to builtin packadd if no rock is found. (#…
Browse files Browse the repository at this point in the history
…114)

* chore(packadd): default `error_on_not_found` to `false`

* feat(packadd): Fall back to builtin `packadd` if no rock is found.
  • Loading branch information
mrcjkb authored Jan 14, 2024
1 parent ac414fd commit 816b916
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
5 changes: 3 additions & 2 deletions doc/rocks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ rocks.nvim commands *rocks.commands*
The rock must be installed by luarocks.
It is added to the 'runtimepath' if it wasn't there yet.
If `Rocks` is called with the optional `!`, the rock is added
to the 'runtimepath' and no 'plugin' or 'ftdetect' scripts are
to the |runtimepath| and no |plugin| or |ftdetect| scripts are
sourced.
This command aims to behave similarly to the builtin 'packadd'.
This command aims to behave similarly to the builtin |packadd|,
and will fall back to it if no rock is found.
To make a rock optional, set `opt = true` in `rocks.toml`.
log Open the log file.

Expand Down
5 changes: 3 additions & 2 deletions lua/rocks/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
--- The rock must be installed by luarocks.
--- It is added to the 'runtimepath' if it wasn't there yet.
--- If `Rocks` is called with the optional `!`, the rock is added
--- to the 'runtimepath' and no 'plugin' or 'ftdetect' scripts are
--- to the |runtimepath| and no |plugin| or |ftdetect| scripts are
--- sourced.
--- This command aims to behave similarly to the builtin 'packadd'.
--- This command aims to behave similarly to the builtin |packadd|,
--- and will fall back to it if no rock is found.
--- To make a rock optional, set `opt = true` in `rocks.toml`.
--- log Open the log file.
---
Expand Down
10 changes: 7 additions & 3 deletions lua/rocks/runtime.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ end

---@class (exact) PackaddOpts
---@field bang? boolean
---@field error_on_not_found? boolean
---@field packadd_fallback? boolean Fall back to the builtin |packadd|? Default `true`.
---@field error_on_not_found? boolean Notify with an error message if no plugin could be found. Ignored if `packadd_fallback` is set to `true`.

---@param rock_name rock_name
---@param opts? PackaddOpts
Expand All @@ -120,7 +121,8 @@ function runtime.packadd(rock_name, opts)
---@cast opts table
opts = vim.tbl_deep_extend("force", {
bang = false,
error_on_not_found = true,
packadd_fallback = true,
error_on_not_found = false,
}, opts or {})
local rtp_glob = mk_rtp_glob(rock_name)
rtp_append(rtp_glob)
Expand All @@ -129,7 +131,9 @@ function runtime.packadd(rock_name, opts)
end
local paths = vim.fn.glob(rtp_glob, nil, true)
if #paths == 0 then
if opts.error_on_not_found then
if opts.packadd_fallback then
vim.cmd.packadd({ rock_name, bang = opts.bang })
elseif opts.error_on_not_found then
vim.notify(("No path found for %s"):format(rock_name), vim.log.levels.ERROR)
end
return
Expand Down

0 comments on commit 816b916

Please sign in to comment.