Skip to content

Commit 48083f8

Browse files
bugfix: Account for leading spaces in code blocks
# Details More info here: #60 When handling code blocks with leading spaces that lanuage gets left shifted. When this is combined with a plugin that adds information to leading spaces, such as indent blank lines, parts of the icon / language can be overlayed and therefore hidden. To fix this account for the leading spaces before placing icon above code blocks.
1 parent 1d72b63 commit 48083f8

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,20 @@ M.render_node = function(namespace, buf, capture, node)
103103
if not util.has_10 then
104104
return
105105
end
106+
-- Fenced code blocks will pick up varying amounts of leading white space depending on
107+
-- the context they are in. This gets lumped into the delimiter node and as a result,
108+
-- after concealing, the extmark will be left shifted. Logic below accounts for this.
109+
local padding = 0
110+
local code_block = ts.parent_in_section(info.node, 'fenced_code_block')
111+
if code_block ~= nil then
112+
padding = str.leading_spaces(ts.info(code_block, buf).text)
113+
end
106114
local highlight = { icon_highlight }
107115
if code.style == 'full' then
108116
highlight = { icon_highlight, code.highlight }
109117
end
110118
vim.api.nvim_buf_set_extmark(buf, namespace, info.start_row, info.start_col, {
111-
virt_text = { { icon .. ' ' .. info.text, highlight } },
119+
virt_text = { { str.pad(icon .. ' ' .. info.text, padding), highlight } },
112120
virt_text_pos = 'inline',
113121
})
114122
elseif capture == 'list_marker' then
@@ -146,7 +154,7 @@ M.render_node = function(namespace, buf, capture, node)
146154
-- List markers from tree-sitter should have leading spaces removed, however there are known
147155
-- edge cases in the parser: https://github.com/tree-sitter-grammars/tree-sitter-markdown/issues/127
148156
-- As a result we handle leading spaces here, can remove if this gets fixed upstream
149-
local _, leading_spaces = info.text:find('^%s*')
157+
local leading_spaces = str.leading_spaces(info.text)
150158
local level = ts.level_in_section(info.node, 'list')
151159
local icon = list.cycle(bullet.icons, level)
152160

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
local M = {}
22

3+
---@param s string
4+
---@return integer
5+
M.leading_spaces = function(s)
6+
local _, leading_spaces = s:find('^%s*')
7+
return leading_spaces or 0
8+
end
9+
310
---@param value string
411
---@param s string
512
---@return string
@@ -9,10 +16,10 @@ M.pad_to = function(value, s)
916
end
1017

1118
---@param s string
12-
---@param padding integer?
19+
---@param padding integer
1320
---@return string
1421
M.pad = function(s, padding)
15-
return string.rep(' ', padding or 0) .. s
22+
return string.rep(' ', padding) .. s
1623
end
1724

1825
return M

0 commit comments

Comments
 (0)