nvim-telescope / telescope-file-browser.nvim

File Browser extension for telescope.nvim
MIT License
1.6k stars 89 forks source link

Toggle between directory-selection mode and live_grep/file_find mode in `path` #382

Closed swoh816 closed 2 months ago

swoh816 commented 2 months ago

Is your feature request related to a problem? Please describe. I would like to use live_grep/find_files in path (the folder the file_browser is currently in) in telescope-file-browser.

Describe the solution you'd like What I think would be nice is to have a functionality to toggle between directory-selection mode and live_grep/file_find mode in path. I have a workflow described in https://github.com/nvim-telescope/telescope.nvim/issues/2201#issuecomment-1521122021 where I need to work outside both CWD and root directory. So far, since you must change CWD or root directory to use live_grep/find_file, I set search_dirs = { desired_directory } to use live_grep/find_files in a desired directory, which is quite cumbersome to type in command mode manually. Telescope-file-browser provides a nice interface for navigating directory structure (which Telescope doesn't provide), which I think would be very useful to be combined with live_grep/find_files that can be run in path.

Describe alternatives you've considered Since telescope-file-browser provides easy functionality to change CWD (change_cwd), I can achieve my goal by 1. changing CWD, and 2. live_grep/find_files with search_dirs = { vim.fn.getcwd() }. However, it is not as simple as toggling between directory-selection mode and live_grep/find_file, and most importantly, you must change CWD every time you want to live_grep/find_file in a different directory.

jamestrew commented 2 months ago

I commented a potential solution here https://github.com/nvim-telescope/telescope.nvim/issues/2201#issuecomment-1485886554

But I'm currently now this

local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")
local fb_utils = require("telescope._extensions.file_browser.utils")
local builtin = require("telescope.builtin")

-- helper function
local open_using = function(finder)
  return function(prompt_bufnr)

    -- gets selections based off tab-selected multi-select entries
    local selections = fb_utils.get_selected_files(prompt_bufnr, false)
    local search_dirs = vim.tbl_map(function(path)
      return path:absolute()
    end, selections)

    -- if nothing is multi-selected, just use the current `path` value
    if vim.tbl_isempty(search_dirs) then
      local current_finder = action_state.get_current_picker(prompt_bufnr).finder
      search_dirs = { current_finder.path }
    end
    actions.close(prompt_bufnr)
    finder({ search_dirs = search_dirs })
  end
end

require("telescope").setup({
  extensions = {
    file_browser = {
      mappings = {
        i = {
          ["<A-g>"] = open_using(builtin.live_grep),
          ["<A-f>"] = open_using(builtin.find_files),
        },
      },
    },
  },
})

The reason why we've resisted adding this to telescope because this can be a fairly opinionated workflow. Even for just myself, I've changed how the search_dirs option is determined for find_files, live_grep. I've yet to find, or hear a good solution that works for most everyone and that can be fairly easily incorporated into telescope, or at least worth the effort.

I hope that helps.