Skip to content

Commit d398f3e

Browse files
feature: Use SignColumn background for signs allow them to be disabled by buftype
# Details Request: #58 Not all color schemes use the Normal background color for the sign column. This can lead to some not so great looking icons. Fix this by combining the color we were going to set with the SignColumn background. Add a new configuration section 'sign' where this is defined and moifiable. While we are adding a new section for signs add 2 other abilities. - The ability to globally disable them from components that create them - The ability to disable them within a set of buftypes, use a default value of 'nofile' so that signs do not appear in LSP documentation
1 parent 8c71558 commit d398f3e

13 files changed

+123
-19
lines changed

Diff for: README.md

+10
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,16 @@ require('render-markdown').setup({
327327
-- Applies to the inlined icon
328328
highlight = '@markup.link.label.markdown_inline',
329329
},
330+
sign = {
331+
-- Turn on / off sign rendering
332+
enabled = true,
333+
-- More granular mechanism, disable signs within specific buftypes
334+
exclude = {
335+
buftypes = { 'nofile' },
336+
},
337+
-- Applies to background of sign text
338+
highlight = 'SignColumn',
339+
},
330340
-- Window options to use that change between rendered and raw view
331341
win_options = {
332342
-- See :h 'conceallevel'

Diff for: doc/render-markdown.txt

+10
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,16 @@ Full Default Configuration ~
364364
-- Applies to the inlined icon
365365
highlight = '@markup.link.label.markdown_inline',
366366
},
367+
sign = {
368+
-- Turn on / off sign rendering
369+
enabled = true,
370+
-- More granular mechanism, disable signs within specific buftypes
371+
exclude = {
372+
buftypes = { 'nofile' },
373+
},
374+
-- Applies to background of sign text
375+
highlight = 'SignColumn',
376+
},
367377
-- Window options to use that change between rendered and raw view
368378
win_options = {
369379
-- See :h 'conceallevel'

Diff for: lua/render-markdown/colors.lua

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---@class render.md.ColorCache
2+
---@field highlights string[]
3+
4+
---@type render.md.ColorCache
5+
local cache = {
6+
highlights = {},
7+
}
8+
9+
---@class render.md.Colors
10+
local M = {}
11+
12+
---@param foreground string
13+
---@param background string
14+
---@return string
15+
M.combine = function(foreground, background)
16+
local name = string.format('RenderMd_%s_%s', foreground, background)
17+
if not vim.tbl_contains(cache.highlights, name) then
18+
local fg = M.get_hl(foreground).fg
19+
local bg = M.get_hl(background).bg
20+
vim.api.nvim_set_hl(0, name, { fg = fg, bg = bg })
21+
table.insert(cache.highlights, name)
22+
end
23+
return name
24+
end
25+
26+
---@private
27+
---@param name string
28+
---@return vim.api.keyset.hl_info
29+
M.get_hl = function(name)
30+
return vim.api.nvim_get_hl(0, { name = name, link = false })
31+
end
32+
33+
return M

Diff for: lua/render-markdown/handler/latex.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ local logger = require('render-markdown.logger')
22
local state = require('render-markdown.state')
33
local ts = require('render-markdown.ts')
44

5-
---@class render.md.Cache
5+
---@class render.md.LatexCache
66
---@field expressions table<string, string[]>
77

8-
---@type render.md.Cache
8+
---@type render.md.LatexCache
99
local cache = {
1010
expressions = {},
1111
}

Diff for: lua/render-markdown/handler/markdown.lua

+24-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local colors = require('render-markdown.colors')
12
local component = require('render-markdown.component')
23
local icons = require('render-markdown.icons')
34
local list = require('render-markdown.list')
@@ -53,12 +54,7 @@ M.render_node = function(namespace, buf, capture, node)
5354
hl_eol = true,
5455
})
5556

56-
vim.api.nvim_buf_set_extmark(buf, namespace, info.start_row, info.start_col, {
57-
end_row = info.end_row,
58-
end_col = info.end_col,
59-
sign_text = list.cycle(heading.signs, level),
60-
sign_hl_group = foreground,
61-
})
57+
M.render_sign(namespace, buf, info, list.cycle(heading.signs, level), foreground)
6258
elseif capture == 'dash' then
6359
local dash = state.config.dash
6460
if not dash.enabled then
@@ -95,12 +91,7 @@ M.render_node = function(namespace, buf, capture, node)
9591
if icon == nil or icon_highlight == nil then
9692
return
9793
end
98-
vim.api.nvim_buf_set_extmark(buf, namespace, info.start_row, info.start_col, {
99-
end_row = info.end_row,
100-
end_col = info.end_col,
101-
sign_text = icon,
102-
sign_hl_group = icon_highlight,
103-
})
94+
M.render_sign(namespace, buf, info, icon, icon_highlight)
10495
-- Requires inline extmarks
10596
if not util.has_10 then
10697
return
@@ -221,6 +212,27 @@ M.render_node = function(namespace, buf, capture, node)
221212
end
222213
end
223214

215+
---@param namespace integer
216+
---@param buf integer
217+
---@param info render.md.NodeInfo
218+
---@param text string
219+
---@param highlight string
220+
M.render_sign = function(namespace, buf, info, text, highlight)
221+
local sign = state.config.sign
222+
if not sign.enabled then
223+
return
224+
end
225+
if vim.tbl_contains(sign.exclude.buftypes, util.get_buftype(buf)) then
226+
return
227+
end
228+
vim.api.nvim_buf_set_extmark(buf, namespace, info.start_row, info.start_col, {
229+
end_row = info.end_row,
230+
end_col = info.end_col,
231+
sign_text = text,
232+
sign_hl_group = colors.combine(highlight, sign.highlight),
233+
})
234+
end
235+
224236
---@param namespace integer
225237
---@param buf integer
226238
---@param info render.md.NodeInfo

