| 1 |
local Window = require('lualine.components.windows.window') |
| 2 |
local M = require('lualine.components.buffers'):extend() |
| 3 |
|
| 4 |
local default_options = { |
| 5 |
disabled_filetypes = {}, |
| 6 |
disabled_buftypes = { 'quickfix', 'prompt' }, |
| 7 |
} |
| 8 |
|
| 9 |
function M:init(options) |
| 10 |
options.buffers_color = nil -- buffers_color isn't windows option. |
| 11 |
M.super.init(self, options) |
| 12 |
|
| 13 |
self.options = vim.tbl_deep_extend('keep', self.options or {}, default_options) |
| 14 |
self.options.windows_color = vim.tbl_deep_extend('keep', self.options.windows_color or {}, self.options.buffers_color) |
| 15 |
self.options.buffers_color = nil -- this is the default value of colors generated by parent buffers component. |
| 16 |
|
| 17 |
self.highlights = { |
| 18 |
active = self:create_hl(self.options.windows_color.active, 'active'), |
| 19 |
inactive = self:create_hl(self.options.windows_color.inactive, 'inactive'), |
| 20 |
} |
| 21 |
end |
| 22 |
|
| 23 |
function M:new_buffer(winnr) |
| 24 |
winnr = winnr or vim.api.nvim_get_current_win() |
| 25 |
|
| 26 |
return Window:new { |
| 27 |
winnr = winnr, |
| 28 |
options = self.options, |
| 29 |
highlights = self.highlights, |
| 30 |
} |
| 31 |
end |
| 32 |
|
| 33 |
--- Override to only return buffers shown in the windows of the current tab |
| 34 |
function M:buffers() |
| 35 |
local tabnr = vim.api.nvim_get_current_tabpage() |
| 36 |
local buffers = {} |
| 37 |
|
| 38 |
for _, winnr in ipairs(vim.api.nvim_tabpage_list_wins(tabnr)) do |
| 39 |
if not self:should_hide(winnr) then |
| 40 |
buffers[#buffers + 1] = self:new_buffer(winnr) |
| 41 |
end |
| 42 |
end |
| 43 |
|
| 44 |
return buffers |
| 45 |
end |
| 46 |
|
| 47 |
function M:should_hide(winnr) |
| 48 |
local bufnr = vim.api.nvim_win_get_buf(winnr) |
| 49 |
local filetype = vim.api.nvim_buf_get_option(bufnr, 'filetype') |
| 50 |
local buftype = vim.api.nvim_buf_get_option(bufnr, 'buftype') |
| 51 |
local is_filetype_disabled = vim.tbl_contains(self.options.disabled_filetypes, filetype) |
| 52 |
local is_buftype_disabled = vim.tbl_contains(self.options.disabled_buftypes, buftype) |
| 53 |
local is_floating = '' ~= vim.api.nvim_win_get_config(winnr).relative |
| 54 |
|
| 55 |
return is_floating or is_buftype_disabled or is_filetype_disabled |
| 56 |
end |
| 57 |
|
| 58 |
vim.cmd([[ |
| 59 |
function! LualineSwitchWindow(win_number, mouseclicks, mousebutton, modifiers) |
| 60 |
execute a:win_number . 'wincmd w' |
| 61 |
endfunction |
| 62 |
]]) |
| 63 |
|
| 64 |
return M |