forked from sudo-tee/opencode.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeymap.lua
More file actions
45 lines (38 loc) · 1.37 KB
/
keymap.lua
File metadata and controls
45 lines (38 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
local api = require('opencode.api')
local M = {}
-- Binds a keymap config with its api fn
-- Name of api fn & keymap global config should always be the same
---@param keymap OpencodeKeymap The keymap configuration table
function M.setup(keymap)
local cmds = api.commands
local global = keymap.global
for key, mapping in pairs(global) do
if type(mapping) == 'string' then
vim.keymap.set({ 'n', 'v' }, mapping, function()
api[key]()
end, { silent = false, desc = cmds[key] and cmds[key].desc })
end
end
end
---@param lhs string|false The left-hand side of the mapping, `false` disables keymaps
---@param rhs function|string The right-hand side of the mapping
---@param bufnrs number|number[] Buffer number(s) to set the mapping for
---@param mode string|string[] Agent(s) for the mapping
---@param opts? table Additional options for vim.keymap.set
function M.buf_keymap(lhs, rhs, bufnrs, mode, opts)
if not lhs then
return
end
opts = opts or { silent = true }
bufnrs = type(bufnrs) == 'table' and bufnrs or { bufnrs }
for _, bufnr in ipairs(bufnrs) do
if
not vim.api.nvim_buf_is_valid(bufnr --[[@as number]])
then
vim.notify(string.format('Invalid buffer number: %s', bufnr), vim.log.levels.WARN)
return
end
vim.keymap.set(mode, lhs, rhs, vim.tbl_extend('force', opts, { buffer = bufnr }))
end
end
return M