refactor!: use newer APIs for setting keymaps and autocmds

BREAKING CHANGE: This drops support for Neovim versions earlier than
0.7. For earlier versions of Neovim, use the nvim-0.5 branch.
This commit is contained in:
Steven Arcangeli 2022-07-28 07:46:20 -07:00
parent 5915d56f89
commit 47b95c1eab
2 changed files with 32 additions and 54 deletions

View File

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

View File

@ -57,16 +57,16 @@ M.select = function(config, items, opts, on_choice)
vim.api.nvim_buf_set_option(bufnr, "filetype", "DressingSelect") vim.api.nvim_buf_set_option(bufnr, "filetype", "DressingSelect")
util.add_title_to_win(winnr, opts.prompt) util.add_title_to_win(winnr, opts.prompt)
local function map(lhs, rhs) vim.keymap.set("n", "<CR>", M.choose, { buffer = bufnr })
vim.api.nvim_buf_set_keymap(bufnr, "n", lhs, rhs, { silent = true, noremap = true }) vim.keymap.set("n", "<C-c>", M.cancel, { buffer = bufnr })
end vim.keymap.set("n", "<Esc>", M.cancel, { buffer = bufnr })
vim.api.nvim_create_autocmd("BufLeave", {
map("<CR>", [[<cmd>lua require('dressing.select.builtin').choose()<CR>]]) desc = "Cancel vim.ui.select",
map("<C-c>", [[<cmd>lua require('dressing.select.builtin').cancel()<CR>]]) buffer = bufnr,
map("<Esc>", [[<cmd>lua require('dressing.select.builtin').cancel()<CR>]]) nested = true,
vim.cmd([[ once = true,
autocmd BufLeave <buffer> ++nested ++once lua require('dressing.select.builtin').cancel() callback = M.cancel,
]]) })
end end
local function close_window() local function close_window()