Skip to content

Commit 2a84411

Browse files
committed
refactor(winbar): Lookup -> depth
doc(winbar): Added type definitions
1 parent 529da96 commit 2a84411

File tree

2 files changed

+139
-93
lines changed

2 files changed

+139
-93
lines changed

lua/bars/components/winbar.lua

Lines changed: 83 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,10 @@
11
local wbC = {};
22
local utils = require("bars.utils");
33

4-
wbC.path = function (buffer, window, main_config)
5-
---@type string | ""
6-
local name = vim.api.nvim_buf_get_name(buffer);
7-
8-
if name == "" then
9-
return;
10-
else
11-
name = vim.fn.fnamemodify(name, ":~:.");
12-
end
13-
14-
---@type integer
15-
local throttle = main_config.throttle or 50;
16-
17-
---@type integer
18-
local before = vim.w[window].__path_time or 0;
19-
---@type string
20-
local old = vim.w[window].__path_data;
21-
---@type integer
22-
local now = vim.uv.hrtime();
23-
24-
if old and (now - before) < (throttle * 1e6) then
25-
return old;
26-
end
27-
28-
local sep = string.sub(package.config or "", 1, 1);
29-
30-
if sep == "\\" then
31-
name = string.gsub(name, "^%u:", "");
32-
end
33-
34-
---@type string[]
35-
local parts = vim.split(name, "/", { trimempty = true });
36-
local _o = "";
37-
38-
while #parts > 0 do
39-
local part = parts[#parts];
40-
41-
local part_config = utils.match(main_config, part, {});
42-
local is_dir = false;
43-
44-
if _o ~= "" and vim.fn.fnamemodify(part, ":e") == "" then
45-
is_dir = true;
46-
end
47-
48-
_o = table.concat({
49-
50-
utils.create_segmant(part_config.corner_left, part_config.corner_left_hl or part_config.hl),
51-
utils.create_segmant(part_config.padding_left, part_config.padding_left_hl or part_config.hl),
52-
utils.create_segmant(is_dir == true and part_config.dir_icon or part_config.icon, (is_dir == true and part_config.dir_icon_hl or part_config.icon_hl) or part_config.hl),
53-
54-
utils.create_segmant(part_config.text or part, part_config.hl),
55-
56-
utils.create_segmant(part_config.padding_right, part_config.padding_right_hl or part_config.hl),
57-
utils.create_segmant(part_config.corner_right, part_config.corner_right_hl or part_config.hl),
58-
59-
utils.create_segmant(_o ~= "" and main_config.separator or "", main_config.separator_hl),
60-
61-
_o,
62-
});
63-
64-
table.remove(parts, #parts);
65-
end
66-
67-
vim.w[window].__path_data = _o;
68-
vim.w[window].__path_time = now;
69-
70-
return _o;
71-
end
72-
734
--- Node under cursor
745
---@param buffer integer
756
---@param window integer
76-
---@param main_config winbar.node
7+
---@param main_config winbar.part.node
778
---@return string
789
wbC.node = function (buffer, window, main_config)
7910
---|fS
@@ -145,13 +76,13 @@ wbC.node = function (buffer, window, main_config)
14576
return node;
14677
end
14778

148-
local lookup = main_config.lookup or 9;
79+
local depth = main_config.depth or 9;
14980
local _o = "";
15081

15182
while node do
15283
---|fS
15384

154-
if lookup <= 0 then
85+
if depth <= 0 then
15586
break;
15687
end
15788

@@ -161,7 +92,7 @@ wbC.node = function (buffer, window, main_config)
16192
local item_config = utils.match(lang_config, node:type(), {});
16293
local has_sep = true;
16394

164-
if lookup == 1 then
95+
if depth == 1 then
16596
has_sep = false;
16697
elseif not node:parent() then
16798
has_sep = false;
@@ -194,7 +125,7 @@ wbC.node = function (buffer, window, main_config)
194125

195126
_o,
196127
});
197-
lookup = lookup - 1;
128+
depth = depth - 1;
198129

