From bcdfac0d0db6e9cf81cdc7444b3ce42d9af893a0 Mon Sep 17 00:00:00 2001 From: delphinus Date: Fri, 3 Jan 2025 23:41:30 +0900 Subject: [PATCH] feat(pickers): add an option to resume and select the entry --- lua/telescope/builtin/__internal.lua | 12 ++++++++++++ lua/telescope/builtin/init.lua | 1 + lua/telescope/pickers.lua | 6 ++++++ 3 files changed, 19 insertions(+) diff --git a/lua/telescope/builtin/__internal.lua b/lua/telescope/builtin/__internal.lua index 1687401..c6b1251 100644 --- a/lua/telescope/builtin/__internal.lua +++ b/lua/telescope/builtin/__internal.lua @@ -148,6 +148,18 @@ internal.resume = function(opts) }) return end + if opts.select_pos then + if picker.manager:num_results() <= math.abs(opts.select_pos) then + utils.notify("builtin.resume", { + msg = string.format("The picker has less entries than supplied pos: %d", opts.select_pos), + level = "ERROR", + }) + return + end + opts._select_pos = opts.select_pos + opts.select_pos = nil + end + -- reset layout strategy and get_window_options if default as only one is valid -- and otherwise unclear which was actually set if picker.layout_strategy == conf.layout_strategy then diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index 6e27c27..02ce994 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -249,6 +249,7 @@ builtin.builtin = require_on_exported_call("telescope.builtin.__internal").built --- - Requires `cache_picker` in setup or when having invoked pickers, see |telescope.defaults.cache_picker| ---@param opts table: options to pass to the picker ---@field cache_index number: what picker to resume, where 1 denotes most recent (default: 1) +---@field select_pos number: select the entry that position is specified by this value and do `actions.select_default` (default: nil) builtin.resume = require_on_exported_call("telescope.builtin.__internal").resume --- Opens a picker over previously cached pickers in their preserved states (incl. multi selections) diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua index b353c8a..8d77509 100644 --- a/lua/telescope/pickers.lua +++ b/lua/telescope/pickers.lua @@ -324,6 +324,8 @@ function Picker:new(opts) cache_picker = config.resolve_table_opts(opts.cache_picker, vim.deepcopy(config.values.cache_picker)), + _select_pos = opts._select_pos, + __scrolling_limit = tonumber(vim.F.if_nil(opts.temp__scrolling_limit, 250)), __locations_input = vim.F.if_nil(opts.__locations_input, false), @@ -1708,6 +1710,10 @@ function Picker:_resume_picker() vim.cmd "do User TelescopeResumePost" end) end + if self._select_pos then + self:move_selection(self._select_pos) + vim.schedule_wrap(actions.select_default)(self.prompt_bufnr) + end end -- if text changed, required to set anew to restart finder; otherwise hl and selection if self.cache_picker.cached_prompt ~= self.default_text then