Skip to content

Commit

Permalink
feat: basic rocks-config.nvim interoperability (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb authored Aug 2, 2024
1 parent b853896 commit e2a2e23
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ path = "~/Projects"

When both `dir` and `dev = true` are present, `dir` gets priority.

## :electric_plug: `rocks-config` interoperability

You can use [`rocks-config.nvim >= 2.0.0`](https://github.com/nvim-neorocks/rocks-config.nvim)
to configure `dev` plugins, however, it does not currently work with [bundles](https://github.com/nvim-neorocks/rocks-config.nvim?tab=readme-ov-file#plugin-bundles).

## :book: License

`rocks-dev.nvim` is licensed under [GPLv3](./LICENSE).
Expand Down
32 changes: 26 additions & 6 deletions lua/rocks-dev/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,45 @@ local rocks = require("rocks.api")

local rocks_dev = {}

-- TODO: Do we want to support [rocks] entries too?

---@class rocks-dev.RocksToml: RocksToml
---@field plugins? table<rock_name, rocks-dev.RockSpec[]> The `[plugins]` entries
---@field dev rocks-dev.Config

---@class rocks-dev.Config
---@field path string Path to install rocks with `dev` = true

---@class rocks-dev.RockSpec: RockSpec
---@field dir? string The install directory
---@field dev? boolean

---@param user_configuration rocks-dev.RocksToml
function rocks_dev.setup(user_configuration)
if not user_configuration or type(user_configuration) ~= "table" then
return
end

local has_rocks_config, rocks_config = pcall(require, "rocks-config")
local config_hook = has_rocks_config and type(rocks_config.configure) == "function" and rocks_config.configure
or function(_) end

local dev_path = user_configuration.dev and user_configuration.dev.path

for _, data in pairs(user_configuration.plugins or {}) do
for _, rock_spec in pairs(user_configuration.plugins or {}) do
---@cast rock_spec rocks-dev.RockSpec
local path
if data.dir then
path = vim.fn.expand(data.dir)
elseif dev_path and data.dev then
path = vim.fn.expand(vim.fs.joinpath(dev_path, data.name))
if rock_spec.dir then
path = vim.fn.expand(rock_spec.dir)
elseif dev_path and rock_spec.dev then
path = vim.fn.expand(vim.fs.joinpath(dev_path, rock_spec.name))
end
if path then
vim.opt.runtimepath:append(path)
config_hook(rock_spec.name)

-- NOTE: We can't support `opt` for dev plugins,
-- as it doesn't integrate with `:Rocks packadd`
-- as it doesn't integrate with `:packadd`
require("rtp_nvim").source_rtp_dir(path)
end
end
Expand Down
14 changes: 14 additions & 0 deletions lua/rocks-dev/rocks/hooks/preload.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---@type rocks.hooks.RockSpecModifier
return {
type = "RockSpecModifier",
---@param rock rocks-dev.RockSpec
hook = function(rock)
if rock.dev or rock.dir then
-- this prevents rocks.nvim/rocks-config.nvim
-- from trying to load the plugin and its configs, respectively.
-- rocks-dev.nvim invokes rocks-config's `configure` API before loading a rock.
rock.opt = true
end
return rock
end,
}
1 change: 1 addition & 0 deletions plugin/rocks-dev.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ if not ok then
end

local user_configuration = api.get_rocks_toml()
---@cast user_configuration rocks-dev.RocksToml

require("rocks-dev").setup(user_configuration)

Expand Down

0 comments on commit e2a2e23

Please sign in to comment.