Skip to content

[Suggestion]: A way to programmatically do something with the selection results from UI picker #605

@lungsin

Description

@lungsin

Which fff frontend(s)?

Neovim plugin (fff.nvim)

What problem are you trying to solve?

I want to integrate fff.nvim with oil.nvim so that after I get the result from find_files(), I can use the result to open the directory containing that file using oil.

As a reference, the following is the way to configure this using snacks.picker and fzf.lua:

--- snacks.picker
keys = {
  {
      '<leader>fo',
      function()
        Snacks.picker.files({
          title = 'Oil',
          -- override the confirm action
          confirm = function(picker, item)
            picker:close()
            if not item then
              return
            end
            local file_path = Snacks.picker.util.path(item) or item.file
            if not file_path then
              return
            end
            local dir_path = file_path:match('^(.*)[/\\][^/\\]*$')
            require('oil').toggle_float(dir_path)
          end,
        })
      end,
      desc = 'Find directory (Oil)',
  }
}
--- fzf.lua
keys = {
   {
      '<leader>fo',
      function()
        FzfLua.files({
          prompt = 'Oil❯ ',
          actions = {
            -- override the default action
            ['default'] = function(selected, opts)
              opts.cwd = opts.cwd or vim.uv.cwd()
              if selected and #selected > 0 then
                local path = vim.split(selected[1], '')[2]
                local file_path = opts.cwd .. '/' .. path
                local dir_path = file_path:match('^(.*)[/\\][^/\\]*$')
                require('oil').toggle_float(dir_path)
              end
            end,
          },
        })
      end,
      desc = 'Find directory (Oil)',
   }
}

Proposed solution

I think the simplest non-breaking API change is by changing the public API of file_search(query, opts) so that the query argument becomes optional instead of required. When the query is nil, the UI is opened instead for the user to interact.

An alternative is by allowing custom actions in opts so that find_files(opts) can call a callback can be callback after the selection is done. But honestly, I don't know how to make the API nice. The select.select_window config seems to assumes that the chosen file must be opened in the buffer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions