telescope.nvim icon indicating copy to clipboard operation
telescope.nvim copied to clipboard

Feat: reload picker with different options

Open joeveiga opened this issue 3 years ago • 9 comments

Is your feature request related to a problem? Please describe.

There are certain options I'd like to change "on the fly" via mappings while Telescope is running without the need to close it and re-launch it with the desired option (and lose the current state in the process, i.e. search query, selected entries, etc.). At the moment I'm forced to have way more mappings than I'd like to open finders in different modes.

A practical example would be toggling hidden for find_files.

Describe the solution you'd like

A way to set options for the picker and refresh it.

Describe alternatives you've considered

I put this hack together quickly.

require("telescope").setup({
  pickers = {
    find_files = {
      mappings = {
        n = {
          ["h"] = function(prompt_bufnr)
	    local current_picker = require("telescope.actions.state").get_current_picker(prompt_bufnr)
	    local opts = {
	      hidden = true,
	      default_text = current_picker:_get_prompt(),
	      -- TODO: copy other relevant state :/
            }

	    require("telescope.actions").close(prompt_bufnr)
	    require("telescope.builtin").find_files(opts)
	  end,
	},
      },
    },
  },
})

I think resume() accepts options as well, so I could try something like:

require("telescope.actions").close(prompt_bufnr)
require("telescope.builtin").resume({ hidden = true })

Should this version work it would be a bit better but still hacky. Open/close event would be re-triggered, for example.

Additional context

Kapture 2022-06-19 at 11 39 27

joeveiga avatar Jun 19 '22 15:06 joeveiga

local finders = require "telescope.finders"
local make_entry = require "telescope.make_entry" 
local action_state = require "telescope.actions.state"

local opts = {}
opts.entry_maker = make_entry.gen_from_file(opts)

local cmd = { "fd", "--type", "f", "--hidden" }
local current_picker = action_state.get_current_picker(prompt_bufnr)
current_picker:refresh(finders.new_oneshot_job(cmd, opts), {})

refresh takes as first argument a new finder, that is being run and as second arg a table with additional configuration:

---@field new_prefix string|table: either as string or { new_string, hl_group }
---@field reset_prompt bool: whether to reset the prompt
---@field multi MultiSelect: multi-selection to persist upon renewing finder (see telescope/pickers/multi.lua)

This is currently as good as it gets. (Code not tested, written in github)

Conni2461 avatar Jun 26 '22 09:06 Conni2461

Does that work for you? If yes i would like to close this issue.

Conni2461 avatar Jun 30 '22 12:06 Conni2461

Thanks @Conni2461 I'll give it a try!

joeveiga avatar Jul 01 '22 22:07 joeveiga

would this work to reload extension? I have similar need to be able to search for files fuzzy / non-fuzzy way (by using fzf-native). And to do that I need to load the extension with different boolean flag

OlegGulevskyy avatar Sep 30 '22 21:09 OlegGulevskyy

@OlegGulevskyy you dont need to reload the extension for that. You can just do Telescope find_files fuzzy=false to disable fuzzy searching with fzf-native. (only works with fzf-native!)

Could be documented in that repo, if you wanna make a PR ;)

Conni2461 avatar Oct 01 '22 07:10 Conni2461

Ah! Very neat, thank you! Saved me many attempts of what appears to be very simple :) Sure, I'll create a PR! Not exactly sure where that'll go as there is no "usage" section but I will try and you can let me know.

Thanks again!

OlegGulevskyy avatar Oct 03 '22 06:10 OlegGulevskyy

Hi there, any chance that refresh could take the command of the existing finder? I was looking at the code but the cmd is not stored in the table https://github.com/nvim-telescope/telescope.nvim/blob/3e944f02ff8040056b44f6a9aed48842317b33ac/lua/telescope/finders/async_oneshot_finder.lua#L15 so if I want to use refresh with a one_shot_finder I'll need to know what command was used with the current picker. My goal is to add a mapping <M-R> to reload the picker using cwd = vim.fn.expand "%:p:h" for many of the pickers I use (find_filed, live_grep, file browser)

protiumx avatar Feb 02 '23 19:02 protiumx