From ee2383daedd6b8df829016d143b706df413b4d9f Mon Sep 17 00:00:00 2001 From: Alexandre Doucet Date: Thu, 2 Jan 2025 21:43:10 -0400 Subject: [PATCH 1/4] Improved the code to use treesitter tools to detect the list maker and intert it into the autocomplete suggestion. --- lua/render-markdown/integ/source.lua | 37 ++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/lua/render-markdown/integ/source.lua b/lua/render-markdown/integ/source.lua index b4d31ee7..2eaca30a 100644 --- a/lua/render-markdown/integ/source.lua +++ b/lua/render-markdown/integ/source.lua @@ -11,6 +11,37 @@ local list_markers = { ---@class render.md.Source local M = {} +---@private +---@param row integer +---@param node TSNode +---@return string +function M.list_prefix(row, node) + local marker = node:named_child(0) + if not marker then + return '' + end + + local marker_row = marker:range() + if marker_row == row then + return '' -- Don't run if on the same line, entry should already have a list marker. + end + + local start_row = select(1, marker:range()) + -- Retrieve the line from the buffer + local marker_line = vim.api.nvim_buf_get_lines(0, start_row, start_row + 1, false)[1] + + if not marker_line or #marker_line == 0 then + return '' -- Return empty if the line is empty or doesn't exist + end + + -- Define valid list markers + local valid_markers = { ['-'] = true, ['+'] = true, ['*'] = true } + local first_char = marker_line:match('%S') + + -- Return corresponding marker if valid, otherwise default to empty string + return valid_markers[first_char] and first_char .. ' ' or '' +end + ---@return boolean function M.enabled() return manager.is_attached(util.current('buf')) @@ -38,9 +69,11 @@ function M.items(buf, row, col) table.insert(items, M.item(component.raw, component.rendered, nil)) end elseif node:type() == 'list_item' or vim.tbl_contains(list_markers, node:type()) then + local last_marker = M.list_prefix(row, node) + print(last_marker) local checkbox = config.checkbox - table.insert(items, M.item('[ ]', checkbox.unchecked.icon, 'unchecked')) - table.insert(items, M.item('[x]', checkbox.checked.icon, 'checked')) + table.insert(items, M.item(last_marker .. '[ ] ', checkbox.unchecked.icon, 'unchecked')) + table.insert(items, M.item(last_marker .. '[x] ', checkbox.checked.icon, 'checked')) for name, component in pairs(checkbox.custom) do table.insert(items, M.item(component.raw, component.rendered, name)) end From 0cdaf0c06c311dbe12e1000bdc012d427a4e301d Mon Sep 17 00:00:00 2001 From: Alexandre Doucet Date: Fri, 3 Jan 2025 16:22:58 -0400 Subject: [PATCH 2/4] - Removed debug print statement - Added the same auto complete behaviour to the custom checkboxes. --- lua/render-markdown/integ/source.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lua/render-markdown/integ/source.lua b/lua/render-markdown/integ/source.lua index 2eaca30a..e698aa9d 100644 --- a/lua/render-markdown/integ/source.lua +++ b/lua/render-markdown/integ/source.lua @@ -70,12 +70,11 @@ function M.items(buf, row, col) end elseif node:type() == 'list_item' or vim.tbl_contains(list_markers, node:type()) then local last_marker = M.list_prefix(row, node) - print(last_marker) local checkbox = config.checkbox table.insert(items, M.item(last_marker .. '[ ] ', checkbox.unchecked.icon, 'unchecked')) table.insert(items, M.item(last_marker .. '[x] ', checkbox.checked.icon, 'checked')) for name, component in pairs(checkbox.custom) do - table.insert(items, M.item(component.raw, component.rendered, name)) + table.insert(items, M.item(list_prefix .. component.raw .. ' ', component.rendered, name)) end end return items From 11dd03e40dce0d019afc2ece83890cda0ce72ec2 Mon Sep 17 00:00:00 2001 From: Alexandre Doucet Date: Fri, 3 Jan 2025 21:10:54 -0400 Subject: [PATCH 3/4] Renamed variable for clarity (last_marker -> list_prefix) --- lua/render-markdown/integ/source.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lua/render-markdown/integ/source.lua b/lua/render-markdown/integ/source.lua index e698aa9d..a83ba293 100644 --- a/lua/render-markdown/integ/source.lua +++ b/lua/render-markdown/integ/source.lua @@ -69,10 +69,10 @@ function M.items(buf, row, col) table.insert(items, M.item(component.raw, component.rendered, nil)) end elseif node:type() == 'list_item' or vim.tbl_contains(list_markers, node:type()) then - local last_marker = M.list_prefix(row, node) + local list_prefix = M.list_prefix(row, node) local checkbox = config.checkbox - table.insert(items, M.item(last_marker .. '[ ] ', checkbox.unchecked.icon, 'unchecked')) - table.insert(items, M.item(last_marker .. '[x] ', checkbox.checked.icon, 'checked')) + table.insert(items, M.item(list_prefix .. '[ ] ', checkbox.unchecked.icon, 'unchecked')) + table.insert(items, M.item(list_prefix .. '[x] ', checkbox.checked.icon, 'checked')) for name, component in pairs(checkbox.custom) do table.insert(items, M.item(list_prefix .. component.raw .. ' ', component.rendered, name)) end From b0181b2df790b043a07c7a11a9d4854efcd19b59 Mon Sep 17 00:00:00 2001 From: Alexandre Doucet Date: Fri, 3 Jan 2025 21:43:38 -0400 Subject: [PATCH 4/4] Removed duplicte variable using :range() call --- lua/render-markdown/integ/source.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lua/render-markdown/integ/source.lua b/lua/render-markdown/integ/source.lua index a83ba293..792af832 100644 --- a/lua/render-markdown/integ/source.lua +++ b/lua/render-markdown/integ/source.lua @@ -26,9 +26,8 @@ function M.list_prefix(row, node) return '' -- Don't run if on the same line, entry should already have a list marker. end - local start_row = select(1, marker:range()) -- Retrieve the line from the buffer - local marker_line = vim.api.nvim_buf_get_lines(0, start_row, start_row + 1, false)[1] + local marker_line = vim.api.nvim_buf_get_lines(0, marker_row, marker_row + 1, false)[1] if not marker_line or #marker_line == 0 then return '' -- Return empty if the line is empty or doesn't exist