Debian Codegraph @

lua/lualine/components/tabs/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 Tab = require('lualine.components.tabs.tab')
5 local M = require('lualine.component'):extend()
6 local highlight = require('lualine.highlight')
7
8 local default_options = {
9 max_length = 0,
10 tab_max_length = 40,
11 mode = 0,
12 use_mode_colors = false,
13 path = 0,
14 tabs_color = {
15 active = nil,
16 inactive = nil,
17 },
18 show_modified_status = true,
19 symbols = {
20 modified = '[+]',
21 },
22 }
23
24 -- This function is duplicated in buffers
25 ---returns the proper hl for tab in section. Used for setting default highlights
26 ---@param section string name of section tabs component is in
27 ---@param is_active boolean
28 ---@return string hl name
29 local function get_hl(section, is_active)
30 local suffix = is_active and highlight.get_mode_suffix() or '_inactive'
31 local section_redirects = {
32 lualine_x = 'lualine_c',
33 lualine_y = 'lualine_b',
34 lualine_z = 'lualine_a',
35 }
36 if section_redirects[section] then
37 section = highlight.highlight_exists(section .. suffix) and section or section_redirects[section]
38 end
39 return section .. suffix
40 end
41
42 function M:init(options)
43 M.super.init(self, options)
44 -- if use_mode_colors is set, use a function so that the colors update
45 local default_active = options.use_mode_colors
46 and function()
47 return get_hl('lualine_' .. options.self.section, true)
48 end
49 or get_hl('lualine_' .. options.self.section, true)
50 default_options.tabs_color = {
51 active = default_active,
52 inactive = get_hl('lualine_' .. options.self.section, false),
53 }
54 self.options = vim.tbl_deep_extend('keep', self.options or {}, default_options)
55 -- stylua: ignore
56 self.highlights = {
57 active = self:create_hl(self.options.tabs_color.active, 'active'),
58 inactive = self:create_hl(self.options.tabs_color.inactive, 'inactive'),
59 }
60 end
61
62 function M:update_status()
63 local data = {}
64 local tabs = {}
65 for nr, id in ipairs(vim.api.nvim_list_tabpages()) do
66 tabs[#tabs + 1] = Tab { tabId = id, tabnr = nr, options = self.options, highlights = self.highlights }
67 end
68 -- mark the first, last, current, before current, after current tabpages
69 -- for rendering
70 local current = vim.fn.tabpagenr()
71 tabs[1].first = true
72 tabs[#tabs].last = true
73 if tabs[current] then
74 tabs[current].current = true
75 end
76 if tabs[current - 1] then
77 tabs[current - 1].beforecurrent = true
78 end
79 if tabs[current + 1] then
80 tabs[current + 1].aftercurrent = true
81 end
82
83 local max_length = self.options.max_length
84 if type(max_length) == 'function' then
85 max_length = max_length(self)
86 end
87
88 if max_length == 0 then
89 max_length = math.floor(vim.o.columns / 3)
90 end
91 local total_length
92 for i, tab in pairs(tabs) do
93 if tab.current then
94 current = i
95 end
96 end
97 local current_tab = tabs[current]
98 -- start drawing from current tab and draw left and right of it until
99 -- all tabpages are drawn or max_length has been reached.
100 if current_tab == nil then -- maybe redundant code
101 local t = Tab {
102 tabId = vim.api.nvim_get_current_tabpage(),
103 tabnr = vim.fn.tabpagenr(),
104 options = self.options,
105 highlights = self.highlights,
106 }
107 t.current = true
108 t.last = true
109 data[#data + 1] = t:render()
110 else
111 data[#data + 1] = current_tab:render()
112 total_length = current_tab.len
113 local i = 0
114 local before, after
115 while true do
116 i = i + 1
117 before = tabs[current - i]
118 after = tabs[current + i]
119 local rendered_before, rendered_after
120 if before == nil and after == nil then
121 break
122 end
123 -- draw left most undrawn tab if fits in max_length
124 if before then
125 rendered_before = before:render()
126 total_length = total_length + before.len
127 if total_length > max_length then
128 break
129 end
130 table.insert(data, 1, rendered_before)
131 end
132 -- draw right most undrawn tab if fits in max_length
133 if after then
134 rendered_after = after:render()
135 total_length = total_length + after.len
136 if total_length > max_length then
137 break
138 end
139 data[#data + 1] = rendered_after
140 end
141 end
142 -- draw ellipsis (...) on relevant sides if all tabs don't fit in max_length
143 if total_length > max_length then
144 if before ~= nil then
145 before.ellipse = true
146 before.first = true
147 table.insert(data, 1, before:render())
148 end
149 if after ~= nil then
150 after.ellipse = true
151 after.last = true
152 data[#data + 1] = after:render()
153 end
154 end
155 end
156
157 return table.concat(data)
158 end
159
160 function M:draw()
161 self.status = ''
162 self.applied_separator = ''
163
164 if self.options.cond ~= nil and self.options.cond() ~= true then
165 return self.status
166 end
167 local status = self:update_status()
168 if type(status) == 'string' and #status > 0 then
169 self.status = status
170 self:apply_section_separators()
171 self:apply_separator()
172 end
173 return self.status
174 end
175
176 vim.cmd([[
177 function! LualineSwitchTab(tabnr, mouseclicks, mousebutton, modifiers)
178 execute a:tabnr . "tabnext"
179 endfunction
180
181 function! LualineRenameTab(...)
182 if a:0 == 1
183 let t:tabname = a:1
184 else
185 unlet t:tabname
186 end
187 redrawtabline
188 endfunction
189
190 command! -nargs=? LualineRenameTab call LualineRenameTab("<args>")
191 ]])
192
193 return M