allow confirm and concel keybind for inplace rename

pull/249/head
ray-x 2 years ago
parent d1db210a88
commit f8c743396d

@ -54,6 +54,8 @@ _NgConfigValues = {
rename = { rename = {
style = 'floating-preview', -- 'floating' | 'floating-preview' | 'inplace-preview' style = 'floating-preview', -- 'floating' | 'floating-preview' | 'inplace-preview'
show_result = true, show_result = true,
confirm = '<S-CR>',
cancel = '<S-ESC>',
}, },
document_highlight = true, -- highlight reference a symbol document_highlight = true, -- highlight reference a symbol
code_lens_action = { code_lens_action = {

@ -314,7 +314,7 @@ M.rename = function()
-- make sure everything was restored -- make sure everything was restored
ghinput.setup({ ghinput.setup({
on_change = function(new_name) end, on_change = function(new_name) end,
on_confirm = function(new_name) end, on_concel = function(new_name) end,
on_cancel = function() end, on_cancel = function() end,
}) })
vim.ui.input = ghinput.input vim.ui.input = ghinput.input
@ -405,6 +405,8 @@ function M.rename_inplace(new_name, options)
vim.notify('[LSP] Rename, no matching language servers with rename capability.') vim.notify('[LSP] Rename, no matching language servers with rename capability.')
end end
local confirm_key = _NgConfigValues.lsp.rename.confirm
local cancel_key = _NgConfigValues.lsp.rename.concel
local win = api.nvim_get_current_win() local win = api.nvim_get_current_win()
-- Compute early to account for cursor movements after going async -- Compute early to account for cursor movements after going async
@ -423,6 +425,20 @@ function M.rename_inplace(new_name, options)
)[1] )[1]
end end
local on_finish_cb = function()
log('leave insert')
api.nvim_buf_clear_namespace(bufnr, ns, 0, -1)
api.nvim_del_augroup_by_name('nav-rename')
vim.keymap.del({ 'i', 'n' }, confirm_key, { buffer = bufnr })
vim.keymap.del({ 'i', 'n' }, cancel_key, { buffer = bufnr })
restore_buffer()
if state.confirm then
-- lets put back
log('execute rename')
inc_rename_execute({ args = state.new_name or vim.fn.expand('<cword>'), params = params })
end
end
local try_use_client local try_use_client
try_use_client = function(idx, client) try_use_client = function(idx, client)
if not client then if not client then
@ -458,7 +474,7 @@ function M.rename_inplace(new_name, options)
return rename(new_name) return rename(new_name)
end end
vim.api.nvim_create_autocmd({ 'TextChangedI' }, { vim.api.nvim_create_autocmd({ 'TextChangedI', 'TextChanged' }, {
group = rename_group, group = rename_group,
callback = function() callback = function()
local w = vim.fn.expand('<cword>') local w = vim.fn.expand('<cword>')
@ -491,29 +507,30 @@ function M.rename_inplace(new_name, options)
incremental_rename_preview({ args = w, floating = false }, ns, bufnr) incremental_rename_preview({ args = w, floating = false }, ns, bufnr)
end, end,
}) })
vim.keymap.set('i', '<S-CR>', function() vim.keymap.set({ 'i', 'n' }, confirm_key, function()
print('done rename') print('done rename')
local input = vim.fn.expand('<cword>') local input = vim.fn.expand('<cword>')
log('newname', input) log('newname', input)
state.confirm = true state.confirm = true
vim.cmd('stopinsert') vim.cmd('stopinsert')
on_finish_cb()
end, { buffer = bufnr }) end, { buffer = bufnr })
vim.api.nvim_create_autocmd({ 'InsertLeave' }, { if cancel_key == nil or cancel_key == '' then
group = rename_group, vim.api.nvim_create_autocmd({ 'InsertLeave' }, {
callback = function() group = rename_group,
log('leave insert') callback = function()
api.nvim_buf_clear_namespace(bufnr, ns, 0, -1) state.confirm = nil
api.nvim_del_augroup_by_name('nav-rename') on_finish_cb()
vim.keymap.del('i', '<S-CR>', { buffer = bufnr }) end,
restore_buffer() })
if state.confirm then else
-- lets put back vim.keymap.set({ 'i', 'n' }, cancel_key, function()
log('execute rename') print('cancel rename')
inc_rename_execute({ args = state.new_name or vim.fn.expand('<cword>'), params = params }) state.confirm = nil
end on_finish_cb()
end, end, { buffer = bufnr })
}) end
vim.cmd('noautocmd startinsert') vim.cmd('noautocmd startinsert')
end, bufnr) end, bufnr)

Loading…
Cancel
Save