Diff for: lua/render-markdown/init.lua

+16
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ local M = {}
1111
---@field public default any
1212
---@field public rendered any
1313

14+
---@class render.md.UserSign
15+
---@field public enabled? boolean
16+
---@field public exclude? render.md.UserExclude
17+
---@field public highlight? string
18+
1419
---@class render.md.UserLink
1520
---@field public enabled? boolean
1621
---@field public image? string
@@ -92,6 +97,7 @@ local M = {}
9297
---@field public pipe_table? render.md.UserPipeTable
9398
---@field public callout? table<string, render.md.UserCustomComponent>
9499
---@field public link? render.md.UserLink
100+
---@field public sign? render.md.UserSign
95101
---@field public win_options? table<string, render.md.WindowOption>
96102
---@field public custom_handlers? table<string, render.md.Handler>
97103

@@ -323,6 +329,16 @@ M.default_config = {
323329
-- Applies to the inlined icon
324330
highlight = '@markup.link.label.markdown_inline',
325331
},
332+
sign = {
333+
-- Turn on / off sign rendering
334+
enabled = true,
335+
-- More granular mechanism, disable signs within specific buftypes
336+
exclude = {
337+
buftypes = { 'nofile' },
338+
},
339+
-- Applies to background of sign text
340+
highlight = 'SignColumn',
341+
},
326342
-- Window options to use that change between rendered and raw view
327343
win_options = {
328344
-- See :h 'conceallevel'

Diff for: lua/render-markdown/manager.lua

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ function M.setup()
2020
group = group,
2121
pattern = state.config.file_types,
2222
callback = function(event)
23-
local buftype = vim.api.nvim_get_option_value('buftype', { buf = event.buf })
24-
if not vim.tbl_contains(state.config.exclude.buftypes, buftype) then
23+
if not vim.tbl_contains(state.config.exclude.buftypes, util.get_buftype(event.buf)) then
2524
M.attach(group, event.buf)
2625
end
2726
end,

Diff for: lua/render-markdown/state.lua

+12
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ function state.validate()
8181
quote = { config.quote, 'table' },
8282
callout = { config.callout, 'table' },
8383
link = { config.link, 'table' },
84+
sign = { config.sign, 'table' },
8485
win_options = { config.win_options, 'table' },
8586
custom_handlers = { config.custom_handlers, 'table' },
8687
})
@@ -186,6 +187,17 @@ function state.validate()
186187
highlight = { link.highlight, 'string' },
187188
})
188189

190+
local sign = config.sign
191+
append_errors('render-markdown.sign', sign, {
192+
enabled = { sign.enabled, 'boolean' },
193+
exclude = { sign.exclude, 'table' },
194+
highlight = { sign.highlight, 'string' },
195+
})
196+
local sign_exclude = sign.exclude
197+
append_errors('render-markdown.sign.exclude', sign_exclude, {
198+
buftypes = string_array(sign_exclude.buftypes),
199+
})
200+
189201
for name, win_option in pairs(config.win_options) do
190202
append_errors('render-markdown.win_options.' .. name, win_option, {
191203
default = { win_option.default, { 'number', 'string' } },

Diff for: lua/render-markdown/types.lua

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
---@class render.md.Sign
2+
---@field public enabled boolean
3+
---@field public exclude render.md.Exclude
4+
---@field public highlight string
5+
16
---@class render.md.Link
27
---@field public enabled boolean
38
---@field public image string
@@ -79,5 +84,6 @@
7984
---@field public pipe_table render.md.PipeTable
8085
---@field public callout table<string, render.md.CustomComponent>
8186
---@field public link render.md.Link
87+
---@field public sign render.md.Sign
8288
---@field public win_options table<string, render.md.WindowOption>
8389
---@field public custom_handlers table<string, render.md.Handler>

Diff for: lua/render-markdown/util.lua

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ M.set_win_option = function(win, name, value)
3434
logger.debug({ option = name, win = win, before = before, after = value })
3535
end
3636

37+
---@param buf integer
38+
---@return any
39+
M.get_buftype = function(buf)
40+
return vim.api.nvim_get_option_value('buftype', { buf = buf })
41+
end
42+
3743
---@param win integer
3844
---@return integer
3945
M.get_leftcol = function(win)

Diff for: tests/callout_spec.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ async_tests.describe('callout.md', function()
9292
row = { tip_start + 4, tip_start + 4 },
9393
col = { 5, 8 },
9494
sign_text = '󰢱 ',
95-
sign_hl_group = 'MiniIconsAzure',
95+
sign_hl_group = 'RenderMd_MiniIconsAzure_SignColumn',
9696
},
9797
{
9898
row = { tip_start + 4 },

Diff for: tests/heading_code_spec.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async_tests.describe('heading_code.md', function()
3636
row = { 12, 12 },
3737
col = { 3, 9 },
3838
sign_text = '󰌠 ',
39-
sign_hl_group = 'MiniIconsYellow',
39+
sign_hl_group = 'RenderMd_MiniIconsYellow_SignColumn',
4040
},
4141
{
4242
row = { 12 },

Diff for: tests/util.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ M.heading = function(row, level)
5252
row = { row, row },
5353
col = { 0, level },
5454
sign_text = '󰫎 ',
55-
sign_hl_group = foreground,
55+
sign_hl_group = string.format('RenderMd_%s_SignColumn', foreground),
5656
},
5757
}
5858
end

0 commit comments

Comments
 (0)