Skip to content

Commit 5dd5c9f

Browse files
committed
fix(statusline): set hl_group for statusline separators
Fixes #569
1 parent 6f380b8 commit 5dd5c9f

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

lua/trouble/api.lua

+1-4
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,7 @@ function M.statusline(opts)
184184
end
185185
renderer:clear()
186186
renderer:sections({ section })
187-
status = renderer:statusline()
188-
if opts.hl_group then
189-
status = require("trouble.config.highlights").fix_statusline(status, opts.hl_group)
190-
end
187+
status = renderer:statusline({ hl_group = opts.hl_group })
191188
return status
192189
end,
193190
}

lua/trouble/config/highlights.lua

+16-15
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,22 @@ function M.source(source, links)
9393
end
9494

9595
M._fixed = {} ---@type table<string, string>
96-
---@param sl string
97-
function M.fix_statusline(sl, statusline_hl)
98-
local bg = vim.api.nvim_get_hl(0, { name = statusline_hl, link = false })
99-
bg = bg and bg.bg or nil
100-
101-
return sl:gsub("%%#(.-)#", function(hl)
102-
if not M._fixed[hl] then
103-
local opts = vim.api.nvim_get_hl(0, { name = hl, link = false }) or {}
104-
opts.bg = bg
105-
local group = "TroubleStatusline" .. vim.tbl_count(M._fixed)
106-
vim.api.nvim_set_hl(0, group, opts)
107-
M._fixed[hl] = group
108-
end
109-
return "%#" .. M._fixed[hl] .. "#"
110-
end)
96+
---@param hl string
97+
---@param statusline_hl string?
98+
function M.fix_statusline_bg(hl, statusline_hl)
99+
if not statusline_hl then
100+
return hl
101+
end
102+
local key = hl .. "_" .. statusline_hl
103+
if not M._fixed[key] then
104+
local opts = vim.api.nvim_get_hl(0, { name = hl, link = false }) or {}
105+
local statusline_opts = vim.api.nvim_get_hl(0, { name = statusline_hl, link = false })
106+
opts.bg = statusline_opts and statusline_opts.bg or nil
107+
local group = "TroubleStatusline" .. vim.tbl_count(M._fixed)
108+
vim.api.nvim_set_hl(0, group, opts)
109+
M._fixed[key] = group
110+
end
111+
return M._fixed[key]
111112
end
112113

113114
return M

lua/trouble/view/text.lua

+12-4
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,28 @@ function M:nl()
104104
return self
105105
end
106106

107-
---@param opts? {sep?:string}
107+
---@param opts? {sep?:string,hl_group?:string}
108108
function M:statusline(opts)
109109
local sep = opts and opts.sep or " "
110+
local hl_group = opts and opts.hl_group or nil
111+
if hl_group then
112+
sep = ("%%#%s#%s%%*"):format(hl_group, sep)
113+
end
110114
local lines = {} ---@type string[]
111115
for _, line in ipairs(self._lines) do
112116
local parts = {}
113117
for _, segment in ipairs(line) do
114118
local str = segment.str:gsub("%%", "%%%%")
115-
if segment.hl then
116-
str = ("%%#%s#%s%%*"):format(segment.hl, str)
119+
local fix_statusline_bg = require("trouble.config.highlights").fix_statusline_bg
120+
local hl = segment.hl and fix_statusline_bg(segment.hl, hl_group) or hl_group
121+
if hl then
122+
str = ("%%#%s#%s%%*"):format(hl, str)
117123
end
118124
parts[#parts + 1] = str
119125
end
120-
table.insert(lines, table.concat(parts, ""))
126+
if #parts ~= 0 then
127+
table.insert(lines, table.concat(parts, ""))
128+
end
121129
end
122130
return table.concat(lines, sep)
123131
end

0 commit comments

Comments
 (0)