feat(nvim): refactor autocommand

This commit is contained in:
Maciej Jur 2023-09-05 18:33:45 +02:00
parent 4acc3621cb
commit a6a2a3318e
Signed by: kamov
GPG key ID: 191CBFF5F72ECAFD
2 changed files with 77 additions and 36 deletions

View file

@ -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,35 +29,31 @@ 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', {}), -- Enable completion triggered by <c-x><c-o>
desc = "LSP actions", vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'
callback = function(ev)
-- Enable completion triggered by <c-x><c-o>
vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'
local function workspaces_list() local function workspaces_list()
print(vim.inspect(vim.lsp.buf.list_workspace_folders())) print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end end
local function format() local function format()
vim.lsp.buf.format({async = true}) vim.lsp.buf.format({async = true})
end end
local buffer = ev.buf local buffer = ev.buf
n "K" (vim.lsp.buf.hover) {"Show hover help", buffer} n "K" (vim.lsp.buf.hover) {"Show hover help", buffer}
n "gD" (vim.lsp.buf.declaration) {"Go to declaration", buffer} n "gD" (vim.lsp.buf.declaration) {"Go to declaration", buffer}
n "gd" (vim.lsp.buf.definition) {"Go to definition", buffer} n "gd" (vim.lsp.buf.definition) {"Go to definition", buffer}
n "gi" (vim.lsp.buf.implementation) {"Go to implementation", buffer} n "gi" (vim.lsp.buf.implementation) {"Go to implementation", buffer}
n "gr" (vim.lsp.buf.references) {"Show references", buffer} n "gr" (vim.lsp.buf.references) {"Show references", buffer}
n "<leader>D" (vim.lsp.buf.type_definition) {"Go to type definition", buffer} n "<leader>D" (vim.lsp.buf.type_definition) {"Go to type definition", buffer}
n "<C-k>" (vim.lsp.buf.signature_help) {"Signature help", buffer} n "<C-k>" (vim.lsp.buf.signature_help) {"Signature help", buffer}
n "<leader>wa" (vim.lsp.buf.add_workspace_folder) {"Add workspace folder", buffer} n "<leader>wa" (vim.lsp.buf.add_workspace_folder) {"Add workspace folder", buffer}
n "<leader>wr" (vim.lsp.buf.remove_workspace_folder) {"Remove workspace folder", buffer} n "<leader>wr" (vim.lsp.buf.remove_workspace_folder) {"Remove workspace folder", buffer}
n "<leader>wl" (workspaces_list) {"List workspace folders", buffer} n "<leader>wl" (workspaces_list) {"List workspace folders", buffer}
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)
})

View file

@ -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