Skip to content

Commit e38795f

Browse files
Do not render bullet points before checkboxes
# Details Resolves: #18 When the next sibling of a list marker is a bullet point overlay with spaces rather than spaces + bullet point. We could also do a conceal here to have the text shift over rather than adding spaces, however this has the downside of a conceal where once the cursor enters the line the text jumps back over and the marker becomes visible again. I prefer to avoid the text jumping behavior.
1 parent fbd4c45 commit e38795f

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

demo/demo.gif

11.9 KB
Loading

lua/render-markdown/handler/markdown.lua

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,21 @@ M.render = function(namespace, root)
5050
hl_eol = true,
5151
})
5252
elseif capture == 'list_marker' then
53-
-- List markers from tree-sitter should have leading spaces removed, however there are known
54-
-- edge cases in the parser: https://github.com/tree-sitter-grammars/tree-sitter-markdown/issues/127
55-
-- As a result we handle leading spaces here, can remove if this gets fixed upstream
56-
local _, leading_spaces = value:find('^%s*')
57-
local level = M.calculate_list_level(node)
58-
local bullet = list.cycle(state.config.bullets, level)
53+
local list_marker_overlay = ''
54+
if M.is_sibling_checkbox(node) then
55+
-- Hide the list marker for checkboxes rather than replacing with a bullet point
56+
list_marker_overlay = string.rep(' ', #value)
57+
else
58+
-- List markers from tree-sitter should have leading spaces removed, however there are known
59+
-- edge cases in the parser: https://github.com/tree-sitter-grammars/tree-sitter-markdown/issues/127
60+
-- As a result we handle leading spaces here, can remove if this gets fixed upstream
61+
local _, leading_spaces = value:find('^%s*')
62+
local level = M.calculate_list_level(node)
63+
local bullet = list.cycle(state.config.bullets, level)
64+
list_marker_overlay = string.rep(' ', leading_spaces or 0) .. bullet
65+
end
5966

60-
local virt_text = { string.rep(' ', leading_spaces or 0) .. bullet, highlights.bullet }
67+
local virt_text = { list_marker_overlay, highlights.bullet }
6168
vim.api.nvim_buf_set_extmark(0, namespace, start_row, start_col, {
6269
end_row = end_row,
6370
end_col = end_col,
@@ -144,6 +151,16 @@ M.render = function(namespace, root)
144151
end
145152
end
146153

154+
---@param node TSNode
155+
---@return boolean
156+
M.is_sibling_checkbox = function(node)
157+
local sibling = node:next_sibling()
158+
if sibling == nil then
159+
return false
160+
end
161+
return vim.startswith(sibling:type(), 'task_list_marker')
162+
end
163+
147164
--- Walk through all parent nodes and count the number of nodes with type list
148165
--- to calculate the level of the given node
149166
---@param node TSNode

tests/init_spec.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ async_tests.describe('init', function()
155155

156156
-- Checkboxes
157157
vim.list_extend(expected, {
158-
-- Unchecked, bullet point, not created intentionally, remove if fixed
158+
-- Unchecked, list marker
159159
{
160160
row = { 35, 35 },
161161
col = { 0, 2 },
162-
virt_text = { { '', 'Normal' } },
162+
virt_text = { { ' ', 'Normal' } },
163163
virt_text_pos = 'overlay',
164164
},
165165
-- Unchecked, checkbox
@@ -169,11 +169,11 @@ async_tests.describe('init', function()
169169
virt_text = { { ' 󰄱 ', '@markup.list.unchecked' } },
170170
virt_text_pos = 'overlay',
171171
},
172-
-- Checked, bullet point, not created intentionally, remove if fixed
172+
-- Checked, list marker
173173
{
174174
row = { 36, 36 },
175175
col = { 0, 2 },
176-
virt_text = { { '', 'Normal' } },
176+
virt_text = { { ' ', 'Normal' } },
177177
virt_text_pos = 'overlay',
178178
},
179179
-- Checked, checkbox

0 commit comments

Comments
 (0)