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'}
|
||||
|
||||
|
||||
|
@ -28,35 +29,31 @@ n "]d" (vim.diagnostic.goto_next) "Next diagnostic message"
|
|||
n "<leader>dl" (vim.diagnostic.setloclist) "Diagnostic: list messages"
|
||||
|
||||
-- LSP
|
||||
vim.api.nvim_create_autocmd('LspAttach', {
|
||||
group = vim.api.nvim_create_augroup('UserLspConfig', {}),
|
||||
desc = "LSP actions",
|
||||
callback = function(ev)
|
||||
-- Enable completion triggered by <c-x><c-o>
|
||||
vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'
|
||||
autocmd "LspAttach" {"LSP actions", group=augroup "UserLspConfig"} (function(ev)
|
||||
-- Enable completion triggered by <c-x><c-o>
|
||||
vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'
|
||||
|
||||
local function workspaces_list()
|
||||
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
|
||||
end
|
||||
local function workspaces_list()
|
||||
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
|
||||
end
|
||||
|
||||
local function format()
|
||||
vim.lsp.buf.format({async = true})
|
||||
end
|
||||
local function format()
|
||||
vim.lsp.buf.format({async = true})
|
||||
end
|
||||
|
||||
local buffer = ev.buf
|
||||
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.definition) {"Go to definition", buffer}
|
||||
n "gi" (vim.lsp.buf.implementation) {"Go to implementation", buffer}
|
||||
n "gr" (vim.lsp.buf.references) {"Show references", 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 "<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>wl" (workspaces_list) {"List workspace folders", buffer}
|
||||
n "<leader>lr" (vim.lsp.buf.rename) {"Rename identifier", buffer}
|
||||
nv "<leader>la" (vim.lsp.buf.code_action) {"Show code actions", buffer}
|
||||
n "<leader>lf" (format) {"Format file", buffer}
|
||||
end,
|
||||
})
|
||||
local buffer = ev.buf
|
||||
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.definition) {"Go to definition", buffer}
|
||||
n "gi" (vim.lsp.buf.implementation) {"Go to implementation", buffer}
|
||||
n "gr" (vim.lsp.buf.references) {"Show references", 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 "<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>wl" (workspaces_list) {"List workspace folders", buffer}
|
||||
n "<leader>lr" (vim.lsp.buf.rename) {"Rename identifier", buffer}
|
||||
nv "<leader>la" (vim.lsp.buf.code_action) {"Show code actions", buffer}
|
||||
n "<leader>lf" (format) {"Format file", buffer}
|
||||
end)
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ local M = {}
|
|||
|
||||
local def_opts = {noremap = true, silent = true}
|
||||
|
||||
|
||||
---Loads plugin config
|
||||
---@param path string
|
||||
function M.plugin(path)
|
||||
|
@ -13,11 +14,12 @@ function M.plugin(path)
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
---@class KeymapOpts
|
||||
---@field [1] string? Shorthand description
|
||||
---@field desc string? Description
|
||||
---@field [2] number|boolean? Shorthand buffer
|
||||
---@field buffer number|boolean? Buffer
|
||||
---@field [1]? string Shorthand description
|
||||
---@field desc? string Description
|
||||
---@field [2]? number|boolean Shorthand buffer
|
||||
---@field buffer? number|boolean Buffer
|
||||
|
||||
---Wrapper around `vim.keymap.set`
|
||||
---@param modes string|string[]
|
||||
|
@ -28,11 +30,11 @@ function M.keymap(modes)
|
|||
return function(rhs)
|
||||
---@param opts string|KeymapOpts
|
||||
return function(opts)
|
||||
local supplied = type(opts)
|
||||
local type = type(opts)
|
||||
local options
|
||||
if supplied == "string" then
|
||||
if type == "string" then
|
||||
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.buffer = opts[2] or opts.buffer
|
||||
opts[1], opts[2] = nil, nil
|
||||
|
@ -44,4 +46,46 @@ function M.keymap(modes)
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue