nvim-telescope / telescope-file-browser.nvim

File Browser extension for telescope.nvim
MIT License
1.68k stars 92 forks source link

Is it possible to get whether a user open a file or exit the browser? #347

Closed nullptr-yxw closed 9 months ago

nullptr-yxw commented 9 months ago

For example, I want to create a usercmd Config, which will use the file_browser to choose file in vim.fn.stdpath('config'), and call vim.api.nvim_set_current_dir(vim.fn.stdpath("config")) if I open a file by <cr> in the file-browser (and do nothing if I exit the browser, for example, by typing esc)

-- An example for my perpose
M.start_config = function()
    local config_path = vim.fn.stdpath("config")
    require("telescope").extensions.file_browser.file_browser({
        path = config_path,
        cwd_to_path = true,
    })

    --the function will be called even if I exit the file_browser
    vim.api.nvim_set_current_dir(config_path) 
end

I don't know how to get whether the user open a file, and the fb_picker.file_browser cannot specify mapping for a certain browser as well. So how can I do this elegantly?

jamestrew commented 9 months ago

I think something like this is what you're asking for?

local function start_config()
  require("telescope").extensions.file_browser.file_browser({
    path = vim.fn.stdpath("config"),
    cwd_to_path = true,
    on_complete = {
      function(picker)
        local finder = picker.finder
        local path = finder.path
        if vim.fn.isdirectory(path) then
          vim.api.nvim_set_current_dir(path)
        end
      end,
    },
  })
end

This seems to work for me unless I've misinterpreted something. I'll close this issue but feel free to reopen/ask questions if I've missed anything.

jamestrew commented 9 months ago

I found a cleaner solution and updated my original response, in case you've seen it already.

nullptr-yxw commented 8 months ago

I think something like this is what you're asking for?

local function start_config()
  require("telescope").extensions.file_browser.file_browser({
    path = vim.fn.stdpath("config"),
    cwd_to_path = true,
    on_complete = {
      function(picker)
        local finder = picker.finder
        local path = finder.path
        if vim.fn.isdirectory(path) then
          vim.api.nvim_set_current_dir(path)
        end
      end,
    },
  })
end

This seems to work for me unless I've misinterpreted something. I'll close this issue but feel free to reopen/ask questions if I've missed anything.

Actually I'd like to change the dir only when I pick a file. If I close the file-browser by esc, the dir shouldn't be changed. However, I find that functions in on_complete will be called even though the file-browser is closed by esc. And I cannot find the document of on_complete. I have searched on_complete on :h telescope.nvim, but found nothing. So is there any way to call a function only when I pick a file?

jamestrew commented 8 months ago

I think the easiest (but not so elegant way) off the top of my head is to just copy/paste our select (<cr>) logic and add some lines to change cwd.

local function start_config()
  require("telescope").extensions.file_browser.file_browser({
    path = vim.fn.stdpath("config"),
    cwd_to_path = true,
    attach_mappings = function(_, _)
      local entry_is_dir = function()
        local entry = action_state.get_selected_entry()
        return entry and fb_utils.is_dir(entry.Path)
      end

      local entry_is_nil = function(prompt_bufnr)
        local prompt = action_state.get_current_picker(prompt_bufnr):_get_prompt()
        local entry = action_state.get_selected_entry()

        return entry == nil and #prompt > 0
      end

      action_set.select:replace_map({
        [entry_is_dir] = function(prompt_bufnr)
          -- changes here [[
          fb_actions.open_dir(prompt_bufnr)
          local current_picker = action_state.get_current_picker(prompt_bufnr)
          local finder = current_picker.finder
          vim.api.nvim_set_current_dir(finder.path)
          --]]
        end,
        [entry_is_nil] = fb_actions.create_from_prompt,
      })

      return true
    end,
  })
end