Skip to content

Commit 932432c

Browse files
fix: padding for inline code and list items
## Details Issue: #364 Broke in last refactor due to passing list of lines to `virt_text` property. Fix is to get rid of the additional list wrapper. Added unit tests to catch this in the future.
1 parent 81374ff commit 932432c

File tree

7 files changed

+183
-36
lines changed

7 files changed

+183
-36
lines changed

doc/render-markdown.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2025 March 07
1+
*render-markdown.txt* For 0.10.0 Last change: 2025 March 09
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*

lua/render-markdown/health.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local state = require('render-markdown.state')
55
local M = {}
66

77
---@private
8-
M.version = '8.1.0'
8+
M.version = '8.1.1'
99

1010
function M.check()
1111
M.start('version')

lua/render-markdown/render/code_inline.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function Render:side_padding(highlight, row, col)
3333
if #line > 0 then
3434
self.marks:add(true, row, col, {
3535
priority = 0,
36-
virt_text = { line },
36+
virt_text = line,
3737
virt_text_pos = 'inline',
3838
})
3939
end

lua/render-markdown/render/list_item.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ function Render:side_padding(row, col, amount)
187187
if #line > 0 then
188188
self.marks:add(false, row, col, {
189189
priority = 0,
190-
virt_text = { line },
190+
virt_text = line,
191191
virt_text_pos = 'inline',
192192
})
193193
end

tests/list_table_spec.lua

