marleyvim/nvim/lua/lib/init.lua
2024-12-04 21:35:35 -08:00

65 lines
1.6 KiB
Lua

---@class lib
local M = {}
---Find the root of a project based on `vim.g.root_spec`. Defaults to
---`{ '.git' }` if unset.
---@return string?
function M.root()
local root_spec = vim.g.root_spec or { '.git' }
return vim.fs.root(0, root_spec)
end
---Require a file relative to the given prefix, to avoid repetition.
---@param prefix string The string to prefix to all req calls.
function M.local_require(prefix)
---@param mod string The module to require.
return function(mod)
return require(prefix .. '.' .. mod)
end
end
---Generates a function that can be used to create which-key mappings.
---@param color string The color to use for the icon.
---@param setRhs? boolean Whether to allow setting the rhs.
---@return function
function M.wkSpec(color, setRhs)
if setRhs then
---@param lhs string
---@param rhs string | fun()
---@param icon string | wk.Icon
---@param opts? wk.Spec
return function(lhs, rhs, icon, opts)
if type(icon) == 'string' then
icon = { icon = icon, color = color }
else
icon = vim.tbl_deep_extend('force', icon, { color = color })
end
return vim.tbl_deep_extend(
'force',
{ lhs, rhs, icon = icon },
(opts or {})
)
end
end
---@param lhs string
---@param icon string | wk.Icon
---@param opts? wk.Spec
return function(lhs, icon, opts)
if type(icon) == 'string' then
icon = { icon = icon, color = color }
else
icon = vim.tbl_deep_extend('force', icon, { color = color })
end
return vim.tbl_deep_extend(
'force',
{ lhs, icon = { icon = icon, color = color } },
(opts or {})
)
end
end
return M