fix: fix keep insert when going from telescope window to telescope window (#1600)

Also fixes `initial_mode`

Co-authored-by: Simon Hauser <Simon-Hauser@outlook.de>
This commit is contained in:
kylo252
2022-04-22 16:13:32 +02:00
committed by GitHub
parent 5a58b1f535
commit cc1a3440f9
5 changed files with 33 additions and 40 deletions

View File

@@ -2179,14 +2179,14 @@ actions.close({prompt_bufnr}) *telescope.actions.close()*
{prompt_bufnr} (number) The prompt bufnr {prompt_bufnr} (number) The prompt bufnr
actions._close({prompt_bufnr}, {keepinsert}) *telescope.actions._close()* actions._close({prompt_bufnr}) *telescope.actions._close()*
Close the Telescope window and specify if you want to keep insert mode or Close the Telescope window, usually used within an action
not Deprecated and no longer needed, does the same as
|telescope.actions.close|. Might be removed in the future
Parameters: ~ Parameters: ~
{prompt_bufnr} (number) The prompt bufnr {prompt_bufnr} (number) The prompt bufnr
{keepinsert} (boolean) Remain in INSERT mode if true
actions.edit_command_line({prompt_bufnr}) *telescope.actions.edit_command_line()* actions.edit_command_line({prompt_bufnr}) *telescope.actions.edit_command_line()*

View File

@@ -343,28 +343,26 @@ end
--- Close the Telescope window, usually used within an action --- Close the Telescope window, usually used within an action
---@param prompt_bufnr number: The prompt bufnr ---@param prompt_bufnr number: The prompt bufnr
actions.close = function(prompt_bufnr) actions.close = function(prompt_bufnr)
actions._close(prompt_bufnr, false)
end
--- Close the Telescope window and specify if you want to keep insert mode or not
---@param prompt_bufnr number: The prompt bufnr
---@param keepinsert boolean: Remain in INSERT mode if true
actions._close = function(prompt_bufnr, keepinsert)
action_state.get_current_history():reset() action_state.get_current_history():reset()
local picker = action_state.get_current_picker(prompt_bufnr) local picker = action_state.get_current_picker(prompt_bufnr)
local prompt_win = state.get_status(prompt_bufnr).prompt_win local prompt_win = state.get_status(prompt_bufnr).prompt_win
local original_win_id = picker.original_win_id local original_win_id = picker.original_win_id
actions.close_pum(prompt_bufnr) actions.close_pum(prompt_bufnr)
if not keepinsert then
vim.cmd [[stopinsert]]
end
vim.api.nvim_win_close(prompt_win, true) vim.api.nvim_win_close(prompt_win, true)
pcall(vim.cmd, string.format([[silent bdelete! %s]], prompt_bufnr)) pcall(vim.cmd, string.format([[silent bdelete! %s]], prompt_bufnr))
pcall(a.nvim_set_current_win, original_win_id) pcall(a.nvim_set_current_win, original_win_id)
end end
--- Close the Telescope window, usually used within an action<br>
--- Deprecated and no longer needed, does the same as |telescope.actions.close|. Might be removed in the future
---@deprecated
---@param prompt_bufnr number: The prompt bufnr
actions._close = function(prompt_bufnr)
actions.close(prompt_bufnr)
end
local set_edit_line = function(prompt_bufnr, fname, prefix, postfix) local set_edit_line = function(prompt_bufnr, fname, prefix, postfix)
postfix = vim.F.if_nil(postfix, "") postfix = vim.F.if_nil(postfix, "")
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
@@ -460,7 +458,7 @@ end
---@param prompt_bufnr number: The prompt bufnr ---@param prompt_bufnr number: The prompt bufnr
actions.insert_symbol_i = function(prompt_bufnr) actions.insert_symbol_i = function(prompt_bufnr)
local symbol = action_state.get_selected_entry().value[1] local symbol = action_state.get_selected_entry().value[1]
actions._close(prompt_bufnr, true) actions.close(prompt_bufnr)
vim.schedule(function() vim.schedule(function()
vim.cmd [[startinsert]] vim.cmd [[startinsert]]
vim.api.nvim_put({ symbol }, "", true, true) vim.api.nvim_put({ symbol }, "", true, true)

View File

@@ -178,9 +178,10 @@ internal.pickers = function(opts)
actions.select_default:replace(function(prompt_bufnr) actions.select_default:replace(function(prompt_bufnr)
local current_picker = action_state.get_current_picker(prompt_bufnr) local current_picker = action_state.get_current_picker(prompt_bufnr)
local selection_index = current_picker:get_index(current_picker:get_selection_row()) local selection_index = current_picker:get_index(current_picker:get_selection_row())
actions._close(prompt_bufnr, cached_pickers[selection_index].initial_mode == "insert") actions.close(prompt_bufnr)
opts.cache_picker = opts._cache_picker opts.cache_picker = opts._cache_picker
opts["cache_index"] = selection_index opts["cache_index"] = selection_index
opts["initial_mode"] = cached_pickers[selection_index].initial_mode
internal.resume(opts) internal.resume(opts)
end) end)
map("i", "<C-x>", actions.remove_selected_picker) map("i", "<C-x>", actions.remove_selected_picker)

View File

@@ -432,21 +432,19 @@ function Picker:find()
pcall(a.nvim_buf_set_option, prompt_bufnr, "filetype", "TelescopePrompt") pcall(a.nvim_buf_set_option, prompt_bufnr, "filetype", "TelescopePrompt")
pcall(a.nvim_buf_set_option, results_bufnr, "filetype", "TelescopeResults") pcall(a.nvim_buf_set_option, results_bufnr, "filetype", "TelescopeResults")
-- TODO(async): I wonder if this should actually happen _before_ we nvim_buf_attach. if self.default_text then
-- This way the buffer would always start with what we think it should when we start the loop. self:set_prompt(self.default_text)
if self.initial_mode == "insert" or self.initial_mode == "normal" then end
-- required for set_prompt to work adequately
vim.cmd [[startinsert!]] if self.initial_mode == "insert" then
if self.default_text then vim.schedule(function()
self:set_prompt(self.default_text) -- startinsert! did not reliable do `A` no idea why, i even looked at the source code
end -- Example: live_grep -> type something -> quit -> Telescope pickers -> resume -> cursor of by one
if self.initial_mode == "normal" then if vim.fn.mode() ~= "i" then
-- otherwise (i) insert mode exitted faster than `picker:set_prompt`; (ii) cursor on wrong pos vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<ESC>A", true, false, true), "n", true)
await_schedule(function() end
vim.cmd [[stopinsert]] end)
end) elseif self.initial_mode ~= "normal" then
end
else
error("Invalid setting for initial_mode: " .. self.initial_mode) error("Invalid setting for initial_mode: " .. self.initial_mode)
end end
@@ -722,10 +720,8 @@ function Picker:delete_selection(delete_cb)
end, 50) end, 50)
end end
function Picker:set_prompt(str) function Picker:set_prompt(text)
-- TODO(conni2461): As soon as prompt_buffers are fix use this: self:reset_prompt(text)
-- vim.api.nvim_buf_set_lines(self.prompt_bufnr, 0, 1, false, { str })
vim.api.nvim_feedkeys(str, "n", false)
end end
--- Closes the windows for the prompt, results and preview --- Closes the windows for the prompt, results and preview
@@ -890,7 +886,6 @@ end
function Picker:reset_prompt(text) function Picker:reset_prompt(text)
local prompt_text = self.prompt_prefix .. (text or "") local prompt_text = self.prompt_prefix .. (text or "")
vim.api.nvim_buf_set_lines(self.prompt_bufnr, 0, -1, false, { prompt_text }) vim.api.nvim_buf_set_lines(self.prompt_bufnr, 0, -1, false, { prompt_text })
self:_reset_prefix_color(self._current_prefix_hl_group) self:_reset_prefix_color(self._current_prefix_hl_group)
if text then if text then
@@ -1223,7 +1218,7 @@ end
--- Close all open Telescope pickers --- Close all open Telescope pickers
function Picker:close_existing_pickers() function Picker:close_existing_pickers()
for _, prompt_bufnr in ipairs(state.get_existing_prompts()) do for _, prompt_bufnr in ipairs(state.get_existing_prompts()) do
pcall(actions._close, prompt_bufnr, true) pcall(actions.close, prompt_bufnr)
end end
end end
@@ -1529,7 +1524,6 @@ function Picker:_resume_picker()
self.cache_picker.is_cached = false self.cache_picker.is_cached = false
-- if text changed, required to set anew to restart finder; otherwise hl and selection -- if text changed, required to set anew to restart finder; otherwise hl and selection
if self.cache_picker.cached_prompt ~= self.default_text then if self.cache_picker.cached_prompt ~= self.default_text then
self:reset_prompt()
self:set_prompt(self.default_text) self:set_prompt(self.default_text)
else else
-- scheduling required to apply highlighting and selection appropriately -- scheduling required to apply highlighting and selection appropriately

View File

@@ -1,5 +1,5 @@
if !has('nvim-0.7.0') if !has('nvim-0.7.0')
echoerr "Telescope.nvim requires at least nvim-0.7.0. See `:h telescope.changelog-1549`" echoerr "Telescope.nvim requires at least nvim-0.7.0. See `:h telescope.changelog-1851`"
finish finish
end end