+140-7
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ describe('list_table.md', function()
3737
util.table_border(row:increment(2), true, { 8, 15, 7, 6 }),
3838
util.table_pipe(row:get(), 0, true),
3939
util.highlight(row:get(), 2, 8, 'CodeInline'),
40-
util.table_padding(row:get(), 9, 2),
40+
util.padding(row:get(), 9, 2, 'table'),
4141
util.table_pipe(row:get(), 9, true),
42-
util.table_padding(row:get(), 11, 3),
42+
util.padding(row:get(), 11, 3, 'table'),
4343
util.conceal(row:get(), 24, 25),
4444
util.table_pipe(row:get(), 25, true),
4545
util.table_pipe(row:get(), 33, true),
@@ -49,21 +49,21 @@ describe('list_table.md', function()
4949
vim.list_extend(expected, {
5050
util.table_pipe(row:increment(), 0, false),
5151
util.highlight(row:get(), 2, 8, 'CodeInline'),
52-
util.table_padding(row:get(), 9, 2),
52+
util.padding(row:get(), 9, 2, 'table'),
5353
util.table_pipe(row:get(), 9, false),
54-
util.table_padding(row:get(), 11, 4),
54+
util.padding(row:get(), 11, 4, 'table'),
5555
util.table_pipe(row:get(), 25, false),
5656
util.table_pipe(row:get(), 33, false),
5757
util.table_pipe(row:get(), 40, false),
5858
})
5959
vim.list_extend(expected, {
6060
util.table_pipe(row:increment(), 0, false),
6161
util.table_pipe(row:get(), 9, false),
62-
util.table_padding(row:get(), 11, 3),
62+
util.padding(row:get(), 11, 3, 'table'),
6363
util.link(row:get(), 11, 24, 'link'),
64-
util.table_padding(row:get(), 25, 4),
64+
util.padding(row:get(), 25, 4, 'table'),
6565
util.table_pipe(row:get(), 25, false),
66-
util.table_padding(row:get(), 27, 1),
66+
util.padding(row:get(), 27, 1, 'table'),
6767
util.conceal(row:get(), 32, 33),
6868
util.table_pipe(row:get(), 33, false),
6969
util.table_pipe(row:get(), 40, false),
@@ -99,4 +99,137 @@ describe('list_table.md', function()
9999
' 24 [example]: https://example.com',
100100
})
101101
end)
102+
103+
it('padding', function()
104+
util.setup('demo/list_table.md', {
105+
code = { inline_pad = 2 },
106+
bullet = { left_pad = 2, right_pad = 2 },
107+
})
108+
109+
local expected, row = {}, util.row()
110+
111+
vim.list_extend(expected, util.heading(row:get(), 1))
112+
113+
vim.list_extend(expected, {
114+
util.padding(row:increment(2), 0, 2),
115+
util.bullet(row:get(), 0, 1),
116+
util.padding(row:get(), 1, 2),
117+
util.link(row:get(), 20, 47, 'web'),
118+
119+
util.padding(row:increment(), 0, 2),
120+
util.bullet(row:get(), 0, 1),
121+
util.padding(row:get(), 1, 2),
122+
util.padding(row:get(), 20, 2, 'code'),
123+
util.highlight(row:get(), 20, 28, 'CodeInline'),
124+
util.padding(row:get(), 28, 2, 'code'),
125+
126+
util.padding(row:increment(), 0, 2),
127+
util.bullet(row:get(), 2, 2, 2),
128+
util.padding(row:get(), 5, 2),
129+
130+
util.padding(row:increment(), 0, 2),
131+
util.bullet(row:get(), 4, 2),
132+
util.padding(row:get(), 5, 2),
133+
134+
util.padding(row:increment(), 0, 2),
135+
util.bullet(row:get(), 6, 3),
136+
util.padding(row:get(), 7, 2),
137+
138+
util.padding(row:increment(), 0, 2),
139+
util.bullet(row:get(), 8, 4),
140+
util.padding(row:get(), 9, 2),
141+
142+
util.padding(row:increment(), 0, 2),
143+
util.bullet(row:get(), 10, 1),
144+
util.padding(row:get(), 11, 2),
145+
146+
util.padding(row:increment(), 0, 2),
147+
util.bullet(row:get(), 0, 1),
148+
util.padding(row:get(), 1, 2),
149+
util.link(row:get(), 20, 45, 'link'),
150+
})
151+
152+
vim.list_extend(expected, util.heading(row:increment(2), 1))
153+
154+
vim.list_extend(expected, {
155+
util.padding(row:increment(2), 0, 2),
156+
util.ordered(row:get(), 0, '1.'),
157+
util.padding(row:get(), 2, 2),
158+
159+
util.padding(row:increment(), 0, 2),
160+
util.ordered(row:get(), 0, '2.'),
161+
util.padding(row:get(), 2, 2),
162+
})
163+
164+
vim.list_extend(expected, util.heading(row:increment(2), 1))
165+
166+
vim.list_extend(expected, {
167+
util.table_border(row:increment(2), true, { 10, 15, 7, 6 }),
168+
util.table_pipe(row:get(), 0, true),
169+
util.padding(row:get(), 2, 2, 'code'),
170+
util.highlight(row:get(), 2, 8, 'CodeInline'),
171+
util.padding(row:get(), 8, 2, 'code'),
172+
util.table_pipe(row:get(), 9, true),
173+
util.padding(row:get(), 11, 3, 'table'),
174+
util.conceal(row:get(), 24, 25),
175+
util.table_pipe(row:get(), 25, true),
176+
util.table_pipe(row:get(), 33, true),
177+
util.table_pipe(row:get(), 40, true),
178+
})
179+
table.insert(expected, util.table_delimiter(row:increment(), { { 1, 9 }, { 1, 13, 1 }, { 6, 1 }, 6 }, nil, 2))
180+
vim.list_extend(expected, {
181+
util.table_pipe(row:increment(), 0, false),
182+
util.padding(row:get(), 2, 2, 'code'),
183+
util.highlight(row:get(), 2, 8, 'CodeInline'),
184+
util.padding(row:get(), 8, 2, 'code'),
185+
util.table_pipe(row:get(), 9, false),
186+
util.padding(row:get(), 11, 4, 'table'),
187+
util.table_pipe(row:get(), 25, false),
188+
util.table_pipe(row:get(), 33, false),
189+
util.table_pipe(row:get(), 40, false),
190+
})
191+
vim.list_extend(expected, {
192+
util.table_pipe(row:increment(), 0, false),
193+
util.padding(row:get(), 9, 2, 'table'),
194+
util.table_pipe(row:get(), 9, false),
195+
util.padding(row:get(), 11, 3, 'table'),
196+
util.link(row:get(), 11, 24, 'link'),
197+
util.padding(row:get(), 25, 4, 'table'),
198+
util.table_pipe(row:get(), 25, false),
199+
util.padding(row:get(), 27, 1, 'table'),
200+
util.conceal(row:get(), 32, 33),
201+
util.table_pipe(row:get(), 33, false),
202+
util.table_pipe(row:get(), 40, false),
203+
util.table_border(row:get(), false, { 10, 15, 7, 6 }),
204+
})
205+
206+
util.assert_view(expected, {
207+
'󰫎 1 󰲡 Unordered List',
208+
' 2',
209+
' 3 ● List Item 1: with 󰖟 link',
210+
' 4 ● List Item 2: with inline code',
211+
' 5 ○ Nested List 1 Item 1',
212+
' 6 ○ Nested List 1 Item 2',
213+
' 7 ◆ Nested List 2 Item 1',
214+
' 8 ◇ Nested List 3 Item 1',
215+
' 9 ● Nested List 4 Item 1',
216+
' 10 ● List Item 3: with 󰌹 reference link',
217+
' 11',
218+
'󰫎 12 󰲡 Ordered List',
219+
' 13',
220+
' 14 1. Item 1',
221+
' 15 2. Item 2',
222+
' 16',
223+
'󰫎 17 󰲡 Table',
224+
' 18',
225+
' ┌──────────┬───────────────┬───────┬──────┐',
226+
' 19 │ Left │ Center │ Right │ None │',
227+
' 20 ├━─────────┼━─────────────━┼──────━┼──────┤',
228+
' 21 │ Code │ Bold │ Plain │ Item │',
229+
' 22 │ Item │ 󰌹 link │ Item │ Item │',
230+
' └──────────┴───────────────┴───────┴──────┘',
231+
' 23',
232+
' 24 [example]: https://example.com',
233+
})
234+
end)
102235
end)

tests/table_spec.lua

+9-9
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ describe('table.md', function()
1313
util.table_border(row:increment(2), true, { 11, 24 }),
1414
util.table_pipe(row:get(), 0, true),
1515
util.table_pipe(row:get(), 12, true),
16-
util.table_padding(row:get(), 14, 13),
16+
util.padding(row:get(), 14, 13, 'table'),
1717
util.highlight(row:get(), 14, 25, 'CodeInline'),
1818
util.conceal(row:get(), 26, 37),
1919
util.table_pipe(row:get(), 37, true),
2020
util.table_delimiter(row:increment(), { 11, { 23, 1 } }),
2121
util.table_pipe(row:increment(), 0, false),
2222
util.highlight(row:get(), 2, 12, 'CodeInline'),
23-
util.table_padding(row:get(), 13, 2),
23+
util.padding(row:get(), 13, 2, 'table'),
2424
util.table_pipe(row:get(), 13, false),
25-
util.table_padding(row:get(), 15, 16),
25+
util.padding(row:get(), 15, 16, 'table'),
2626
util.link(row:get(), 15, 38, 'web'),
2727
util.table_pipe(row:get(), 39, false),
2828
util.table_pipe(row:increment(), 0, false),
29-
util.table_padding(row:get(), 12, 8),
29+
util.padding(row:get(), 12, 8, 'table'),
3030
util.table_pipe(row:get(), 12, false),
31-
util.table_padding(row:get(), 14, 16),
31+
util.padding(row:get(), 14, 16, 'table'),
3232
util.inline_highlight(row:get(), 14, 25),
3333
util.conceal(row:get(), 26, 38),
3434
util.table_pipe(row:get(), 38, false),
@@ -84,15 +84,15 @@ describe('table.md', function()
8484
util.table_delimiter(row:increment(), { 11, { 10, 1 } }, 13),
8585
util.table_pipe(row:increment(), 0, false),
8686
util.highlight(row:get(), 2, 12, 'CodeInline'),
87-
util.table_padding(row:get(), 13, 2),
87+
util.padding(row:get(), 13, 2, 'table'),
8888
util.table_pipe(row:get(), 13, false),
89-
util.table_padding(row:get(), 15, 3),
89+
util.padding(row:get(), 15, 3, 'table'),
9090
util.link(row:get(), 15, 38, 'web'),
9191
util.table_pipe(row:get(), 39, false),
9292
util.table_pipe(row:increment(), 0, false),
93-
util.table_padding(row:get(), 12, 8),
93+
util.padding(row:get(), 12, 8, 'table'),
9494
util.table_pipe(row:get(), 12, false),
95-
util.table_padding(row:get(), 14, 3),
95+
util.padding(row:get(), 14, 3, 'table'),
9696
util.inline_highlight(row:get(), 14, 25),
9797
util.conceal(row:get(), 26, 38),
9898
util.table_pipe(row:get(), 38, false),

tests/util.lua

+30-16
Original file line numberDiff line numberDiff line change
@@ -353,31 +353,40 @@ end
353353

354354
---@param row integer
355355
---@param col integer
356-
---@param head boolean
356+
---@param spaces integer
357+
---@param kind? 'code'|'table'
357358
---@return render.md.MarkInfo
358-
function M.table_pipe(row, col, head)
359-
local highlight = head and 'TableHead' or 'TableRow'
359+
function M.padding(row, col, spaces, kind)
360+
local highlight
361+
if kind == 'code' then
362+
highlight = M.hl('CodeInline')
363+
elseif kind == 'table' then
364+
highlight = M.hl('TableFill')
365+
else
366+
highlight = 'Normal'
367+
end
360368
---@type render.md.MarkInfo
361369
return {
362-
row = { row, row },
363-
col = { col, col + 1 },
364-
virt_text = { { '', M.hl(highlight) } },
365-
virt_text_pos = 'overlay',
370+
row = { row },
371+
col = { col },
372+
virt_text = { { string.rep(' ', spaces), highlight } },
373+
virt_text_pos = 'inline',
374+
priority = 0,
366375
}
367376
end
368377

369378
---@param row integer
370379
---@param col integer
371-
---@param spaces integer
380+
---@param head boolean
372381
---@return render.md.MarkInfo
373-
function M.table_padding(row, col, spaces)
382+
function M.table_pipe(row, col, head)
383+
local highlight = head and 'TableHead' or 'TableRow'
374384
---@type render.md.MarkInfo
375385
return {
376-
row = { row },
377-
col = { col },
378-
virt_text = { { string.rep(' ', spaces), M.hl('TableFill') } },
379-
virt_text_pos = 'inline',
380-
priority = 0,
386+
row = { row, row },
387+
col = { col, col + 1 },
388+
virt_text = { { '', M.hl(highlight) } },
389+
virt_text_pos = 'overlay',
381390
}
382391
end
383392

@@ -407,8 +416,9 @@ end
407416
---@param row integer
408417
---@param sections (integer|integer[])[]
409418
---@param suffix? integer
419+
---@param offset? integer
410420
---@return render.md.MarkInfo
411-
function M.table_delimiter(row, sections, suffix)
421+
function M.table_delimiter(row, sections, suffix, offset)
412422
local parts = vim.tbl_map(function(width_or_widths)
413423
local widths = vim.islist(width_or_widths) and width_or_widths or { width_or_widths }
414424
local section = vim.tbl_map(function(width)
@@ -417,10 +427,14 @@ function M.table_delimiter(row, sections, suffix)
417427
return table.concat(section, '')
418428
end, sections)
419429
local value = '' .. table.concat(parts, '') .. '' .. string.rep(' ', suffix or 0)
430+
local col = vim.fn.strdisplaywidth(value)
431+
if offset ~= nil then
432+
col = col - offset
433+
end
420434
---@type render.md.MarkInfo
421435
return {
422436
row = { row, row },
423-
col = { 0, vim.fn.strdisplaywidth(value) },
437+
col = { 0, col },
424438
virt_text = { { value, M.hl('TableHead') } },
425439
virt_text_pos = 'overlay',
426440
}

0 commit comments

Comments
 (0)