Skip to content

Commit 2369dcf

Browse files
committed
feat(statuscolumn): Added filter support for signs
Signs can now be filtered to show only the signs you need! doc(statuscolumn): Added missing component documentations
1 parent 134c7d1 commit 2369dcf

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

lua/bars/components/statuscolumn.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,28 @@ scC.signs = function (buffer, _, config)
302302
local _o = " ";
303303
local priority = -999;
304304

305+
---@type table<integer, string>
306+
local ns_map = {};
307+
308+
for ns, id in pairs(vim.api.nvim_get_namespaces()) do
309+
ns_map[id] = ns;
310+
end
311+
305312
for _, sign in ipairs(extmarks) do
306313
local sPR = sign[4].priority or 0;
314+
---@diagnostic disable-next-line
315+
local has_filter, pass = pcall(config.filter, buffer, ns_map, unpack(sign));
316+
317+
if has_filter == true and pass ~= true then
318+
goto continue;
319+
end
307320

308321
if sPR > priority and sign[4].sign_text then
309322
_o = utils.set_hl(sign[4].sign_hl_group) .. utils.align("left", sign[4].sign_text, 2);
310323
priority = sPR;
311324
end
325+
326+
::continue::
312327
end
313328

314329
return _o;
@@ -326,6 +341,13 @@ end
326341
scC.get = function (name, buffer, window, part_config, statuscolumn)
327342
---|fS
328343

344+
--- Options that shouldn't be
345+
--- evaluated.
346+
---@type { [string]: string[] }
347+
local eval_ignore = {
348+
signs = { "filter" }
349+
};
350+
329351
if type(name) ~= "string" then
330352
--- Component doesn't exist.
331353
return "";
@@ -353,6 +375,8 @@ scC.get = function (name, buffer, window, part_config, statuscolumn)
353375
for key, value in pairs(static_config) do
354376
if type(value) ~= "function" then
355377
goto continue;
378+
elseif vim.list_contains(eval_ignore[name] or {}, key) then
379+
goto continue;
356380
end
357381

358382
local s_success, s_val = pcall(value, buffer, window, statuscolumn);

lua/bars/statuscolumn.lua

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,41 @@ statuscolumn.config = {
1616
{
1717
kind = "signs",
1818
width = 1,
19-
hl = "Normal"
19+
hl = "Normal",
20+
21+
filter = function (buffer, namespaces, _, _, _, details)
22+
---@type string
23+
local mode = vim.api.nvim_get_mode().mode;
24+
local name = namespaces[details.ns_id] or "";
25+
26+
if package.loaded["markview"] and vim.bo[buffer] == "markdown" then
27+
--- On markdown files when on normal
28+
--- mode only show markview signs.
29+
if mode == "n" then
30+
return string.match(name, "^markview");
31+
else
32+
return true;
33+
end
34+
elseif package.loaded["helpview"] and vim.bo[buffer].ft == "help" then
35+
--- On help files when on normal
36+
--- mode only show helpview signs.
37+
if mode == "n" then
38+
return string.match(name, "^helpview");
39+
else
40+
return true;
41+
end
42+
else
43+
if mode == "n" then
44+
--- On normal mode only show LSP signs.
45+
return string.match(name, "^vim[%._]lsp") ~= nil;
46+
elseif vim.list_contains({ "i", "v", "V", "" }, mode) then
47+
--- On visual mode only show git signs.
48+
return string.match(name, "^gitsigns") ~= nil;
49+
end
50+
51+
return true;
52+
end
53+
end
2054
},
2155
{
2256
kind = "folds",

lua/definitions/statuscolumn.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@
102102
--- Border for the statuscolumn.
103103
---@class statuscolumn.parts.border
104104
---
105+
--- Condition for this component.
106+
---@field condition? fun(buffer: integer, window: integer, statuscolumn: string): boolean
107+
---
105108
--- What kind of component is this?
106109
---@field kind "border"
107110
---
@@ -117,6 +120,9 @@
117120
--- Fold column for the statuscolumn.
118121
---@class statuscolumn.parts.folds
119122
---
123+
--- Condition for this component.
124+
---@field condition? fun(buffer: integer, window: integer, statuscolumn: string): boolean
125+
---
120126
--- What kind of component is this?
121127
---@field kind "folds"
122128
---
@@ -179,7 +185,18 @@
179185

180186
---@class statuscolumn.parts.signs
181187
---
188+
--- Condition for this component.
189+
---@field condition? fun(buffer: integer, window: integer, statuscolumn: string): boolean
190+
---
191+
--- What kind of component is this?
182192
---@field kind "signs"
193+
---
194+
--- Filter for signs.
195+
---@field filter? fun(buffer: integer, ns_map: table<integer, string>, ns: integer, row: integer, col: integer, extmark: table): boolean
196+
---
197+
--- Highlight group for the signs.
198+
--- NOTE, This will overwrite the
199+
--- sign's original highlight group.
183200
---@field hl? string
184201

185202

0 commit comments

Comments
 (0)