Skip to content

Commit 9b771cc

Browse files
feature: Allow signs to be disabled per component
## Details Request: #64 Currently the controls that exist around sign column rendering are at a buffer level. Meaning either all signs are rendered for a particular buffer or none of them are. There are 2 components that can render into the sign column, headings & code blocks. Add a 'sign' field to both of these which can be toggled on / off. If the 'sign' field is set to false then do not attempt to render any signs for that component. These component level settings are checked first, before any of the buffer level ones, meaning they have the highest presedence. Other minor changes made: - Split heading icon & background to 2 different extmarks - Have each handler set 'render.md.Handler' as a parent class, this fixes the diagnostic error so no longer needs to be disabled - Make logger a class, missed this when adding everywhere else - Add more methods to logger - Have all other methods call into logger.error / logger.debug to avoid checking same condition in multiple places - Add UserWindowOption type to avoid type errors when only default or rendered is set, align with health check and use number or string instead of any
1 parent 3578523 commit 9b771cc

File tree

13 files changed

+108
-67
lines changed

13 files changed

+108
-67
lines changed

Diff for: README.md

+12-4
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,14 @@ require('render-markdown').setup({
174174
heading = {
175175
-- Turn on / off heading icon & background rendering
176176
enabled = true,
177+
-- Turn on / off any sign column related rendering
178+
sign = true,
177179
-- Replaces '#+' of 'atx_h._marker'
178180
-- The number of '#' in the heading determines the 'level'
179181
-- The 'level' is used to index into the array using a cycle
180182
-- The result is left padded with spaces to hide any additional '#'
181183
icons = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
182-
-- Added to the sign column
184+
-- Added to the sign column if enabled
183185
-- The 'level' is used to index into the array using a cycle
184186
signs = { '󰫎 ' },
185187
-- The 'level' is used to index into the array using a clamp
@@ -199,10 +201,12 @@ require('render-markdown').setup({
199201
code = {
200202
-- Turn on / off code block & inline code rendering
201203
enabled = true,
204+
-- Turn on / off any sign column related rendering
205+
sign = true,
202206
-- Determines how code blocks & inline code are rendered:
203207
-- none: disables all rendering
204208
-- normal: adds highlight group to code blocks & inline code
205-
-- language: adds language icon to sign column and icon + name above code blocks
209+
-- language: adds language icon to sign column if enabled and icon + name above code blocks
206210
-- full: normal + language
207211
style = 'full',
208212
-- Determins how the top / bottom of code block are rendered:
@@ -383,12 +387,14 @@ require('render-markdown').setup({
383387
heading = {
384388
-- Turn on / off heading icon & background rendering
385389
enabled = true,
390+
-- Turn on / off any sign column related rendering
391+
sign = true,
386392
-- Replaces '#+' of 'atx_h._marker'
387393
-- The number of '#' in the heading determines the 'level'
388394
-- The 'level' is used to index into the array using a cycle
389395
-- The result is left padded with spaces to hide any additional '#'
390396
icons = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
391-
-- Added to the sign column
397+
-- Added to the sign column if enabled
392398
-- The 'level' is used to index into the array using a cycle
393399
signs = { '󰫎 ' },
394400
-- The 'level' is used to index into the array using a clamp
@@ -415,10 +421,12 @@ require('render-markdown').setup({
415421
code = {
416422
-- Turn on / off code block & inline code rendering
417423
enabled = true,
424+
-- Turn on / off any sign column related rendering
425+
sign = true,
418426
-- Determines how code blocks & inline code are rendered:
419427
-- none: disables all rendering
420428
-- normal: adds highlight group to code blocks & inline code
421-
-- language: adds language icon to sign column and icon + name above code blocks
429+
-- language: adds language icon to sign column if enabled and icon + name above code blocks
422430
-- full: normal + language
423431
style = 'full',
424432
-- Determins how the top / bottom of code block are rendered:

Diff for: doc/render-markdown.txt

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 July 13
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 July 15
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -212,12 +212,14 @@ Full Default Configuration ~
212212
heading = {
213213
-- Turn on / off heading icon & background rendering
214214
enabled = true,
215+
-- Turn on / off any sign column related rendering
216+
sign = true,
215217
-- Replaces '#+' of 'atx_h._marker'
216218
-- The number of '#' in the heading determines the 'level'
217219
-- The 'level' is used to index into the array using a cycle
218220
-- The result is left padded with spaces to hide any additional '#'
219221
icons = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
220-
-- Added to the sign column
222+
-- Added to the sign column if enabled
221223
-- The 'level' is used to index into the array using a cycle
222224
signs = { '󰫎 ' },
223225
-- The 'level' is used to index into the array using a clamp
@@ -237,10 +239,12 @@ Full Default Configuration ~
237239
code = {
238240
-- Turn on / off code block & inline code rendering
239241
enabled = true,
242+
-- Turn on / off any sign column related rendering
243+
sign = true,
240244
-- Determines how code blocks & inline code are rendered:
241245
-- none: disables all rendering
242246
-- normal: adds highlight group to code blocks & inline code
243-
-- language: adds language icon to sign column and icon + name above code blocks
247+
-- language: adds language icon to sign column if enabled and icon + name above code blocks
244248
-- full: normal + language
245249
style = 'full',
246250
-- Determins how the top / bottom of code block are rendered:
@@ -420,12 +424,14 @@ HEADINGS *render-markdown-setup-headings*
420424
heading = {
421425
-- Turn on / off heading icon & background rendering
422426
enabled = true,
427+
-- Turn on / off any sign column related rendering
428+
sign = true,
423429
-- Replaces '#+' of 'atx_h._marker'
424430
-- The number of '#' in the heading determines the 'level'
425431
-- The 'level' is used to index into the array using a cycle
426432
-- The result is left padded with spaces to hide any additional '#'
427433
icons = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
428-
-- Added to the sign column
434+
-- Added to the sign column if enabled
429435
-- The 'level' is used to index into the array using a cycle
430436
signs = { '󰫎 ' },
431437
-- The 'level' is used to index into the array using a clamp
@@ -453,10 +459,12 @@ CODE BLOCKS *render-markdown-setup-code-blocks*
453459
code = {
454460
-- Turn on / off code block & inline code rendering
455461
enabled = true,
462+
-- Turn on / off any sign column related rendering
463+
sign = true,
456464
-- Determines how code blocks & inline code are rendered:
457465
-- none: disables all rendering
458466
-- normal: adds highlight group to code blocks & inline code
459-
-- language: adds language icon to sign column and icon + name above code blocks
467+
-- language: adds language icon to sign column if enabled and icon + name above code blocks
460468
-- full: normal + language
461469
style = 'full',
462470
-- Determins how the top / bottom of code block are rendered:

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ local cache = {
1010
expressions = {},
1111
}
1212

13-
---@class render.md.handler.Latex
13+
---@class render.md.handler.Latex: render.md.Handler
1414
local M = {}
1515

1616
---@param namespace integer

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

+20-23
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ local str = require('render-markdown.str')
99
local ts = require('render-markdown.ts')
1010
local util = require('render-markdown.util')
1111

12-
---@class render.md.handler.Markdown
12+
---@class render.md.handler.Markdown: render.md.Handler
1313
local M = {}
1414

1515
---@param namespace integer
@@ -42,15 +42,13 @@ M.render = function(namespace, root, buf)
4242
if nested_capture == 'quote_marker' then
4343
M.render_quote_marker(namespace, buf, nested_info, info)
4444
else
45-
-- Should only get here if user provides custom capture, currently unhandled
46-
logger.error('Unhandled markdown quote capture: ' .. nested_capture)
45+
logger.unhandled_capture('markdown quote', nested_capture)
4746
end
4847
end
4948
elseif capture == 'table' then
5049
M.render_table(namespace, buf, info)
5150
else
52-
-- Should only get here if user provides custom capture, currently unhandled
53-
logger.error('Unhandled markdown capture: ' .. capture)
51+
logger.unhandled_capture('markdown', capture)
5452
end
5553
end
5654
end
@@ -74,7 +72,6 @@ M.render_heading = function(namespace, buf, info)
7472
-- is added to account for the space after the last `#` but before the heading title,
7573
-- and concealed text is subtracted since that space is not usable
7674
local padding = level + 1 - ts.concealed(buf, info) - str.width(icon)
77-
7875
if padding < 0 then
7976
-- Requires inline extmarks to place when there is not enough space available
8077
if util.has_10 then
@@ -85,25 +82,25 @@ M.render_heading = function(namespace, buf, info)
8582
virt_text_pos = 'inline',
8683
conceal = '',
8784
})
88-
vim.api.nvim_buf_set_extmark(buf, namespace, info.start_row, 0, {
89-
end_row = info.end_row + 1,
90-
end_col = 0,
91-
hl_group = background,
92-
hl_eol = true,
93-
})
9485
end
9586
else
96-
vim.api.nvim_buf_set_extmark(buf, namespace, info.start_row, 0, {
97-
end_row = info.end_row + 1,
98-
end_col = 0,
99-
hl_group = background,
87+
vim.api.nvim_buf_set_extmark(buf, namespace, info.start_row, info.start_col, {
88+
end_row = info.end_row,
89+
end_col = info.end_col,
10090
virt_text = { { str.pad(icon, padding), { foreground, background } } },
10191
virt_text_pos = 'overlay',
102-
hl_eol = true,
10392
})
10493
end
94+
vim.api.nvim_buf_set_extmark(buf, namespace, info.start_row, 0, {
95+
end_row = info.end_row + 1,
96+
end_col = 0,
97+
hl_group = background,
98+
hl_eol = true,
99+
})
105100

106-
M.render_sign(namespace, buf, info, list.cycle(heading.signs, level), foreground)
101+
if heading.sign then
102+
M.render_sign(namespace, buf, info, list.cycle(heading.signs, level), foreground)
103+
end
107104
end
108105

109106
---@private
@@ -188,7 +185,9 @@ M.render_language = function(namespace, buf, info, code_block)
188185
if icon == nil or icon_highlight == nil then
189186
return false
190187
end
191-
M.render_sign(namespace, buf, info, icon, icon_highlight)
188+
if code.sign then
189+
M.render_sign(namespace, buf, info, icon, icon_highlight)
190+
end
192191
-- Requires inline extmarks
193192
if not util.has_10 then
194193
return false
@@ -351,8 +350,7 @@ M.render_table = function(namespace, buf, info)
351350
end
352351
M.render_table_row(namespace, buf, row, pipe_table.row)
353352
else
354-
-- Should only get here if markdown introduces more row types, currently unhandled
355-
logger.error('Unhandled markdown row type: ' .. row.type)
353+
logger.unhandled_type('markdown', 'row', row.type)
356354
end
357355
end
358356
if pipe_table.style == 'full' then
@@ -411,8 +409,7 @@ M.render_table_row = function(namespace, buf, row, highlight)
411409
end
412410
end
413411
else
414-
-- Should only get here if markdown introduces more cell types, currently unhandled
415-
logger.error('Unhandled markdown cell type: ' .. cell.type)
412+
logger.unhandled_type('markdown', 'cell', cell.type)
416413
end
417414
end
418415
elseif pipe_table.cell == 'overlay' then

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ local str = require('render-markdown.str')
66
local ts = require('render-markdown.ts')
77
local util = require('render-markdown.util')
88

9-
---@class render.md.handler.MarkdownInline
9+
---@class render.md.handler.MarkdownInline: render.md.Handler
1010
local M = {}
1111

1212
---@param namespace integer
@@ -25,8 +25,7 @@ M.render = function(namespace, root, buf)
2525
elseif capture == 'link' then
2626
M.render_link(namespace, buf, info)
2727
else
28-
-- Should only get here if user provides custom capture, currently unhandled
29-
logger.error('Unhandled inline capture: ' .. capture)
28+
logger.unhandled_capture('inline', capture)
3029
end
3130
end
3231
end

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

+12-6
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ local M = {}
77
---@field public render fun(namespace: integer, root: TSNode, buf: integer)
88
---@field public extends? boolean
99

10-
---@class render.md.WindowOption
11-
---@field public default any
12-
---@field public rendered any
10+
---@class render.md.UserWindowOption
11+
---@field public default? number|string
12+
---@field public rendered? number|string
1313

1414
---@class render.md.UserSign
1515
---@field public enabled? boolean
@@ -58,6 +58,7 @@ local M = {}
5858

5959
---@class render.md.UserCode
6060
---@field public enabled? boolean
61+
---@field public sign? boolean
6162
---@field public style? 'full'|'normal'|'language'|'none'
6263
---@field public border? 'thin'|'thick'
6364
---@field public above? string
@@ -66,6 +67,7 @@ local M = {}
6667

6768
---@class render.md.UserHeading
6869
---@field public enabled? boolean
70+
---@field public sign? boolean
6971
---@field public icons? string[]
7072
---@field public signs? string[]
7173
---@field public backgrounds? string[]
@@ -101,7 +103,7 @@ local M = {}
101103
---@field public callout? table<string, render.md.UserCustomComponent>
102104
---@field public link? render.md.UserLink
103105
---@field public sign? render.md.UserSign
104-
---@field public win_options? table<string, render.md.WindowOption>
106+
---@field public win_options? table<string, render.md.UserWindowOption>
105107
---@field public custom_handlers? table<string, render.md.Handler>
106108

107109
---@type render.md.Config
@@ -179,12 +181,14 @@ M.default_config = {
179181
heading = {
180182
-- Turn on / off heading icon & background rendering
181183
enabled = true,
184+
-- Turn on / off any sign column related rendering
185+
sign = true,
182186
-- Replaces '#+' of 'atx_h._marker'
183187
-- The number of '#' in the heading determines the 'level'
184188
-- The 'level' is used to index into the array using a cycle
185189
-- The result is left padded with spaces to hide any additional '#'
186190
icons = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
187-
-- Added to the sign column
191+
-- Added to the sign column if enabled
188192
-- The 'level' is used to index into the array using a cycle
189193
signs = { '󰫎 ' },
190194
-- The 'level' is used to index into the array using a clamp
@@ -204,10 +208,12 @@ M.default_config = {
204208
code = {
205209
-- Turn on / off code block & inline code rendering
206210
enabled = true,
211+
-- Turn on / off any sign column related rendering
212+
sign = true,
207213
-- Determines how code blocks & inline code are rendered:
208214
-- none: disables all rendering
209215
-- normal: adds highlight group to code blocks & inline code
210-
-- language: adds language icon to sign column and icon + name above code blocks
216+
-- language: adds language icon to sign column if enabled and icon + name above code blocks
211217
-- full: normal + language
212218
style = 'full',
213219
-- Determins how the top / bottom of code block are rendered:

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

+27-13
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ log.flush = function()
5454
log.reset()
5555
end
5656

57+
---@class render.md.Logger
5758
local M = {}
5859

5960
M.start = function()
@@ -67,26 +68,39 @@ M.debug = function(message)
6768
end
6869
end
6970

70-
---@param capture string
71-
---@param info render.md.NodeInfo
72-
M.debug_node_info = function(capture, info)
73-
if vim.tbl_contains({ 'debug' }, state.config.log_level) then
74-
log.add('debug', {
75-
capture = capture,
76-
text = info.text,
77-
rows = { info.start_row, info.end_row },
78-
cols = { info.start_col, info.end_col },
79-
})
80-
end
81-
end
82-
8371
---@param message any
8472
M.error = function(message)
8573
if vim.tbl_contains({ 'debug', 'error' }, state.config.log_level) then
8674
log.add('error', message)
8775
end
8876
end
8977

78+
---@param capture string
79+
---@param info render.md.NodeInfo
80+
M.debug_node_info = function(capture, info)
81+
M.debug({
82+
capture = capture,
83+
text = info.text,
84+
rows = { info.start_row, info.end_row },
85+
cols = { info.start_col, info.end_col },
86+
})
87+
end
88+
89+
---Encountered if user provides custom capture
90+
---@param group string
91+
---@param capture string
92+
M.unhandled_capture = function(group, capture)
93+
M.error(string.format('Unhandled %s capture: %s', group, capture))
94+
end
95+
96+
---Encountered if new type is seen for a particular group
97+
---@param language string
98+
---@param group string
99+
---@param value string
100+
M.unhandled_type = function(language, group, value)
101+
M.error(string.format('Unhandled %s %s type: %s', language, group, value))
102+
end
103+
90104
M.flush = function()
91105
log.flush()
92106
end

0 commit comments

Comments
 (0)