Skip to content

Commit d1cec33

Browse files
fix: empty lines in indented code blocks
## Details Follow up to: #133 Empty lines in indented code blocks ignore start_col when adding background highlight, since any value > 0 is out of the valid range. To fix this we need to add an inline mark to shift the starting point based on the overall block start_col. To do this update code block parsing to track empty rows. Update the left padding logic to add more additional padding to these empty lines.
1 parent cd0a5ad commit d1cec33

File tree

7 files changed

+60
-18
lines changed

7 files changed

+60
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
[4c823b1](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/4c823b1df151dbf1ed3ddaacac517be606b1e145)
1717
- do not set noref in vim.deepcopy [#139](https://github.com/MeanderingProgrammer/render-markdown.nvim/pull/139)
1818
- gate virt_text_repeat_linebreak to neovim >= 0.10.0 [98f9965](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/98f996563591b753470942165d2d5134df868529)
19+
- account for folds when computing visible range [#138](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/138)
20+
[cd0a5ad](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/cd0a5ad8c77c3754d02437048bc0bb604a2fe268)
1921

2022
### Collaborator Shoutouts
2123

doc/render-markdown.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 August 18
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 August 19
22

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

lua/render-markdown/handler/markdown.lua

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -369,18 +369,30 @@ end
369369
---@param add_background boolean
370370
function Handler:code_left_pad(code_block, add_background)
371371
local code = self.config.code
372-
if code.left_pad <= 0 then
372+
if (code_block.col == 0 or #code_block.empty_rows == 0) and code.left_pad <= 0 then
373373
return
374374
end
375-
local padding = str.pad(code.left_pad)
376-
local highlight = add_background and code.highlight or 'Normal'
375+
376+
-- Use low priority to include other marks in padding when code block is at edge
377+
local priority = code_block.col == 0 and 0 or nil
378+
local outer_text = { str.pad(code_block.col), 'Normal' }
379+
local left_text = { str.pad(code.left_pad), add_background and code.highlight or 'Normal' }
380+
377381
for row = code_block.start_row, code_block.end_row - 1 do
378-
-- Uses a low priority so other marks are loaded first and included in padding
379-
self:add(false, row, code_block.col, {
380-
priority = 0,
381-
virt_text = { { padding, highlight } },
382-
virt_text_pos = 'inline',
383-
})
382+
local virt_text = {}
383+
if code_block.col > 0 and vim.tbl_contains(code_block.empty_rows, row) then
384+
table.insert(virt_text, outer_text)
385+
end
386+
if code.left_pad > 0 then
387+
table.insert(virt_text, left_text)
388+
end
389+
if #virt_text > 0 then
390+
self:add(false, row, code_block.col, {
391+
priority = priority,
392+
virt_text = virt_text,
393+
virt_text_pos = 'inline',
394+
})
395+
end
384396
end
385397
end
386398

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
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.7'
8+
M.version = '6.1.8'
99

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

lua/render-markdown/parser/code_block.lua

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local M = {}
88
---@field start_row integer
99
---@field end_row integer
1010
---@field leading_spaces integer
11+
---@field empty_rows integer[]
1112
---@field longest_line integer
1213
---@field width integer
1314
---@field code_info_hidden boolean
@@ -25,15 +26,17 @@ function M.parse(config, context, info)
2526
if info.end_row - info.start_row <= 1 then
2627
return nil
2728
end
29+
local widths = vim.tbl_map(str.width, info:lines())
2830
local code_info = info:child('info_string', info.start_row)
2931
local language_info = code_info ~= nil and code_info:child('language', info.start_row) or nil
30-
local longest_line, width = M.get_width(config, context, info)
32+
local longest_line, width = M.get_width(config, context, widths)
3133
---@type render.md.parsed.CodeBlock
3234
return {
3335
col = info.start_col,
3436
start_row = info.start_row,
3537
end_row = info.end_row,
3638
leading_spaces = str.leading_spaces(info.text),
39+
empty_rows = M.get_empty_rows(info.start_row, widths),
3740
longest_line = longest_line,
3841
width = width,
3942
code_info_hidden = context:hidden(code_info),
@@ -44,14 +47,27 @@ function M.parse(config, context, info)
4447
}
4548
end
4649

50+
---@private
51+
---@param start_row integer
52+
---@param widths integer[]
53+
---@return integer[]
54+
function M.get_empty_rows(start_row, widths)
55+
local empty_rows = {}
56+
for row, width in ipairs(widths) do
57+
if width == 0 then
58+
table.insert(empty_rows, start_row + row - 1)
59+
end
60+
end
61+
return empty_rows
62+
end
63+
4764
---@private
4865
---@param config render.md.Code
4966
---@param context render.md.Context
50-
---@param info render.md.NodeInfo
67+
---@param widths integer[]
5168
---@return integer, integer
52-
function M.get_width(config, context, info)
53-
local lines = info:lines()
54-
local code_width = vim.fn.max(vim.tbl_map(str.width, lines))
69+
function M.get_width(config, context, widths)
70+
local code_width = vim.fn.max(widths)
5571
local longest_line = config.left_pad + code_width + config.right_pad
5672
local width = math.max(longest_line, config.min_width)
5773
if config.width == 'block' then

tests/code_spec.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ describe('code.md', function()
2424
util.code_row(11, 2),
2525
util.code_language(11, 5, 8, 'lua'),
2626
util.code_row(12, 2),
27-
-- TODO: look into fixing indented code with blank lines
28-
-- probably need to fill with inline extmark
2927
util.code_row(13, 0),
28+
util.padding(13, 2),
3029
util.code_row(14, 2),
3130
util.code_below(15, 2),
3231
})

tests/util.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,19 @@ function M.quote(row, format, highlight)
185185
}
186186
end
187187

188+
---@param row integer
189+
---@param spaces integer
190+
---@return render.md.MarkInfo
191+
function M.padding(row, spaces)
192+
---@type render.md.MarkInfo
193+
return {
194+
row = { row },
195+
col = { 0 },
196+
virt_text = { { string.rep(' ', spaces), 'Normal' } },
197+
virt_text_pos = 'inline',
198+
}
199+
end
200+
188201
---@param row integer
189202
---@param col integer
190203
---@param head boolean

0 commit comments

Comments
 (0)