marleyvim/nvim/lua/lib/init.lua

50 lines
1.4 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
---Get and format the foreground of a highlight group.
---@param name string The highlight group name to fetch from.
---@return {fg:string}?
function M.fg(name)
local hl = vim.api.nvim_get_hl(0, { name = name, link = false })
return hl and { fg = string.format('#%06x', hl.fg) } or nil
end
---Generates a function that can be used to create which-key mappings.
---@param color string The color to use for the icon.
---@return function
function M.wkSpec(color)
---@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
return M