feat(nvim): refactor autocommand
This commit is contained in:
parent
4acc3621cb
commit
a6a2a3318e
|
@ -1,4 +1,5 @@
|
||||||
local map = require("utility").keymap
|
local U = require("utility")
|
||||||
|
local map, augroup, autocmd = U.keymap, U.augroup, U.autocmd
|
||||||
local n, t, nv = map 'n', map 't', map {'n', 'v'}
|
local n, t, nv = map 'n', map 't', map {'n', 'v'}
|
||||||
|
|
||||||
|
|
||||||
|
@ -28,10 +29,7 @@ n "]d" (vim.diagnostic.goto_next) "Next diagnostic message"
|
||||||
n "<leader>dl" (vim.diagnostic.setloclist) "Diagnostic: list messages"
|
n "<leader>dl" (vim.diagnostic.setloclist) "Diagnostic: list messages"
|
||||||
|
|
||||||
-- LSP
|
-- LSP
|
||||||
vim.api.nvim_create_autocmd('LspAttach', {
|
autocmd "LspAttach" {"LSP actions", group=augroup "UserLspConfig"} (function(ev)
|
||||||
group = vim.api.nvim_create_augroup('UserLspConfig', {}),
|
|
||||||
desc = "LSP actions",
|
|
||||||
callback = function(ev)
|
|
||||||
-- Enable completion triggered by <c-x><c-o>
|
-- Enable completion triggered by <c-x><c-o>
|
||||||
vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'
|
vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'
|
||||||
|
|
||||||
|
@ -57,6 +55,5 @@ vim.api.nvim_create_autocmd('LspAttach', {
|
||||||
n "<leader>lr" (vim.lsp.buf.rename) {"Rename identifier", buffer}
|
n "<leader>lr" (vim.lsp.buf.rename) {"Rename identifier", buffer}
|
||||||
nv "<leader>la" (vim.lsp.buf.code_action) {"Show code actions", buffer}
|
nv "<leader>la" (vim.lsp.buf.code_action) {"Show code actions", buffer}
|
||||||
n "<leader>lf" (format) {"Format file", buffer}
|
n "<leader>lf" (format) {"Format file", buffer}
|
||||||
end,
|
end)
|
||||||
})
|
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ local M = {}
|
||||||
|
|
||||||
local def_opts = {noremap = true, silent = true}
|
local def_opts = {noremap = true, silent = true}
|
||||||
|
|
||||||
|
|
||||||
---Loads plugin config
|
---Loads plugin config
|
||||||
---@param path string
|
---@param path string
|
||||||
function M.plugin(path)
|
function M.plugin(path)
|
||||||
|
@ -13,11 +14,12 @@ function M.plugin(path)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---@class KeymapOpts
|
---@class KeymapOpts
|
||||||
---@field [1] string? Shorthand description
|
---@field [1]? string Shorthand description
|
||||||
---@field desc string? Description
|
---@field desc? string Description
|
||||||
---@field [2] number|boolean? Shorthand buffer
|
---@field [2]? number|boolean Shorthand buffer
|
||||||
---@field buffer number|boolean? Buffer
|
---@field buffer? number|boolean Buffer
|
||||||
|
|
||||||
---Wrapper around `vim.keymap.set`
|
---Wrapper around `vim.keymap.set`
|
||||||
---@param modes string|string[]
|
---@param modes string|string[]
|
||||||
|
@ -28,11 +30,11 @@ function M.keymap(modes)
|
||||||
return function(rhs)
|
return function(rhs)
|
||||||
---@param opts string|KeymapOpts
|
---@param opts string|KeymapOpts
|
||||||
return function(opts)
|
return function(opts)
|
||||||
local supplied = type(opts)
|
local type = type(opts)
|
||||||
local options
|
local options
|
||||||
if supplied == "string" then
|
if type == "string" then
|
||||||
options = vim.tbl_extend("force", def_opts, {desc=opts})
|
options = vim.tbl_extend("force", def_opts, {desc=opts})
|
||||||
elseif supplied == "table" then
|
elseif type == "table" then
|
||||||
opts.desc = opts[1] or opts.desc
|
opts.desc = opts[1] or opts.desc
|
||||||
opts.buffer = opts[2] or opts.buffer
|
opts.buffer = opts[2] or opts.buffer
|
||||||
opts[1], opts[2] = nil, nil
|
opts[1], opts[2] = nil, nil
|
||||||
|
@ -44,4 +46,46 @@ function M.keymap(modes)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
---@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
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
Loading…
Reference in a new issue