feat: expose mappings to user via config
This commit is contained in:
parent
4466fd9836
commit
b1c08146ee
|
@ -34,6 +34,20 @@ local default_config = {
|
|||
-- Change default highlight groups (see :help winhl)
|
||||
winhighlight = "",
|
||||
|
||||
-- Set to `false` to disable
|
||||
mappings = {
|
||||
n = {
|
||||
["<Esc>"] = "Close",
|
||||
["<CR>"] = "Confirm",
|
||||
},
|
||||
i = {
|
||||
["<C-c>"] = "Close",
|
||||
["<CR>"] = "Confirm",
|
||||
["<Up>"] = "HistoryPrev",
|
||||
["<Down>"] = "HistoryNext",
|
||||
},
|
||||
},
|
||||
|
||||
override = function(conf)
|
||||
-- This is the config that will be passed to nvim_open_win.
|
||||
-- Change values here to customize the layout
|
||||
|
@ -118,6 +132,13 @@ local default_config = {
|
|||
max_height = 0.9,
|
||||
min_height = { 10, 0.2 },
|
||||
|
||||
-- Set to `false` to disable
|
||||
mappings = {
|
||||
["<Esc>"] = "Close",
|
||||
["<C-c>"] = "Close",
|
||||
["<CR>"] = "Confirm",
|
||||
},
|
||||
|
||||
override = function(conf)
|
||||
-- This is the config that will be passed to nvim_open_win.
|
||||
-- Change values here to customize the layout
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
local map_util = require("dressing.map_util")
|
||||
local global_config = require("dressing.config")
|
||||
local patch = require("dressing.patch")
|
||||
local util = require("dressing.util")
|
||||
|
@ -12,6 +13,37 @@ local context = {
|
|||
start_in_insert = nil,
|
||||
}
|
||||
|
||||
local keymaps = {
|
||||
{
|
||||
desc = "Close vim.ui.input without a result",
|
||||
plug = "<Plug>DressingInput:Close",
|
||||
rhs = function()
|
||||
M.close()
|
||||
end,
|
||||
},
|
||||
{
|
||||
desc = "Close vim.ui.input with the current buffer contents",
|
||||
plug = "<Plug>DressingInput:Confirm",
|
||||
rhs = function()
|
||||
M.confirm()
|
||||
end,
|
||||
},
|
||||
{
|
||||
desc = "Show previous vim.ui.input history entry",
|
||||
plug = "<Plug>DressingInput:HistoryPrev",
|
||||
rhs = function()
|
||||
M.history_prev()
|
||||
end,
|
||||
},
|
||||
{
|
||||
desc = "Show next vim.ui.input history entry",
|
||||
plug = "<Plug>DressingInput:HistoryNext",
|
||||
rhs = function()
|
||||
M.history_next()
|
||||
end,
|
||||
},
|
||||
}
|
||||
|
||||
local function set_input(text)
|
||||
vim.api.nvim_buf_set_lines(0, 0, -1, true, { text })
|
||||
vim.api.nvim_win_set_cursor(0, { 1, vim.api.nvim_strwidth(text) })
|
||||
|
@ -266,16 +298,16 @@ setmetatable(M, {
|
|||
-- Finish setting up the buffer
|
||||
vim.api.nvim_buf_set_option(bufnr, "swapfile", false)
|
||||
vim.api.nvim_buf_set_option(bufnr, "bufhidden", "wipe")
|
||||
vim.keymap.set("n", "<Esc>", M.close, { buffer = bufnr })
|
||||
|
||||
map_util.create_plug_maps(bufnr, keymaps)
|
||||
for mode, user_maps in pairs(config.mappings) do
|
||||
map_util.create_maps_to_plug(bufnr, mode, user_maps, "DressingInput:")
|
||||
end
|
||||
|
||||
if config.insert_only then
|
||||
vim.keymap.set("i", "<Esc>", M.close, { buffer = bufnr })
|
||||
end
|
||||
|
||||
vim.keymap.set("i", "<C-c>", M.close, { buffer = bufnr })
|
||||
vim.keymap.set("i", "<CR>", M.confirm, { buffer = bufnr })
|
||||
vim.keymap.set("n", "<CR>", M.confirm, { buffer = bufnr })
|
||||
vim.keymap.set("i", "<Up>", M.history_prev, { buffer = bufnr })
|
||||
vim.keymap.set("i", "<Down>", M.history_next, { buffer = bufnr })
|
||||
vim.api.nvim_buf_set_option(bufnr, "filetype", "DressingInput")
|
||||
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, { opts.default or "" })
|
||||
-- Disable nvim-cmp if installed
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
local M = {}
|
||||
|
||||
M.create_plug_maps = function(bufnr, plug_bindings)
|
||||
for _, binding in ipairs(plug_bindings) do
|
||||
vim.keymap.set("", binding.plug, binding.rhs, { buffer = bufnr, desc = binding.desc })
|
||||
end
|
||||
end
|
||||
|
||||
---@param bufnr number
|
||||
---@param mode string
|
||||
---@param bindings table<string, string>
|
||||
---@param prefix string
|
||||
M.create_maps_to_plug = function(bufnr, mode, bindings, prefix)
|
||||
local maps
|
||||
if mode == "i" then
|
||||
maps = vim.api.nvim_buf_get_keymap(bufnr, "")
|
||||
end
|
||||
for lhs, rhs in pairs(bindings) do
|
||||
if rhs then
|
||||
-- Prefix with <Plug> unless this is a <Cmd> or :Cmd mapping
|
||||
if type(rhs) == "string" and not rhs:match("[<:]") then
|
||||
rhs = "<Plug>" .. prefix .. rhs
|
||||
end
|
||||
if mode == "i" then
|
||||
-- HACK for some reason I can't get plug mappings to work in insert mode
|
||||
for _, map in ipairs(maps) do
|
||||
if map.lhs == rhs then
|
||||
rhs = map.callback or map.rhs
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
vim.keymap.set(mode, lhs, rhs, { buffer = bufnr, remap = true })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
|
@ -1,6 +1,24 @@
|
|||
local map_util = require("dressing.map_util")
|
||||
local util = require("dressing.util")
|
||||
local M = {}
|
||||
|
||||
local keymaps = {
|
||||
{
|
||||
desc = "Close vim.ui.select without a result",
|
||||
plug = "<Plug>DressingSelect:Close",
|
||||
rhs = function()
|
||||
M.cancel()
|
||||
end,
|
||||
},
|
||||
{
|
||||
desc = "Select the current vim.ui.select item under the cursor",
|
||||
plug = "<Plug>DressingSelect:Confirm",
|
||||
rhs = function()
|
||||
M.choose()
|
||||
end,
|
||||
},
|
||||
}
|
||||
|
||||
M.is_supported = function()
|
||||
return true
|
||||
end
|
||||
|
@ -57,9 +75,8 @@ M.select = function(config, items, opts, on_choice)
|
|||
vim.api.nvim_buf_set_option(bufnr, "filetype", "DressingSelect")
|
||||
util.add_title_to_win(winnr, opts.prompt)
|
||||
|
||||
vim.keymap.set("n", "<CR>", M.choose, { buffer = bufnr })
|
||||
vim.keymap.set("n", "<C-c>", M.cancel, { buffer = bufnr })
|
||||
vim.keymap.set("n", "<Esc>", M.cancel, { buffer = bufnr })
|
||||
map_util.create_plug_maps(bufnr, keymaps)
|
||||
map_util.create_maps_to_plug(bufnr, "n", config.mappings, "DressingSelect:")
|
||||
vim.api.nvim_create_autocmd("BufLeave", {
|
||||
desc = "Cancel vim.ui.select",
|
||||
buffer = bufnr,
|
||||
|
|
Loading…
Reference in New Issue