More user-friendly LualineBuffersJump (#780)

* buffers: user friendly LualineBuffersJump version

LualineBuffersJump throws an error when an attempt to jump to a
non-existent buffer index is made. It's expected from users to
trigger this error by accident while attempting to switch buffers.
To deal with this add <bang> support to LualineBuffersJump.

Signed-off-by: Plato Kiorpelidis <kioplato@gmail.com>

* chore: autogen (vimdocs+formating)

Co-authored-by: kioplato <kioplato@users.noreply.github.com>
This commit is contained in:
Plato 2022-08-08 18:52:28 +03:00 committed by GitHub
parent 4b5048aee1
commit 03bcf015d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 4 deletions

View File

@ -759,10 +759,14 @@ Shows currently open buffers. Like bufferline . See
for all builtin behaviors of buffers component. for all builtin behaviors of buffers component.
You can use `:LualineBuffersJump` to jump to buffer based on index You can use `:LualineBuffersJump` to jump to buffer based on index
of buffer in buffers component. of buffer in buffers component.
Jumping to non-existent buffer indices generates an error. To avoid these errors
`LualineBuffersJump` provides `<bang>` support, meaning that you can call
`:LualineBufferJump!` to ignore these errors.
```vim ```vim
:LualineBuffersJump 2 " Jumps to 2nd buffer in buffers component. :LualineBuffersJump 2 " Jumps to 2nd buffer in buffers component.
:LualineBuffersJump $ " Jumps to last buffer in buffers component. :LualineBuffersJump $ " Jumps to last buffer in buffers component.
:LualineBuffersJump! 3 " Attempts to jump to 3rd buffer, if it exists.
``` ```
#### Tabs #### Tabs

View File

@ -793,12 +793,19 @@ Buffers Shows currently open buffers. Like
builtin behaviors of buffers component. builtin behaviors of buffers component.
You can use `:LualineBuffersJump` to You can use `:LualineBuffersJump` to
jump to buffer based on index of buffer jump to buffer based on index of buffer
in buffers component. in buffers component. Jumping to
non-existent buffer indices generates an
error. To avoid these errors
`LualineBuffersJump` provides `<bang>`
support, meaning that you can call
`:LualineBufferJump!` to ignore these
errors.
> >
:LualineBuffersJump 2 " Jumps to 2nd buffer in buffers component. :LualineBuffersJump 2 " Jumps to 2nd buffer in buffers component.
:LualineBuffersJump $ " Jumps to last buffer in buffers component. :LualineBuffersJump $ " Jumps to last buffer in buffers component.
:LualineBuffersJump! 3 " Attempts to jump to 3rd buffer, if it exists.
< <

View File

@ -211,14 +211,18 @@ function M:draw()
return self.status return self.status
end end
function M.buffer_jump(buf_pos) function M.buffer_jump(buf_pos, bang)
if buf_pos == '$' then if buf_pos == '$' then
buf_pos = #M.bufpos2nr buf_pos = #M.bufpos2nr
else else
buf_pos = tonumber(buf_pos) buf_pos = tonumber(buf_pos)
end end
if buf_pos < 1 or buf_pos > #M.bufpos2nr then if buf_pos < 1 or buf_pos > #M.bufpos2nr then
if bang ~= '!' then
error('Error: Unable to jump buffer position out of range') error('Error: Unable to jump buffer position out of range')
else
return
end
end end
vim.api.nvim_set_current_buf(M.bufpos2nr[buf_pos]) vim.api.nvim_set_current_buf(M.bufpos2nr[buf_pos])
end end
@ -228,7 +232,7 @@ vim.cmd([[
execute ":buffer " . a:bufnr execute ":buffer " . a:bufnr
endfunction endfunction
command! -nargs=1 LualineBuffersJump call v:lua.require'lualine.components.buffers'.buffer_jump(<f-args>) command! -nargs=1 -bang LualineBuffersJump call v:lua.require'lualine.components.buffers'.buffer_jump(<f-args>, "<bang>")
]]) ]])
return M return M