Merge pull request #51 from stevearc/stevearc-keymaps

config options for setting keymaps
This commit is contained in:
Steven Arcangeli 2022-07-28 21:32:51 -07:00 committed by GitHub
commit d98769dfc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 217 additions and 91 deletions

View File

@ -1,31 +0,0 @@
name: Lint
on: [push, pull_request]
jobs:
luacheck:
name: Luacheck
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v1
- name: Prepare
run: |
sudo apt-get update
sudo add-apt-repository universe
sudo apt install luarocks -y
sudo luarocks install luacheck
- name: Run Luacheck
run: luacheck .
stylua:
name: StyLua
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Stylua
uses: JohnnyMorganz/stylua-action@1.0.0
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --check .

View File

@ -1,12 +1,43 @@
name: Run tests
on: [push, pull_request]
on:
push:
branches:
- master
pull_request: ~
jobs:
luacheck:
name: Luacheck
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v1
- name: Prepare
run: |
sudo apt-get update
sudo add-apt-repository universe
sudo apt install luarocks -y
sudo luarocks install luacheck
- name: Run Luacheck
run: luacheck .
stylua:
name: StyLua
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Stylua
uses: JohnnyMorganz/stylua-action@1.0.0
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --check .
run_tests:
strategy:
matrix:
nvim_tag: [v0.5.0, v0.5.1, v0.6.0, v0.6.1, v0.7.0]
nvim_tag: [v0.7.0, v0.7.2]
name: Run tests
runs-on: ubuntu-20.04

View File

@ -22,9 +22,7 @@ options in the functions. Customization will be done entirely using a separate
## Requirements
Neovim 0.5+
On versions prior to 0.6, this plugin will act as a polyfill for `vim.ui`
Neovim 0.7.0+ (for earlier versions, use the [nvim-0.5 branch](https://github.com/stevearc/dressing.nvim/tree/nvim-0.5))
## Screenshots
@ -153,6 +151,20 @@ require('dressing').setup({
-- 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
@ -237,6 +249,13 @@ require('dressing').setup({
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

View File

@ -41,6 +41,20 @@ Configure dressing.nvim by calling the setup() function.
-- 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
@ -125,6 +139,13 @@ Configure dressing.nvim by calling the setup() function.
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

View File

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

View File

@ -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) })
@ -155,9 +187,9 @@ _G.dressing_input_complete = M.completefunc
M.trigger_completion = function()
if vim.fn.pumvisible() == 1 then
return vim.api.nvim_replace_termcodes("<C-n>", true, false, true)
return "<C-n>"
else
return vim.api.nvim_replace_termcodes("<C-x><C-u>", true, false, true)
return "<C-x><C-u>"
end
end
@ -266,31 +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")
local keyopts = { silent = true, noremap = true }
local close_rhs = "<cmd>lua require('dressing.input').close()<CR>"
vim.api.nvim_buf_set_keymap(bufnr, "n", "<Esc>", close_rhs, keyopts)
if config.insert_only then
vim.api.nvim_buf_set_keymap(bufnr, "i", "<Esc>", close_rhs, keyopts)
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
local confirm_rhs = "<cmd>lua require('dressing.input').confirm()<CR>"
vim.api.nvim_buf_set_keymap(bufnr, "i", "<C-c>", close_rhs, keyopts)
vim.api.nvim_buf_set_keymap(bufnr, "i", "<CR>", confirm_rhs, keyopts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "<CR>", confirm_rhs, keyopts)
vim.api.nvim_buf_set_keymap(
bufnr,
"i",
"<Up>",
"<cmd>lua require('dressing.input').history_prev()<CR>",
keyopts
)
vim.api.nvim_buf_set_keymap(
bufnr,
"i",
"<Down>",
"<cmd>lua require('dressing.input').history_next()<CR>",
keyopts
)
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
@ -306,32 +323,25 @@ setmetatable(M, {
{ align = config.prompt_align }
)
vim.cmd([[
aug DressingHighlight
autocmd! * <buffer>
autocmd TextChanged <buffer> lua require('dressing.input').highlight()
autocmd TextChangedI <buffer> lua require('dressing.input').highlight()
aug END
]])
vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, {
desc = "Update highlights",
buffer = bufnr,
callback = M.highlight,
})
if opts.completion then
vim.api.nvim_buf_set_option(bufnr, "completefunc", "v:lua.dressing_input_complete")
vim.api.nvim_buf_set_option(bufnr, "omnifunc", "")
vim.api.nvim_buf_set_keymap(
bufnr,
"i",
"<Tab>",
[[luaeval("require('dressing.input').trigger_completion()")]],
{ expr = true }
)
vim.keymap.set("i", "<Tab>", M.trigger_completion, { buffer = bufnr, expr = true })
end
vim.cmd([[
aug DressingCloseWin
autocmd! * <buffer>
autocmd BufLeave <buffer> ++nested ++once lua require('dressing.input').close()
aug END
]])
vim.api.nvim_create_autocmd("BufLeave", {
desc = "Cancel vim.ui.input",
buffer = bufnr,
nested = true,
once = true,
callback = M.close,
})
if config.start_in_insert then
vim.cmd("startinsert!")

38
lua/dressing/map_util.lua Normal file
View File

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

View File

@ -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,16 +75,15 @@ 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)
local function map(lhs, rhs)
vim.api.nvim_buf_set_keymap(bufnr, "n", lhs, rhs, { silent = true, noremap = true })
end
map("<CR>", [[<cmd>lua require('dressing.select.builtin').choose()<CR>]])
map("<C-c>", [[<cmd>lua require('dressing.select.builtin').cancel()<CR>]])
map("<Esc>", [[<cmd>lua require('dressing.select.builtin').cancel()<CR>]])
vim.cmd([[
autocmd BufLeave <buffer> ++nested ++once lua require('dressing.select.builtin').cancel()
]])
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,
nested = true,
once = true,
callback = M.cancel,
})
end
local function close_window()