Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fall back to a function with BufferLinePick #230

Closed
kkharji opened this issue Sep 26, 2021 · 4 comments
Closed

feat: fall back to a function with BufferLinePick #230

kkharji opened this issue Sep 26, 2021 · 4 comments

Comments

@kkharji
Copy link

kkharji commented Sep 26, 2021

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
Copy link
Author

kkharji commented Sep 26, 2021

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
Copy link
Author

kkharji commented Sep 26, 2021

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

@akinsho
Copy link
Owner

akinsho commented Sep 27, 2021

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 akinsho changed the title feat: fall back to a funciton with BufferLinePick feat: fall back to a function with BufferLinePick Sep 27, 2021
@akinsho
Copy link
Owner

akinsho commented Apr 26, 2023

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 akinsho closed this as completed Apr 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants