Skip to content

Commit 4c823b1

Browse files
fix: background of indented code blocks in lists
## Details Bug report: #133 For most code blocks we assumed a starting column of 0. This let us handle multiline blocks with a single extmark. However for indented code blocks this leads to jutting out background sections which do not line up with the rest of the highlights. To fix this apply background highlight for code blocks one line at a time using the input starting column value.
1 parent 7986be4 commit 4c823b1

File tree

7 files changed

+35
-29
lines changed

7 files changed

+35
-29
lines changed

Diff for: benches/readme_spec.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local util = require('benches.util')
44

55
describe('README.md', function()
66
it('default', function()
7-
local base_marks = 45
7+
local base_marks = 50
88
util.less_than(util.setup('README.md'), 50)
99
util.num_marks(base_marks)
1010

Diff for: 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: 2024 August 14
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 August 18
22

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

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

+10-12
Original file line numberDiff line numberDiff line change
@@ -350,18 +350,16 @@ function Handler:code_background(code_block, icon_added)
350350
end
351351
end
352352

353-
self:add(false, code_block.start_row, 0, {
354-
end_row = code_block.end_row,
355-
end_col = 0,
356-
hl_group = code.highlight,
357-
hl_eol = true,
358-
})
359-
360-
if code.width == 'block' then
361-
-- Overwrite anything beyond width with Normal
362-
local padding = str.pad(vim.o.columns * 2)
363-
for row = code_block.start_row, code_block.end_row - 1 do
364-
self:add(false, row, 0, {
353+
local padding = str.pad(vim.o.columns * 2)
354+
for row = code_block.start_row, code_block.end_row - 1 do
355+
self:add(false, row, code_block.col, {
356+
end_row = row + 1,
357+
hl_group = code.highlight,
358+
hl_eol = true,
359+
})
360+
if code.width == 'block' then
361+
-- Overwrite anything beyond width with Normal
362+
self:add(false, row, code_block.col, {
365363
priority = 0,
366364
virt_text = { { padding, 'Normal' } },
367365
virt_text_win_col = code_block.width,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local M = {}
55

66
---@private
77
---@type string
8-
M.version = '6.1.4'
8+
M.version = '6.1.5'
99

1010
function M.check()
1111
vim.health.start('render-markdown.nvim [version]')

Diff for: tests/callout_spec.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ describe('callout.md', function()
5050
callout(tip_start + 2, 2, 8, '󰌶 Tip', ok), -- Callout text
5151
util.quote(tip_start + 3, '%s', ok), -- Quote continued
5252
util.quote(tip_start + 4, '%s ', ok),
53-
util.code_block(tip_start + 4, tip_start + 6),
53+
util.code_block_row(tip_start + 4, 2),
5454
})
55-
vim.list_extend(expected, util.code_language(tip_start + 4, 5, 8, '󰢱 ', 'lua', 'MiniIconsAzure'))
55+
vim.list_extend(expected, util.code_language(tip_start + 4, 5, 8, 'lua'))
5656
vim.list_extend(expected, {
5757
util.quote(tip_start + 5, '%s ', ok),
58+
util.code_block_row(tip_start + 5, 2),
5859
util.quote(tip_start + 6, '%s ', ok),
5960
util.code_below(tip_start + 6, 2),
6061
})

Diff for: tests/heading_code_spec.lua

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ describe('heading_code.md', function()
1717

1818
vim.list_extend(expected, {
1919
util.link(10, 0, 21, 'image'), -- Image link
20-
util.code_block(12, 22), -- Code block
20+
util.code_block_row(12, 0), -- Code block start
2121
})
22-
vim.list_extend(expected, util.code_language(12, 3, 9, '󰌠 ', 'python', 'MiniIconsYellow'))
23-
vim.list_extend(expected, { util.code_below(22, 0) })
22+
vim.list_extend(expected, util.code_language(12, 3, 9, 'python'))
23+
for i = 13, 21 do
24+
table.insert(expected, util.code_block_row(i, 0))
25+
end
26+
table.insert(expected, util.code_below(22, 0))
2427

2528
local actual = util.get_actual_marks()
2629
util.marks_are_equal(expected, actual)

Diff for: tests/util.lua

+13-9
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ function M.inline_code(row, start_col, end_col)
7676
}
7777
end
7878

79-
---@param start_row integer
80-
---@param end_row integer
79+
---@param row integer
80+
---@param col integer
8181
---@return render.md.MarkInfo
82-
function M.code_block(start_row, end_row)
82+
function M.code_block_row(row, col)
8383
---@type render.md.MarkInfo
8484
return {
85-
row = { start_row, end_row },
86-
col = { 0, 0 },
85+
row = { row, row + 1 },
86+
col = { col, 0 },
8787
hl_eol = true,
8888
hl_group = M.hl('Code'),
8989
}
@@ -92,11 +92,15 @@ end
9292
---@param row integer
9393
---@param start_col integer
9494
---@param end_col integer
95-
---@param icon string
96-
---@param name string
97-
---@param highlight string
95+
---@param name 'python'|'lua'
9896
---@return render.md.MarkInfo[]
99-
function M.code_language(row, start_col, end_col, icon, name, highlight)
97+
function M.code_language(row, start_col, end_col, name)
98+
local icon, highlight
99+
if name == 'python' then
100+
icon, highlight = '󰌠 ', 'MiniIconsYellow'
101+
elseif name == 'lua' then
102+
icon, highlight = '󰢱 ', 'MiniIconsAzure'
103+
end
100104
---@type render.md.MarkInfo
101105
local sign_mark = {
102106
row = { row, row },

0 commit comments

Comments
 (0)