dotfiles/nvim/lua/utility.lua

92 lines
2.2 KiB
Lua
Raw Normal View History

2023-09-02 22:09:00 +02:00
local M = {}
local def_opts = {noremap = true, silent = true}
2023-09-05 18:33:45 +02:00
2023-09-02 22:09:00 +02:00
---Loads plugin config
---@param path string
function M.plugin(path)
local loaded = require(path)
---@param meta table
---@return table
return function(meta)
return vim.tbl_extend("keep", meta, loaded)
end
end
2023-09-05 18:33:45 +02:00
---@class KeymapOpts
2023-09-05 18:33:45 +02:00
---@field [1]? string Shorthand description
---@field desc? string Description
---@field [2]? number|boolean Shorthand buffer
---@field buffer? number|boolean Buffer
2023-09-02 22:09:00 +02:00
---Wrapper around `vim.keymap.set`
---@param modes string|string[]
2023-09-04 23:01:56 +02:00
function M.keymap(modes)
2023-09-02 22:09:00 +02:00
---@param lhs string
return function(lhs)
---@param rhs string|function
return function(rhs)
---@param opts string|KeymapOpts
2023-09-02 22:09:00 +02:00
return function(opts)
2023-09-05 18:33:45 +02:00
local type = type(opts)
2023-09-02 22:09:00 +02:00
local options
2023-09-05 18:33:45 +02:00
if type == "string" then
2023-09-02 22:09:00 +02:00
options = vim.tbl_extend("force", def_opts, {desc=opts})
2023-09-05 18:33:45 +02:00
elseif type == "table" then
opts.desc = opts[1] or opts.desc
opts.buffer = opts[2] or opts.buffer
opts[1], opts[2] = nil, nil
2023-09-02 22:09:00 +02:00
options = vim.tbl_extend("force", def_opts, opts)
end
vim.keymap.set(modes, lhs, rhs, options)
end
end
end
end
2023-09-05 18:33:45 +02:00
---@class AugroupOpts
---@field [1] string Name
---@field clear? boolean Clear existing commands if the group already exists
---Wrapper around `vim.api.nvim_create_augroup`
---@param opts string|AugroupOpts
---@return number
function M.augroup(opts)
local type = type(opts)
local name, options
if type == "string" then
name, options = opts, {}
elseif type == "table" then
name, options = opts[1], opts
options[1] = nil
end
return vim.api.nvim_create_augroup(name, options)
end
---@class AutocmdOpts
---@field [1]? string Shorthand desc
---@field desc? string Description for this autocommand
---@field group? string|number
---@field callback? function
---Wrapper around `vim.api.nvim_create_autocmd`
---@param event string|string[]
function M.autocmd(event)
---@param opts AutocmdOpts
return function(opts)
opts.desc = opts[1] or opts.desc
opts[1] = nil
---@param callback? function
return function(callback)
opts.callback = callback
vim.api.nvim_create_autocmd(event, opts)
end
end
end
2023-09-02 22:09:00 +02:00
return M