Skip to content

Commit cbabe00

Browse files
Checkbox list completion improvements (#277)
* Improved the code to use treesitter tools to detect the list maker and intert it into the autocomplete suggestion. * - Removed debug print statement - Added the same auto complete behaviour to the custom checkboxes. * Renamed variable for clarity (last_marker -> list_prefix) * Removed duplicte variable using :range() call
1 parent 4d8e991 commit cbabe00

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

Diff for: lua/render-markdown/integ/source.lua

+34-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,36 @@ local list_markers = {
1111
---@class render.md.Source
1212
local M = {}
1313

14+
---@private
15+
---@param row integer
16+
---@param node TSNode
17+
---@return string
18+
function M.list_prefix(row, node)
19+
local marker = node:named_child(0)
20+
if not marker then
21+
return ''
22+
end
23+
24+
local marker_row = marker:range()
25+
if marker_row == row then
26+
return '' -- Don't run if on the same line, entry should already have a list marker.
27+
end
28+
29+
-- Retrieve the line from the buffer
30+
local marker_line = vim.api.nvim_buf_get_lines(0, marker_row, marker_row + 1, false)[1]
31+
32+
if not marker_line or #marker_line == 0 then
33+
return '' -- Return empty if the line is empty or doesn't exist
34+
end
35+
36+
-- Define valid list markers
37+
local valid_markers = { ['-'] = true, ['+'] = true, ['*'] = true }
38+
local first_char = marker_line:match('%S')
39+
40+
-- Return corresponding marker if valid, otherwise default to empty string
41+
return valid_markers[first_char] and first_char .. ' ' or ''
42+
end
43+
1444
---@return boolean
1545
function M.enabled()
1646
return manager.is_attached(util.current('buf'))
@@ -38,11 +68,12 @@ function M.items(buf, row, col)
3868
table.insert(items, M.item(component.raw, component.rendered, nil))
3969
end
4070
elseif node:type() == 'list_item' or vim.tbl_contains(list_markers, node:type()) then
71+
local list_prefix = M.list_prefix(row, node)
4172
local checkbox = config.checkbox
42-
table.insert(items, M.item('[ ]', checkbox.unchecked.icon, 'unchecked'))
43-
table.insert(items, M.item('[x]', checkbox.checked.icon, 'checked'))
73+
table.insert(items, M.item(list_prefix .. '[ ] ', checkbox.unchecked.icon, 'unchecked'))
74+
table.insert(items, M.item(list_prefix .. '[x] ', checkbox.checked.icon, 'checked'))
4475
for name, component in pairs(checkbox.custom) do
45-
table.insert(items, M.item(component.raw, component.rendered, name))
76+
table.insert(items, M.item(list_prefix .. component.raw .. ' ', component.rendered, name))
4677
end
4778
end
4879
return items

0 commit comments

Comments
 (0)