Debian Codegraph @

lua/lualine/components/buffers/init.lua

raw
1 -- Copyright (c) 2020-2021 shadmansaleh
2 -- MIT license, see LICENSE for more details.
3 local require = require('lualine_require').require
4 local Buffer = require('lualine.components.buffers.buffer')
5 local M = require('lualine.component'):extend()
6 local highlight = require('lualine.highlight')
7
8 local default_options = {
9 show_filename_only = true,
10 hide_filename_extension = false,
11 show_modified_status = true,
12 mode = 0,
13 max_length = 0,
14 filetype_names = {
15 TelescopePrompt = 'Telescope',
16 dashboard = 'Dashboard',
17 packer = 'Packer',
18 fzf = 'FZF',
19 alpha = 'Alpha',
20 },
21 use_mode_colors = false,
22 buffers_color = {
23 active = nil,
24 inactive = nil,
25 },
26 symbols = {
27 modified = ' ●',
28 alternate_file = '#',
29 directory = '',
30 },
31 }
32
33 -- This function is duplicated in tabs
34 ---returns the proper hl for buffer in section. Used for setting default highlights
35 ---@param section string name of section buffers component is in
36 ---@param is_active boolean
37 ---@return string hl name
38 local function get_hl(section, is_active)
39 local suffix = is_active and highlight.get_mode_suffix() or '_inactive'
40 local section_redirects = {
41 lualine_x = 'lualine_c',
42 lualine_y = 'lualine_b',
43 lualine_z = 'lualine_a',
44 }
45 if section_redirects[section] then
46 section = highlight.highlight_exists(section .. suffix) and section or section_redirects[section]
47 end
48 return section .. suffix
49 end
50
51 function M:init(options)
52 M.super.init(self, options)
53 -- if use_mode_colors is set, use a function so that the colors update
54 local default_active = options.use_mode_colors
55 and function()
56 return get_hl('lualine_' .. options.self.section, true)
57 end
58 or get_hl('lualine_' .. options.self.section, true)
59 default_options.buffers_color = {
60 active = default_active,
61 inactive = get_hl('lualine_' .. options.self.section, false),
62 }
63 self.options = vim.tbl_deep_extend('keep', self.options or {}, default_options)
64 if self.options.component_name == 'buffers' then
65 self.highlights = {
66 active = self:create_hl(self.options.buffers_color.active, 'active'),
67 inactive = self:create_hl(self.options.buffers_color.inactive, 'inactive'),
68 }
69 end
70 end
71
72 function M:new_buffer(bufnr, buf_index)
73 bufnr = bufnr or vim.api.nvim_get_current_buf()
74 buf_index = buf_index or ''
75 return Buffer:new {
76 bufnr = bufnr,
77 buf_index = buf_index,
78 options = self.options,
79 highlights = self.highlights,
80 }
81 end
82
83 function M:buffers()
84 local buffers = {}
85 M.bufpos2nr = {}
86 for b = 1, vim.fn.bufnr('$') do
87 if vim.fn.buflisted(b) ~= 0 and vim.api.nvim_buf_get_option(b, 'buftype') ~= 'quickfix' then
88 buffers[#buffers + 1] = self:new_buffer(b, #buffers + 1)
89 M.bufpos2nr[#buffers] = b
90 end
91 end
92
93 return buffers
94 end
95
96 function M:update_status()
97 local data = {}
98 local buffers = self:buffers()
99 local current = -2
100 -- mark the first, last, current, before current, after current buffers
101 -- for rendering
102 if buffers[1] then
103 buffers[1].first = true
104 end
105 if buffers[#buffers] then
106 buffers[#buffers].last = true
107 end
108 for i, buffer in ipairs(buffers) do
109 if buffer:is_current() then
110 buffer.current = true
111 current = i
112 end
113 end
114 if buffers[current - 1] then
115 buffers[current - 1].beforecurrent = true
116 end
117 if buffers[current + 1] then
118 buffers[current + 1].aftercurrent = true
119 end
120
121 local max_length = self.options.max_length
122 if type(max_length) == 'function' then
123 max_length = max_length(self)
124 end
125
126 if max_length == 0 then
127 max_length = math.floor(2 * vim.o.columns / 3)
128 end
129 local total_length
130 for i, buffer in pairs(buffers) do
131 if buffer.current then
132 current = i
133 end
134 end
135 -- start drawing from current buffer and draw left and right of it until
136 -- all buffers are drawn or max_length has been reached.
137 if current == -2 then
138 local b = self:new_buffer()
139 b.current = true
140 if self.options.self.section < 'x' then
141 b.last = true
142 if #buffers > 0 then
143 buffers[#buffers].last = nil
144 end
145 buffers[#buffers + 1] = b
146 current = #buffers
147 else
148 b.first = true
149 if #buffers > 0 then
150 buffers[1].first = nil
151 end
152 table.insert(buffers, 1, b)
153 current = 1
154 end
155 end
156 local current_buffer = buffers[current]
157 data[#data + 1] = current_buffer:render()
158 total_length = current_buffer.len
159 local i = 0
160 local before, after
161 while true do
162 i = i + 1
163 before = buffers[current - i]
164 after = buffers[current + i]
165 local rendered_before, rendered_after
166 if before == nil and after == nil then
167 break
168 end
169 -- draw left most undrawn buffer if fits in max_length
170 if before then
171 rendered_before = before:render()
172 total_length = total_length + before.len
173 if total_length > max_length then
174 break
175 end
176 table.insert(data, 1, rendered_before)
177 end
178 -- draw right most undrawn buffer if fits in max_length
179 if after then
180 rendered_after = after:render()
181 total_length = total_length + after.len
182 if total_length > max_length then
183 break
184 end
185 data[#data + 1] = rendered_after
186 end
187 end
188 -- draw ellipsis (...) on relevant sides if all buffers don't fit in max_length
189 if total_length > max_length then
190 if before ~= nil then
191 before.ellipse = true
192 before.first = true
193 table.insert(data, 1, before:render())
194 end
195 if after ~= nil then
196 after.ellipse = true
197 after.last = true
198 data[#data + 1] = after:render()
199 end
200 end
201
202 return table.concat(data)
203 end
204
205 function M:draw()
206 self.status = ''
207 self.applied_separator = ''
208
209 if self.options.cond ~= nil and self.options.cond() ~= true then
210 return self.status
211 end
212 local status = self:update_status()
213 if type(status) == 'string' and #status > 0 then
214 self.status = status
215 self:apply_section_separators()
216 self:apply_separator()
217 end
218 return self.status
219 end
220
221 function M.buffer_jump(buf_pos, bang)
222 if buf_pos == '$' then
223 buf_pos = #M.bufpos2nr
224 else
225 buf_pos = tonumber(buf_pos)
226 end
227 if buf_pos < 1 or buf_pos > #M.bufpos2nr then
228 if bang ~= '!' then
229 error('Error: Unable to jump buffer position out of range')
230 else
231 return
232 end
233 end
234 vim.api.nvim_set_current_buf(M.bufpos2nr[buf_pos])
235 end
236
237 vim.cmd([[
238 function! LualineSwitchBuffer(bufnr, mouseclicks, mousebutton, modifiers)
239 execute ":buffer " . a:bufnr
240 endfunction
241
242 command! -nargs=1 -bang LualineBuffersJump call v:lua.require'lualine.components.buffers'.buffer_jump(<f-args>, "<bang>")
243 ]])
244
245 return M