Skip to content

Commit adecead

Browse files
authored
Fix/continuation (#18)
* fix: enter was broken on non-bullet lines, now fixed * feat: fixed tests and improved some commands
1 parent 5c5afa9 commit adecead

File tree

5 files changed

+162
-154
lines changed

5 files changed

+162
-154
lines changed

lua/markdown-tools/commands.lua

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,6 @@ function M.insert_code_block(opts)
269269
new_text_lines
270270
)
271271
end
272-
273-
-- Exit visual mode only if actually in visual mode
274-
if mode:match("^[vV]") then
275-
vim.cmd("normal! <Esc>")
276-
end
277272
else
278273
-- Normal mode insertion
279274
vim.api.nvim_put({ opening, "", closing }, "l", true, true)
@@ -511,19 +506,19 @@ function M.insert_checkbox()
511506
local trimmed_line = current_line_content:match("^%s*(.-)%s*$") or ""
512507

513508
local checkbox = "- [ ] "
514-
local checkbox_len_chars = vim.fn.strchars(checkbox)
515509

516510
if trimmed_line == "" then
517511
-- Line is empty or only whitespace, replace it and stay in insert mode
518512
vim.api.nvim_buf_set_lines(0, cursor_row - 1, cursor_row, false, { checkbox })
519-
-- Use feedkeys 'a' to enter insert mode at the end of the line
520-
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("a", true, false, true), "n", false)
513+
-- Use feedkeys 'A' to enter insert mode at the end of the line
514+
vim.api.nvim_feedkeys("A", "n", true)
521515
else
516+
local cursor_position = vim.api.nvim_win_get_cursor(0)
522517
-- Line has content, insert checkbox at the beginning and return to normal mode
523518
local new_line = checkbox .. current_line_content
524519
vim.api.nvim_buf_set_lines(0, cursor_row - 1, cursor_row, false, { new_line })
525520
-- Set cursor position using 0-based index
526-
vim.api.nvim_win_set_cursor(0, { cursor_row, checkbox_len_chars }) -- Move cursor after checkbox
521+
vim.api.nvim_win_set_cursor(0, { cursor_position[1], cursor_position[2] + vim.fn.strchars(checkbox) })
527522
-- Ensure Normal mode by sending Esc
528523
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, false, true), "n", false)
529524
end

lua/markdown-tools/keymaps.lua

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ function M.setup_keymaps(keymaps, commands_enabled, file_types)
6161
mode = "v", -- Visual mode only
6262
key = keymaps.insert_code_block,
6363
cmd = function()
64-
-- Still need Lua for prompt, exit visual first
65-
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, false, true), "n", false)
6664
require("markdown-tools.commands").insert_code_block({ range = 2 })
6765
end,
6866
desc = "Code block (Visual)",
@@ -155,25 +153,14 @@ function M.setup_keymaps(keymaps, commands_enabled, file_types)
155153
},
156154
{
157155
command_key = "insert_checkbox",
158-
mode = "n", -- Normal mode only
159-
key = keymaps.insert_checkbox,
160-
cmd = function()
161-
require("markdown-tools.commands").insert_checkbox()
162-
end,
163-
desc = "Checkbox (Normal)",
164-
},
165-
{
166-
command_key = "insert_checkbox",
167-
mode = "v", -- Visual mode only
156+
mode = { "n", "v" },
168157
key = keymaps.insert_checkbox,
169-
-- Call the Lua function directly, which handles line insertion
170158
cmd = function()
171-
-- Exit visual mode before calling the command
159+
-- Esc first, in case in visual mode.
172160
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, false, true), "n", false)
173161
require("markdown-tools.commands").insert_checkbox()
174162
end,
175-
desc = "Checkbox", -- Updated description
176-
opts = nil, -- Remove remap = true, no longer needed
163+
desc = "Checkbox",
177164
},
178165
{
179166
command_key = "toggle_checkbox", -- Renamed key
@@ -207,30 +194,12 @@ function M.setup_keymaps(keymaps, commands_enabled, file_types)
207194

208195
-- Add keymap for continuing lists on Enter if enabled (buffer-local)
209196
if require("markdown-tools.config").options.continue_lists_on_enter then
210-
vim.keymap.set("i", "<CR>", function()
211-
local line = vim.api.nvim_get_current_line()
212-
local _, cursor_col = unpack(vim.api.nvim_win_get_cursor(0))
213-
214-
-- Define separate patterns for each list type
215-
local pattern_bullet = "^%s*[-*+]%s+"
216-
local pattern_numbered = "^%s*%d+%.%s+"
217-
local pattern_checkbox = "^%s*[-*+] %[[ x]%]%s+"
218-
219-
-- Check if cursor is at end and any pattern matched
220-
local is_list_end = cursor_col == #line
221-
and (line:match(pattern_bullet) or line:match(pattern_numbered) or line:match(pattern_checkbox))
222-
223-
if is_list_end then
224-
-- If it is, schedule the list continuation function to run soon
225-
vim.schedule(function()
226-
require("markdown-tools.lists").continue_list_on_enter()
227-
end)
228-
return "" -- Handled: tell Neovim to do nothing further for this <CR>
229-
else
230-
-- Otherwise, let Neovim handle <CR> as default
231-
return vim.api.nvim_replace_termcodes("<CR>", true, true, true)
232-
end
233-
end, { buffer = true, desc = "Continue Markdown List", expr = true })
197+
vim.keymap.set(
198+
"i",
199+
"<CR>",
200+
require("markdown-tools.lists").keymap_init,
201+
{ buffer = true, desc = "Continue Markdown List", expr = true }
202+
)
234203
end
235204
end,
236205
})

lua/markdown-tools/lists.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
11
---@mod markdown-tools.lists List handling utilities
22
local M = {}
33

4+
function M.keymap_init()
5+
local line = vim.api.nvim_get_current_line()
6+
local _, cursor_col = unpack(vim.api.nvim_win_get_cursor(0))
7+
8+
-- Define separate patterns for each list type
9+
local pattern_bullet = "^%s*[-*+]%s+"
10+
local pattern_numbered = "^%s*%d+%.%s+"
11+
local pattern_checkbox = "^%s*[-*+] %[[ x]%]%s+"
12+
13+
-- Check if cursor is at end and any pattern matched
14+
local is_list_end = cursor_col == #line
15+
and (line:match(pattern_bullet) or line:match(pattern_numbered) or line:match(pattern_checkbox))
16+
17+
if is_list_end then
18+
-- If it is, schedule the list continuation function to run soon
19+
vim.schedule(function()
20+
require("markdown-tools.lists").continue_list_on_enter()
21+
end)
22+
return "" -- Handled: tell Neovim to do nothing further for this <CR>
23+
else
24+
-- Otherwise, let Neovim handle <CR> as default
25+
-- return vim.api.nvim_replace_termcodes("<CR>", true, true, true)
26+
return vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<CR>", true, true, true), "nt", false)
27+
end
28+
end
29+
430
--- Handles the <CR> key press in insert mode for markdown files.
531
-- Continues lists (bullet, numbered, checkbox) automatically.
632
-- If the current list item is empty, hitting Enter removes the list marker and inserts a blank line.

0 commit comments

Comments
 (0)