Skip to content

Commit

Permalink
feat: expose mappings to user via config
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Jul 29, 2022
1 parent 4466fd9 commit b1c0814
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 9 deletions.
21 changes: 21 additions & 0 deletions lua/dressing/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
44 changes: 38 additions & 6 deletions lua/dressing/input.lua
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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) })
Expand Down Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions lua/dressing/map_util.lua
Original file line number Diff line number Diff line change
@@ -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
23 changes: 20 additions & 3 deletions lua/dressing/select/builtin.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b1c0814

Please sign in to comment.