feat: show index in buffers component instead of bufnr (#628)

Co-authored-by: shadmansaleh <13149513+shadmansaleh@users.noreply.github.com>
This commit is contained in:
Jinq Qian 2022-04-13 13:01:42 +08:00 committed by GitHub
parent 00dc7929fe
commit c2165540a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 13 deletions

View File

@ -438,8 +438,8 @@ sections = {
show_modified_status = true, -- Shows indicator when the buffer is modified. show_modified_status = true, -- Shows indicator when the buffer is modified.
mode = 0, -- 0: Shows buffer name mode = 0, -- 0: Shows buffer name
-- 1: Shows buffer index (bufnr) -- 1: Shows buffer index
-- 2: Shows buffer name + buffer index (bufnr) -- 2: Shows buffer name + buffer index
max_length = vim.o.columns * 2 / 3, -- Maximum width of buffers component, max_length = vim.o.columns * 2 / 3, -- Maximum width of buffers component,
-- it can also be a function that returns -- it can also be a function that returns
@ -610,8 +610,8 @@ sections = {
show_modified_status = true, -- Shows indicator when the window is modified. show_modified_status = true, -- Shows indicator when the window is modified.
mode = 0, -- 0: Shows window name mode = 0, -- 0: Shows window name
-- 1: Shows window index (bufnr) -- 1: Shows window index
-- 2: Shows window name + window index (bufnr) -- 2: Shows window name + window index
max_length = vim.o.columns * 2 / 3, -- Maximum width of windows component, max_length = vim.o.columns * 2 / 3, -- Maximum width of windows component,
-- it can also be a function that returns -- it can also be a function that returns
@ -673,6 +673,12 @@ tabline = {
Shows currently open buffers. Like bufferline . See Shows currently open buffers. Like bufferline . See
[buffers options](#buffers-component-options) [buffers options](#buffers-component-options)
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
of buffer in buffers component.
```vim
:LualineBuffersJump 2 " Jumps to 2nd buffer in buffers component.
:LualineBuffersJump $ " Jumps to last buffer in buffers component.
```
#### Tabs #### Tabs
Shows currently open tab. Like usual tabline. See Shows currently open tab. Like usual tabline. See

View File

@ -455,8 +455,8 @@ Component specific options These are options that are available on
show_modified_status = true, -- Shows indicator when the buffer is modified. show_modified_status = true, -- Shows indicator when the buffer is modified.
mode = 0, -- 0: Shows buffer name mode = 0, -- 0: Shows buffer name
-- 1: Shows buffer index (bufnr) -- 1: Shows buffer index
-- 2: Shows buffer name + buffer index (bufnr) -- 2: Shows buffer name + buffer index
max_length = vim.o.columns * 2 / 3, -- Maximum width of buffers component, max_length = vim.o.columns * 2 / 3, -- Maximum width of buffers component,
-- it can also be a function that returns -- it can also be a function that returns
@ -634,8 +634,8 @@ Component specific options These are options that are available on
show_modified_status = true, -- Shows indicator when the window is modified. show_modified_status = true, -- Shows indicator when the window is modified.
mode = 0, -- 0: Shows window name mode = 0, -- 0: Shows window name
-- 1: Shows window index (bufnr) -- 1: Shows window index
-- 2: Shows window name + window index (bufnr) -- 2: Shows window name + window index
max_length = vim.o.columns * 2 / 3, -- Maximum width of windows component, max_length = vim.o.columns * 2 / 3, -- Maximum width of windows component,
-- it can also be a function that returns -- it can also be a function that returns
@ -704,6 +704,15 @@ Buffers Shows currently open buffers. Like
bufferline. See bufferline. See
|lualine-buffers-options| for all |lualine-buffers-options| for all
builtin behaviors of buffers component. builtin behaviors of buffers component.
You can use `:LualineBuffersJump` to
jump to buffer based on index of buffer
in buffers component.
>
:LualineBuffersJump 2 " Jumps to 2nd buffer in buffers component.
:LualineBuffersJump $ " Jumps to last buffer in buffers component.
<
*lualine-Tabs* *lualine-Tabs*

View File

@ -10,6 +10,7 @@ local modules = require('lualine_require').lazy_require {
function Buffer:init(opts) function Buffer:init(opts)
assert(opts.bufnr, 'Cannot create Buffer without bufnr') assert(opts.bufnr, 'Cannot create Buffer without bufnr')
self.bufnr = opts.bufnr self.bufnr = opts.bufnr
self.buf_index = opts.buf_index
self.options = opts.options self.options = opts.options
self.highlights = opts.highlights self.highlights = opts.highlights
self:get_props() self:get_props()
@ -162,10 +163,10 @@ function Buffer:apply_mode(name)
end end
if self.options.mode == 1 then if self.options.mode == 1 then
return string.format('%s %s%s', self.bufnr, self.icon, self.modified_icon) return string.format('%s %s%s', self.buf_index or '', self.icon, self.modified_icon)
end end
return string.format('%s %s%s%s', self.bufnr, self.icon, name, self.modified_icon) return string.format('%s %s%s%s', self.buf_index or '', self.icon, name, self.modified_icon)
end end
return Buffer return Buffer

View File

@ -67,11 +67,12 @@ function M:init(options)
end end
end end
function M:new_buffer(bufnr) function M:new_buffer(bufnr, buf_index)
bufnr = bufnr or vim.api.nvim_get_current_buf() bufnr = bufnr or vim.api.nvim_get_current_buf()
buf_index = buf_index or ''
return Buffer:new { return Buffer:new {
bufnr = bufnr, bufnr = bufnr,
buf_index = buf_index,
options = self.options, options = self.options,
highlights = self.highlights, highlights = self.highlights,
} }
@ -79,9 +80,11 @@ end
function M:buffers() function M:buffers()
local buffers = {} local buffers = {}
M.bufpos2nr = {}
for b = 1, vim.fn.bufnr('$') do for b = 1, vim.fn.bufnr('$') do
if vim.fn.buflisted(b) ~= 0 and vim.api.nvim_buf_get_option(b, 'buftype') ~= 'quickfix' then if vim.fn.buflisted(b) ~= 0 and vim.api.nvim_buf_get_option(b, 'buftype') ~= 'quickfix' then
buffers[#buffers + 1] = self:new_buffer(b) buffers[#buffers + 1] = self:new_buffer(b, #buffers + 1)
M.bufpos2nr[#buffers] = b
end end
end end
@ -213,10 +216,24 @@ function M:draw()
return self.status return self.status
end end
function M.buffer_jump(buf_pos)
if buf_pos == '$' then
buf_pos = #M.bufpos2nr
else
buf_pos = tonumber(buf_pos)
end
if buf_pos < 1 or buf_pos > #M.bufpos2nr then
error('Error: Unable to jump buffer position out of range')
end
vim.api.nvim_set_current_buf(M.bufpos2nr[buf_pos])
end
vim.cmd([[ vim.cmd([[
function! LualineSwitchBuffer(bufnr, mouseclicks, mousebutton, modifiers) function! LualineSwitchBuffer(bufnr, mouseclicks, mousebutton, modifiers)
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>)
]]) ]])
return M return M