diff options
| author | fdschmidt93 <39233597+fdschmidt93@users.noreply.github.com> | 2021-09-01 18:17:18 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-01 18:17:18 +0200 |
| commit | 5d37c3ea08f40d8c9d3a9ebcc72bd641d366c110 (patch) | |
| tree | e66df8fe1312c38ad28ff64e0f0218b415bf18af /lua/telescope/builtin/internal.lua | |
| parent | 67bc1dcdd6eea1915b7c397b7c02451bbdc277a0 (diff) | |
feat: allow caching and resuming picker (#1051)
* expose `cache_picker` in telescope.setup to configure caching, see `:h telescope.defaults.cache_picker`
* add builtin.resume and builtin.pickers picker
Diffstat (limited to 'lua/telescope/builtin/internal.lua')
| -rw-r--r-- | lua/telescope/builtin/internal.lua | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/lua/telescope/builtin/internal.lua b/lua/telescope/builtin/internal.lua index b71811e..cbacf97 100644 --- a/lua/telescope/builtin/internal.lua +++ b/lua/telescope/builtin/internal.lua @@ -6,7 +6,9 @@ local make_entry = require "telescope.make_entry" local Path = require "plenary.path" local pickers = require "telescope.pickers" local previewers = require "telescope.previewers" +local p_window = require "telescope.pickers.window" local sorters = require "telescope.sorters" +local state = require "telescope.state" local utils = require "telescope.utils" local conf = require("telescope.config").values @@ -71,6 +73,80 @@ internal.builtin = function(opts) }):find() end +internal.resume = function(opts) + opts = opts or {} + opts.cache_index = vim.F.if_nil(opts.cache_index, 1) + + local cached_pickers = state.get_global_key "cached_pickers" + if cached_pickers == nil or vim.tbl_isempty(cached_pickers) then + print "No picker(s) cached." + return + end + local picker = cached_pickers[opts.cache_index] + if picker == nil then + print("Index too large as there are only %s pickers cached", #cached_pickers) + return + 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 + picker.layout_strategy = nil + end + if picker.get_window_options == p_window.get_window_options then + picker.get_window_options = nil + end + picker.cache_picker.index = opts.cache_index + + -- avoid partial `opts.cache_picker` at picker creation + if opts.cache_picker ~= false then + picker.cache_picker = vim.tbl_extend("keep", opts.cache_picker or {}, picker.cache_picker) + else + picker.cache_picker.disabled = true + end + opts.cache_picker = nil + pickers.new(opts, picker):find() +end + +internal.pickers = function(opts) + local cached_pickers = state.get_global_key "cached_pickers" + if cached_pickers == nil or vim.tbl_isempty(cached_pickers) then + print "No picker(s) cached." + return + end + + opts = opts or {} + + -- clear cache picker for immediate pickers.new and pass option to resumed picker + if opts.cache_picker ~= nil then + opts._cache_picker = opts.cache_picker + opts.cache_picker = nil + end + + pickers.new(opts, { + prompt_title = "Pickers", + finder = finders.new_table { + results = cached_pickers, + entry_maker = make_entry.gen_from_picker(opts), + }, + previewer = previewers.pickers.new(opts), + sorter = conf.generic_sorter(opts), + cache_picker = false, + attach_mappings = function(_, map) + actions.select_default:replace(function(prompt_bufnr) + local current_picker = action_state.get_current_picker(prompt_bufnr) + local selection_index = current_picker:get_index(current_picker:get_selection_row()) + actions._close(prompt_bufnr, cached_pickers[selection_index].initial_mode == "insert") + opts.cache_picker = opts._cache_picker + opts["cache_index"] = selection_index + internal.resume(opts) + end) + map("i", "<C-x>", actions.remove_selected_picker) + map("n", "<C-x>", actions.remove_selected_picker) + return true + end, + }):find() +end + internal.planets = function(opts) local show_pluto = opts.show_pluto or false |
