stevearc / dressing.nvim

Neovim plugin to improve the default vim.ui interfaces
MIT License
1.73k stars 32 forks source link

Select opens in normal mode when using a range code action #17

Closed bellini666 closed 2 years ago

bellini666 commented 2 years ago

When opening a select from normal/insert mode, the telescope UI gets used correctly. When doing a visual selection and calling vim.lsp.buf.range_code_action() the telescope UI gets opened in normal mode, making it necessary to press <Esc> before trying to select any option

stevearc commented 2 years ago

This is an inconsistency that comes from telescope itself. I can reproduce the issue with the following code:

vim.defer_fn(function()
  require("telescope.builtin").find_files()
end, 2000)
vim.cmd("normal V")

It would be possible to add a hack to dressing to work around this, but it would be more correct to address it within telescope. I'd recommend filing an issue there with this minimal repro (after confirming that it also repros for you).

mmirus commented 1 year ago

@bellini666 Hey! Did you ever find a solution to this?

mmirus commented 1 year ago

I finally found a couple workarounds for this, which are essentially two methods of sending <Esc> after opening the window so that it exits -- (insert) VISUAL -- and goes to insert mode.

If anyone has a better solution, please share!

I settled on the second one as the simpler of the two, but sharing both in case the other helps anyone.

Here's the first one:

vim.api.nvim_create_autocmd("FileType", {
  pattern = "TelescopePrompt",
  callback = function()
    if vim.api.nvim_get_mode().mode == "v" then
      local escape = vim.api.nvim_replace_termcodes("<Esc>", true, false, true)
      vim.api.nvim_feedkeys(escape, "v", false)
    end
  end,
  group = vim.api.nvim_create_augroup("TelescopeCodeActions", { clear = true }),
})

And here's the second:

-- The bug occurs with this mapping:
-- vim.keymap.set("v", "<space>c", vim.lsp.buf.code_action, { desc = "Code action" })

-- This works around the bug:
vim.keymap.set("v", "<space>c", "<cmd>lua vim.lsp.buf.code_action()<cr><esc>", { desc = "Code action" })