From c93276acd34f5cb20d7cc2de15a8f40526433660 Mon Sep 17 00:00:00 2001 From: fdschmidt93 <39233597+fdschmidt93@users.noreply.github.com> Date: Wed, 4 May 2022 15:00:28 +0200 Subject: fix: initial mode setting issues (#1917) --- lua/telescope/pickers.lua | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) (limited to 'lua') diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua index 77f88f3..1d20df1 100644 --- a/lua/telescope/pickers.lua +++ b/lua/telescope/pickers.lua @@ -423,6 +423,28 @@ function Picker:find() local find_id = self:_next_find_id() + if self.default_text then + self:set_prompt(self.default_text) + end + + if vim.tbl_contains({ "insert", "normal" }, self.initial_mode) then + local mode = vim.fn.mode() + local keys + if self.initial_mode == "normal" then + -- n: A makes sure cursor is at always at end of prompt w/o default_text + keys = mode ~= "n" and "A" or "A" + else + -- always fully retrigger insert mode: required for going from one picker to next + keys = mode ~= "n" and "A" or "A" + end + a.nvim_feedkeys(a.nvim_replace_termcodes(keys, true, false, true), "n", true) + else + utils.notify( + "pickers.find", + { msg = "`initial_mode` should be one of ['normal', 'insert'] but passed " .. self.initial_mode, level = "ERROR" } + ) + end + local main_loop = async.void(function() self.sorter:_init() @@ -430,23 +452,6 @@ function Picker:find() pcall(a.nvim_buf_set_option, prompt_bufnr, "filetype", "TelescopePrompt") pcall(a.nvim_buf_set_option, results_bufnr, "filetype", "TelescopeResults") - if self.default_text then - self:set_prompt(self.default_text) - end - - if self.initial_mode == "insert" then - vim.schedule(function() - -- startinsert! did not reliable do `A` no idea why, i even looked at the source code - -- Example: live_grep -> type something -> quit -> Telescope pickers -> resume -> cursor of by one - local mode = vim.fn.mode() - if mode ~= "i" then - a.nvim_input(mode ~= "n" and "A" or "A") - end - end) - elseif self.initial_mode ~= "normal" then - error("Invalid setting for initial_mode: " .. self.initial_mode) - end - await_schedule() while true do -- cgit v1.2.3