199130
::ignore::
200131

@@ -203,7 +134,7 @@ wbC.node = function (buffer, window, main_config)
203134
---|fE
204135
end
205136

206-
if lookup <= 0 and node then
137+
if depth <= 0 and node then
207138
local lanuage = get_language(node);
208139
local lang_config = utils.match(main_config, lanuage or "default", {});
209140

@@ -226,32 +157,97 @@ wbC.node = function (buffer, window, main_config)
226157
});
227158
end
228159

229-
if type(main_config.max_width) == "number" then
160+
vim.w[window].__node_data = _o;
161+
vim.w[window].__node_time = now;
162+
163+
return _o;
164+
165+
---|fE
166+
end
167+
168+
--- Node under cursor
169+
---@param buffer integer
170+
---@param window integer
171+
---@param main_config winbar.part.path
172+
---@return string
173+
wbC.path = function (buffer, window, main_config)
174+
---|fS
175+
176+
---@type string | ""
177+
local name = vim.api.nvim_buf_get_name(buffer);
178+
179+
if name == "" then
180+
return "";
181+
else
182+
name = vim.fn.fnamemodify(name, ":~:.");
183+
end
184+
185+
---@type integer
186+
local throttle = main_config.throttle or 50;
187+
188+
---@type integer
189+
local before = vim.w[window].__path_time or 0;
190+
---@type string
191+
local old = vim.w[window].__path_data;
192+
---@type integer
193+
local now = vim.uv.hrtime();
194+
195+
if old and (now - before) < (throttle * 1e6) then
196+
return old;
197+
end
198+
199+
local sep = string.sub(package.config or "", 1, 1);
200+
201+
if sep == "\\" then
202+
name = string.gsub(name, "^%u:", "");
203+
end
204+
205+
---@type string[]
206+
local parts = vim.split(name, sep, { trimempty = true });
207+
local _o = "";
208+
209+
while #parts > 0 do
210+
local part = parts[#parts];
211+
212+
local part_config = utils.match(main_config, part, {});
213+
local is_dir = false;
214+
215+
if _o ~= "" and vim.fn.fnamemodify(part, ":e") == "" then
216+
is_dir = true;
217+
end
218+
230219
_o = table.concat({
231-
"%",
232-
0,
233-
".",
234-
main_config.max_width,
235-
"(",
236-
" ",
220+
221+
utils.create_segmant(part_config.corner_left, part_config.corner_left_hl or part_config.hl),
222+
utils.create_segmant(part_config.padding_left, part_config.padding_left_hl or part_config.hl),
223+
utils.create_segmant(is_dir == true and part_config.dir_icon or part_config.icon, (is_dir == true and part_config.dir_icon_hl or part_config.icon_hl) or part_config.hl),
224+
225+
utils.create_segmant(part_config.text or part, part_config.hl),
226+
227+
utils.create_segmant(part_config.padding_right, part_config.padding_right_hl or part_config.hl),
228+
utils.create_segmant(part_config.corner_right, part_config.corner_right_hl or part_config.hl),
229+
230+
utils.create_segmant(_o ~= "" and main_config.separator or "", main_config.separator_hl),
231+
237232
_o,
238-
"%)"
239233
});
234+
235+
table.remove(parts, #parts);
240236
end
241237

242-
vim.w[window].__node_data = _o;
243-
vim.w[window].__node_time = now;
238+
vim.w[window].__path_data = _o;
239+
vim.w[window].__path_time = now;
244240

245241
return _o;
246242

247243
---|fE
248244
end
249245

250246
--- Custom section.
251-
---@param config winbae.parts.custom
247+
---@param config winbar.part.custom
252248
---@return string
253249
wbC.custom = function (_, _, config)
254-
return config.value;
250+
return config.value --[[ @as string ]];
255251
end
256252

257253
----------------------------------------------------------------------

0 commit comments

Comments
 (0)