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

feat: fall back to a function with BufferLinePick

Open kkharji opened this issue 4 years ago • 3 comments

Hey @akinsho

I have set a mapping to pick a buffer tab, but I wanted to have a fall back function (in my case telescope buffer)

I've changed select_buffer_apply a bit, but not sure how to make it configurable

local function select_buffer_apply(func)
 -- ...
  for _, item in ipairs(state.tabs) do
    local buf = item:as_buffer()
    if buf and letter == buf.letter then
      func(buf.id)
    else
      vim.cmd "Telescope buffers"
    end
  end
 -- ....
end

kkharji avatar Sep 26 '21 23:09 kkharji

my usecase is this:

  • <leader>b pick
  • not visible
  • press <space> have telescope buffers open

Though it works now, but there's a bit of delay from pressing space to the actual telescope picker open

kkharji avatar Sep 26 '21 23:09 kkharji

Okay found the reason of the delay and did this

-- Prompts user to select a buffer then applies a function to the buffer
---@param func fun(buf_id: number)
local function select_buffer_apply(func)
  state.is_picking = true
  local picked = false
  refresh()

  local char = vim.fn.getchar()
  local letter = vim.fn.nr2char(char)
  for _, item in ipairs(state.tabs) do
    local buf = item:as_buffer()
    if buf and letter == buf.letter then
      func(buf.id)
      picked = true
    elseif buf and letter == "\27" then -- ignore escape 
      picked = true
    end
  end

  state.is_picking = false
  refresh()
  return picked
end

function M.pick_buffer()
  if not select_buffer_apply(function(buf_id)
    vim.cmd("buffer " .. buf_id)
  end) then
    vim.cmd "Telescope buffers"
  end
end

kkharji avatar Sep 26 '21 23:09 kkharji

Hi @tami5,

So tbh I think adding a fallback is a complexity I'd rather leave to users since this seems like a specific workflow rather than general functionality that the majority of users need. I think your final example which just adds a return value to select_buffer_apply is a good solution. I'm not sure I understand the escape behaviour there though, if a user presses escape seems like picked should be false not true. Also checking directly against \27 doesn't look ideal.

If you have time to submit a PR would be happy to add a return value so you can implement picker buffer like that in your config. Otherwise I'll leave this open till when I have time in the future to add it.

akinsho avatar Sep 27 '21 10:09 akinsho

Gonna close this out or it'll be here forever. I personally have just never needed this and it hasn't been requested again after over a year now. I'm definitely happy to take it as a contribution but I think it's not something I'm personally going to get to.

akinsho avatar Apr 26 '23 17:04 akinsho