Skip to content

Commit 691651d

Browse files
feat: dash width function
## Details Request: #537 Allow `dash.width` to be a function. Function is provided with the window width in the input context, more fields can be added later if needed. Allows greater flexibility in user setup.
1 parent d538564 commit 691651d

File tree

6 files changed

+61
-18
lines changed

6 files changed

+61
-18
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,9 @@ require('render-markdown').setup({
525525
style = 'full',
526526
},
527527
dash = {
528+
-- Useful context to have when evaluating values.
529+
-- | width | width of the current window |
530+
528531
-- Turn on / off thematic break rendering.
529532
enabled = true,
530533
-- Additional modes to render dash.
@@ -533,9 +536,11 @@ require('render-markdown').setup({
533536
-- The icon gets repeated across the window's width.
534537
icon = '',
535538
-- Width of the generated line.
536-
-- | <number> | a hard coded width value |
537-
-- | full | full width of the window |
538539
-- If a float < 1 is provided it is treated as a percentage of available window space.
540+
-- Output is evaluated depending on the type.
541+
-- | function | `value(context)` |
542+
-- | number | `value` |
543+
-- | full | width of the window |
539544
width = 'full',
540545
-- Amount of margin to add to the left of dash.
541546
-- If a float < 1 is provided it is treated as a percentage of available window space.
@@ -1191,6 +1196,9 @@ require('render-markdown').setup({
11911196
```lua
11921197
require('render-markdown').setup({
11931198
dash = {
1199+
-- Useful context to have when evaluating values.
1200+
-- | width | width of the current window |
1201+
11941202
-- Turn on / off thematic break rendering.
11951203
enabled = true,
11961204
-- Additional modes to render dash.
@@ -1199,9 +1207,11 @@ require('render-markdown').setup({
11991207
-- The icon gets repeated across the window's width.
12001208
icon = '',
12011209
-- Width of the generated line.
1202-
-- | <number> | a hard coded width value |
1203-
-- | full | full width of the window |
12041210
-- If a float < 1 is provided it is treated as a percentage of available window space.
1211+
-- Output is evaluated depending on the type.
1212+
-- | function | `value(context)` |
1213+
-- | number | `value` |
1214+
-- | full | width of the window |
12051215
width = 'full',
12061216
-- Amount of margin to add to the left of dash.
12071217
-- If a float < 1 is provided it is treated as a percentage of available window space.

doc/render-markdown.txt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For NVIM v0.11.4 Last change: 2025 October 09
1+
*render-markdown.txt* For NVIM v0.11.4 Last change: 2025 October 13
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -589,6 +589,9 @@ Default Configuration ~
589589
style = 'full',
590590
},
591591
dash = {
592+
-- Useful context to have when evaluating values.
593+
-- | width | width of the current window |
594+
592595
-- Turn on / off thematic break rendering.
593596
enabled = true,
594597
-- Additional modes to render dash.
@@ -597,9 +600,11 @@ Default Configuration ~
597600
-- The icon gets repeated across the window's width.
598601
icon = '─',
599602
-- Width of the generated line.
600-
-- | <number> | a hard coded width value |
601-
-- | full | full width of the window |
602603
-- If a float < 1 is provided it is treated as a percentage of available window space.
604+
-- Output is evaluated depending on the type.
605+
-- | function | `value(context)` |
606+
-- | number | `value` |
607+
-- | full | width of the window |
603608
width = 'full',
604609
-- Amount of margin to add to the left of dash.
605610
-- If a float < 1 is provided it is treated as a percentage of available window space.
@@ -1247,6 +1252,9 @@ Dashed Line Configuration ~
12471252
>lua
12481253
require('render-markdown').setup({
12491254
dash = {
1255+
-- Useful context to have when evaluating values.
1256+
-- | width | width of the current window |
1257+
12501258
-- Turn on / off thematic break rendering.
12511259
enabled = true,
12521260
-- Additional modes to render dash.
@@ -1255,9 +1263,11 @@ Dashed Line Configuration ~
12551263
-- The icon gets repeated across the window's width.
12561264
icon = '─',
12571265
-- Width of the generated line.
1258-
-- | <number> | a hard coded width value |
1259-
-- | full | full width of the window |
12601266
-- If a float < 1 is provided it is treated as a percentage of available window space.
1267+
-- Output is evaluated depending on the type.
1268+
-- | function | `value(context)` |
1269+
-- | number | `value` |
1270+
-- | full | width of the window |
12611271
width = 'full',
12621272
-- Amount of margin to add to the left of dash.
12631273
-- If a float < 1 is provided it is treated as a percentage of available window space.

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ local state = require('render-markdown.state')
66
local M = {}
77

88
---@private
9-
M.version = '8.9.5'
9+
M.version = '8.9.6'
1010

1111
function M.check()
1212
M.start('versions')

lua/render-markdown/lib/env.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ function M.win.view(win)
250250
return vim.api.nvim_win_call(win, vim.fn.winsaveview)
251251
end
252252

253+
---@param win integer
254+
---@return integer
255+
function M.win.width(win)
256+
local infos = vim.fn.getwininfo(win)
257+
local textoff = #infos == 1 and infos[1].textoff or 0
258+
return vim.api.nvim_win_get_width(win) - textoff
259+
end
260+
253261
---@param win integer
254262
---@param value number
255263
---@param used integer
@@ -260,9 +268,7 @@ function M.win.percent(win, value, used)
260268
elseif value >= 1 then
261269
return value
262270
else
263-
local infos = vim.fn.getwininfo(win)
264-
local textoff = #infos == 1 and infos[1].textoff or 0
265-
local width = vim.api.nvim_win_get_width(win) - textoff - used
271+
local width = M.win.width(win) - used
266272
return math.floor((width * value) + 0.5)
267273
end
268274
end

lua/render-markdown/render/markdown/dash.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function Render:setup()
2020
end
2121
local width = self:get_width(self.config.width, 0)
2222
local margin = self:get_width(self.config.left_margin, width)
23-
if width <= 0 and margin <= 0 then
23+
if width <= 0 then
2424
return false
2525
end
2626
self.data = { width = width, margin = margin }
@@ -34,6 +34,8 @@ end
3434
function Render:get_width(width, used)
3535
if type(width) == 'number' then
3636
return env.win.percent(self.context.win, width, used)
37+
elseif type(width) == 'function' then
38+
return width({ width = env.win.width(self.context.win) })
3739
else
3840
return vim.o.columns
3941
end

lua/render-markdown/settings.lua

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -633,10 +633,19 @@ M.dash = {}
633633
---@field left_margin number
634634
---@field highlight string
635635

636-
---@alias render.md.dash.Width 'full'|number
636+
---@class (exact) render.md.dash.Context
637+
---@field width integer
638+
639+
---@alias render.md.dash.Width
640+
---| 'full'
641+
---| number
642+
---| fun(ctx: render.md.dash.Context): integer
637643

638644
---@type render.md.dash.Config
639645
M.dash.default = {
646+
-- Useful context to have when evaluating values.
647+
-- | width | width of the current window |
648+
640649
-- Turn on / off thematic break rendering.
641650
enabled = true,
642651
-- Additional modes to render dash.
@@ -645,9 +654,11 @@ M.dash.default = {
645654
-- The icon gets repeated across the window's width.
646655
icon = '',
647656
-- Width of the generated line.
648-
-- | <number> | a hard coded width value |
649-
-- | full | full width of the window |
650657
-- If a float < 1 is provided it is treated as a percentage of available window space.
658+
-- Output is evaluated depending on the type.
659+
-- | function | `value(context)` |
660+
-- | number | `value` |
661+
-- | full | width of the window |
651662
width = 'full',
652663
-- Amount of margin to add to the left of dash.
653664
-- If a float < 1 is provided it is treated as a percentage of available window space.
@@ -661,7 +672,11 @@ function M.dash.schema()
661672
return M.base.schema({
662673
icon = { type = 'string' },
663674
width = {
664-
union = { { enum = { 'full' } }, { type = 'number' } },
675+
union = {
676+
{ enum = { 'full' } },
677+
{ type = 'number' },
678+
{ type = 'function' },
679+
},
665680
},
666681
left_margin = { type = 'number' },
667682
highlight = { type = 'string' },

0 commit comments

Comments
 (0)