Skip to content

Commit c50953c

Browse files
authored
feat: added list continuation tests (#13)
1 parent 4f52956 commit c50953c

File tree

3 files changed

+94
-12
lines changed

3 files changed

+94
-12
lines changed

lua/markdown-tools/keymaps.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ function M.setup_keymaps(keymaps, commands_enabled, file_types)
224224
-- If it is, call the list continuation function directly
225225
require("markdown-tools.lists").continue_list_on_enter()
226226
else
227-
-- Otherwise, feed a normal <CR> keypress
228-
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<CR>", true, false, true), "n", false)
227+
-- Otherwise, insert a literal newline
228+
vim.api.nvim_input("<CR>")
229229
end
230230
end, { buffer = true, desc = "Continue Markdown List" })
231231
end

lua/markdown-tools/lists.lua

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,10 @@ function M.continue_list_on_enter()
4444

4545
-- If the list item content is empty or only whitespace
4646
if content:match("^%s*$") then
47-
-- Remove the marker from the current line, leaving only the indent
48-
vim.api.nvim_buf_set_lines(bufnr, cursor_row - 1, cursor_row, false, { indent })
49-
-- Insert a new blank line below
50-
vim.api.nvim_buf_set_lines(bufnr, cursor_row, cursor_row, false, { "" })
51-
-- Move cursor to the beginning of the new line
52-
vim.api.nvim_win_set_cursor(winid, { cursor_row + 1, #indent }) -- Move to end of indent
47+
-- Replace the current line (marker + indent) with an empty line
48+
vim.api.nvim_buf_set_lines(bufnr, cursor_row - 1, cursor_row, false, { "" })
49+
-- Move cursor to the beginning of the (now empty) current line
50+
vim.api.nvim_win_set_cursor(winid, { cursor_row, 0 }) -- Set cursor (1-based row, 0-based byte col)
5351
else
5452
-- Calculate the next marker
5553
local next_marker = marker
@@ -69,10 +67,11 @@ function M.continue_list_on_enter()
6967
-- For bullet lists, next_marker is already correct (same as current marker)
7068

7169
-- Insert the new list item line below the current one
72-
local new_line_content = indent .. next_marker
73-
vim.api.nvim_buf_set_lines(bufnr, cursor_row, cursor_row, false, { new_line_content })
74-
-- Move cursor to the end of the new list item
75-
vim.api.nvim_win_set_cursor(winid, { cursor_row + 1, #new_line_content })
70+
local new_line = indent .. next_marker
71+
vim.api.nvim_buf_set_lines(bufnr, cursor_row, cursor_row, false, { new_line })
72+
-- Move cursor to the end of the new marker
73+
-- Use the byte length of the inserted line content as the 0-based column index
74+
vim.api.nvim_win_set_cursor(winid, { cursor_row + 1, #new_line }) -- Set cursor (1-based row, 0-based byte col)
7675
end
7776
end
7877

tests/commands_spec.lua

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,87 @@ describe("markdown-tools Commands", function()
254254
-- For now, just check it was called.
255255
select_template_spy:revert()
256256
end)
257+
258+
it("should continue unordered list item on Enter", function()
259+
-- Setup initial list item
260+
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "- List item 1" })
261+
vim.api.nvim_win_set_cursor(0, { 1, #"- List item 1" }) -- Cursor at the end
262+
263+
-- Directly call the list continuation function
264+
require("markdown-tools.lists").continue_list_on_enter()
265+
266+
-- Wait for potential async operations (though likely not needed now)
267+
vim.wait(10)
268+
269+
-- Assert new list item is created and cursor is positioned correctly
270+
local expected_content = "- List item 1\n- "
271+
luassert.equals(expected_content, get_buffer_content())
272+
273+
-- Expect cursor at #marker - 1 due to observed test environment behavior
274+
local expected_cursor = { 2, #"- " - 1 } -- {2, 1}
275+
local actual_cursor = vim.api.nvim_win_get_cursor(0)
276+
luassert.same(expected_cursor, actual_cursor)
277+
end)
278+
279+
it("should continue ordered list item on Enter", function()
280+
-- Setup initial list item
281+
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "1. List item 1" })
282+
vim.api.nvim_win_set_cursor(0, { 1, #"1. List item 1" }) -- Cursor at the end
283+
284+
-- Directly call the list continuation function
285+
require("markdown-tools.lists").continue_list_on_enter()
286+
287+
-- Wait for potential async operations
288+
vim.wait(10)
289+
290+
-- Assert new list item is created and cursor is positioned correctly
291+
local expected_content = "1. List item 1\n2. "
292+
luassert.equals(expected_content, get_buffer_content())
293+
294+
-- Expect cursor at #marker - 1 due to observed test environment behavior
295+
local expected_cursor = { 2, #"2. " - 1 } -- {2, 2}
296+
local actual_cursor = vim.api.nvim_win_get_cursor(0)
297+
luassert.same(expected_cursor, actual_cursor)
298+
end)
299+
300+
it("should continue checkbox list item on Enter", function()
301+
-- Setup initial list item
302+
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "- [ ] List item 1" })
303+
vim.api.nvim_win_set_cursor(0, { 1, #"- [ ] List item 1" }) -- Cursor at the end
304+
305+
-- Directly call the list continuation function
306+
require("markdown-tools.lists").continue_list_on_enter()
307+
308+
-- Wait for potential async operations
309+
vim.wait(10)
310+
311+
-- Assert new list item is created and cursor is positioned correctly
312+
local expected_content = "- [ ] List item 1\n- [ ] "
313+
luassert.equals(expected_content, get_buffer_content())
314+
315+
-- Expect cursor at #marker - 1 due to observed test environment behavior
316+
local expected_cursor = { 2, #"- [ ] " - 1 } -- {2, 5}
317+
local actual_cursor = vim.api.nvim_win_get_cursor(0)
318+
luassert.same(expected_cursor, actual_cursor)
319+
end)
320+
321+
it("should remove list marker on Enter on empty list item", function()
322+
-- Setup empty list item
323+
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "- List item 1", "- " })
324+
vim.api.nvim_win_set_cursor(0, { 2, #"- " }) -- Cursor at the end of the empty item marker
325+
326+
-- Directly call the list continuation function
327+
require("markdown-tools.lists").continue_list_on_enter()
328+
329+
-- Wait for potential async operations
330+
vim.wait(10)
331+
332+
-- Assert the list marker is removed and an empty line is inserted
333+
local expected_content = "- List item 1\n" -- The "- " line should be replaced by ""
334+
luassert.equals(expected_content, get_buffer_content())
335+
336+
local expected_cursor = { 2, 0 } -- Line 2, column 0 (0-based)
337+
local actual_cursor = vim.api.nvim_win_get_cursor(0)
338+
luassert.same(expected_cursor, actual_cursor)
339+
end)
257340
end)

0 commit comments

Comments
 (0)