Skip to content

Commit 8dcbf60

Browse files
committed
refactor(statuscolumn): :InpectTree window now uses custom statusline
fix(statuscolumn): `relativenumber` & `numberwidth` are now applied with the custom statuscolumn Fixes issues with extremely wide statuscolumn. doc(statuscolumn): Added some type definitions
1 parent db076b4 commit 8dcbf60

File tree

1 file changed

+82
-2
lines changed

1 file changed

+82
-2
lines changed

lua/bars/statuscolumn.lua

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
--- Custom statuscolumn module.
12
local statuscolumn = {};
23
local components = require("bars.components.statuscolumn");
34

@@ -8,13 +9,30 @@ local STC = "%!v:lua.require('bars.statuscolumn').render()";
89
---@type statuscolumn.config
910
statuscolumn.config = {
1011
ignore_filetypes = { "blink-cmp-menu" },
11-
ignore_buftypes = { "nofile", "help" },
12+
ignore_buftypes = { "help" },
13+
14+
condition = function (buffer)
15+
local ft, bt = vim.bo[buffer].ft, vim.bo[buffer].bt;
16+
17+
if bt == "nofile" and ft == "query" then
18+
--- Buffer for `:InspectTree`
19+
return true;
20+
elseif bt == "nofile" then
21+
--- Normal nofile buffer.
22+
return false;
23+
else
24+
return true;
25+
end
26+
end,
1227

1328
default = {
1429
parts = {
30+
---|fS
31+
1532
{
1633
kind = "empty",
1734
width = 1,
35+
1836
hl = "Normal"
1937
},
2038
{
@@ -157,10 +175,14 @@ statuscolumn.config = {
157175
---|fE
158176
end
159177
},
178+
179+
---|fE
160180
}
161181
},
162182

163183
query = {
184+
---|fS
185+
164186
condition = function (buffer)
165187
return vim.bo[buffer].ft == "query" and vim.bo[buffer].bt == "nofile";
166188
end,
@@ -169,9 +191,11 @@ statuscolumn.config = {
169191
{
170192
kind = "empty",
171193
width = 1,
172-
hl = "Normal"
194+
hl = "LineNr"
173195
},
174196
}
197+
198+
---|fE
175199
}
176200
};
177201

@@ -274,7 +298,12 @@ statuscolumn.render = function ()
274298
---|fE
275299
end
276300

301+
--- Can we detach from {win}?
302+
---@param win integer
303+
---@return boolean
277304
statuscolumn.can_detach = function (win)
305+
---|fS
306+
278307
if vim.api.nvim_win_is_valid(win) == false then
279308
return false;
280309
end
@@ -300,6 +329,8 @@ statuscolumn.can_detach = function (win)
300329
return false;
301330
end
302331
end
332+
333+
---|fE
303334
end
304335

305336
--- Detaches from {buffer}.
@@ -357,7 +388,13 @@ statuscolumn.detach = function (window)
357388
---|fE
358389
end
359390

391+
--- Can we attach to {win}
392+
---@param win integer
393+
---@param force? boolean Forcefully attach(for windows with state `false`).
394+
---@return boolean
360395
statuscolumn.can_attach = function (win, force)
396+
---|fS
397+
361398
if type(win) ~= "number" or vim.api.nvim_win_is_valid(win) == false then
362399
return false;
363400
elseif force ~= true and statuscolumn.state.attached_windows[win] == false then
@@ -387,6 +424,8 @@ statuscolumn.can_attach = function (win, force)
387424
return true;
388425
end
389426
end
427+
428+
---|fE
390429
end
391430

392431
--- Attaches the statuscolumn module to the windows
@@ -410,6 +449,9 @@ statuscolumn.attach = function (window, force)
410449
vim.w[window].__relativenumber = vim.wo[window].relativenumber;
411450
vim.w[window].__numberwidth = vim.wo[window].numberwidth;
412451

452+
vim.wo[window].relativenumber = true;
453+
vim.wo[window].numberwidth = 1;
454+
413455
--- If the statuscolumn matches the one we set then
414456
--- this is most likely due to inheriting window properties.
415457
---
@@ -424,6 +466,8 @@ end
424466

425467
--- Attaches globally.
426468
statuscolumn.global_attach = function ()
469+
---|fS
470+
427471
if statuscolumn.state.enable == false then
428472
return;
429473
end
@@ -432,8 +476,16 @@ statuscolumn.global_attach = function ()
432476
statuscolumn.update_id(window);
433477
end
434478

479+
vim.g.__relativenumber = vim.o.relativenumber;
480+
vim.g.__numberwidth = vim.o.numberwidth;
481+
482+
vim.o.relativenumber = true;
483+
vim.o.numberwidth = 1;
484+
435485
vim.g.__statuscolumn = vim.o.statuscolumn == STC and "" or vim.o.statuscolumn;
436486
vim.o.statuscolumn = STC;
487+
488+
---|fE
437489
end
438490

439491
--- Cleans up invalid buffers and recalculates
@@ -457,73 +509,99 @@ end
457509
--- Enables statuscolumn for `window`.
458510
---@param window integer
459511
statuscolumn.enable = function (window)
512+
---|fS
513+
460514
if type(window) ~= "number" or statuscolumn.state.attached_windows[window] == nil then
461515
return;
462516
end
463517

464518
statuscolumn.attach(window, true);
519+
520+
---|fE
465521
end
466522

467523
--- Enables *all* attached windows.
468524
statuscolumn.Enable = function ()
525+
---|fS
526+
469527
statuscolumn.state.enable = true;
470528

471529
for window, state in pairs(statuscolumn.state.attached_windows) do
472530
if state ~= true then
473531
statuscolumn.enable(window);
474532
end
475533
end
534+
535+
---|fE
476536
end
477537

478538
--- Disables statuscolumn for `window`.
479539
---@param window integer
480540
statuscolumn.disable = function (window)
541+
---|fS
542+
481543
if type(window) ~= "number" or statuscolumn.state.attached_windows[window] == nil then
482544
return;
483545
end
484546

485547
statuscolumn.detach(window);
548+
549+
---|fE
486550
end
487551

488552
--- Disables *all* attached windows.
489553
statuscolumn.Disable = function ()
554+
---|fS
555+
490556
for window, state in pairs(statuscolumn.state.attached_windows) do
491557
if state ~= false then
492558
statuscolumn.disable(window);
493559
end
494560
end
495561

496562
statuscolumn.state.enable = false;
563+
564+
---|fE
497565
end
498566

499567
----------------------------------------------------------------------
500568

501569
--- Toggles state of given window.
502570
---@param window integer
503571
statuscolumn.toggle = function (window)
572+
---|fS
573+
504574
if type(window) ~= "number" or statuscolumn.state.attached_windows[window] == nil then
505575
return;
506576
elseif statuscolumn.state.attached_windows[window] == true then
507577
statuscolumn.disable(window);
508578
else
509579
statuscolumn.enable(window);
510580
end
581+
582+
---|fE
511583
end
512584

513585
--- Toggles statuscolumn **globally**.
514586
statuscolumn.Toggle = function ()
587+
---|fS
588+
515589
if statuscolumn.state.enable == true then
516590
statuscolumn.Disable();
517591
else
518592
statuscolumn.Enable();
519593
end
594+
595+
---|fE
520596
end
521597

522598
----------------------------------------------------------------------
523599

524600
--- Sets up the statuscolumn module.
525601
---@param config statuscolumn.config | boolean | nil
526602
statuscolumn.setup = function (config)
603+
---|fS
604+
527605
if type(config) == "table" then
528606
statuscolumn.config = vim.tbl_extend("force", statuscolumn.config, config);
529607
elseif type(config) == "boolean" then
@@ -533,6 +611,8 @@ statuscolumn.setup = function (config)
533611
for window, _ in pairs(statuscolumn.state.attached_windows) do
534612
vim.w[window].__scID = statuscolumn.update_id(window);
535613
end
614+
615+
---|fE
536616
end
537617

538618
return statuscolumn;

0 commit comments

Comments
 (0)