Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/supports insert mode #12

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ on_attach = function(bufnr)
local opts = { buffer = bufnr }
map({ 'n', 'i' }, '<M-l><M-o>', '<Cmd>MDListItemBelow<CR>', opts)
map({ 'n', 'i' }, '<M-L><M-O>', '<Cmd>MDListItemAbove<CR>', opts)
map('i', '<CR>', '<Cmd>MDListItemBelow<CR>', opts)
map('n', '<M-c>', '<Cmd>MDTaskToggle<CR>', opts)
map('x', '<M-c>', ':MDTaskToggle<CR>', opts)
end,
Expand Down
1 change: 1 addition & 0 deletions doc/markdown.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ markdown.setup({opts}) *markdown.setup()*
local opts = { buffer = bufnr }
map({ 'n', 'i' }, '<M-l><M-o>', '<Cmd>MDListItemBelow<CR>', opts)
map({ 'n', 'i' }, '<M-L><M-O>', '<Cmd>MDListItemAbove<CR>', opts)
map('i', '<CR>', '<Cmd>MDListItemBelow<CR>', opts)
map('n', '<M-c>', '<Cmd>MDTaskToggle<CR>', opts)
map('x', '<M-c>', ':MDTaskToggle<CR>', opts)
end,
Expand Down
35 changes: 34 additions & 1 deletion lua/markdown/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,41 @@ local function insert_list_item(loc)

ts.get_parser(0, "markdown"):parse()
local list_item = md_ts.find_node(is_list_item, { pos = { curr_row, curr_eol } })

-- Supports unlisted item.
if list_item == nil then
return
if loc == REL_POS.above then
vim.cmd("startinsert!")
local new_row = curr_row
api.nvim_buf_set_lines(0, new_row, new_row, true, { "" })
new_row = new_row + 1
api.nvim_win_set_cursor(0, { new_row, vim.fn.charcol({ new_row, "$" }) })
return
else
if vim.fn.mode() == "n" then
vim.cmd("normal! o")
vim.cmd("startinsert!")
return
end
end
end

-- Supports insert mode.
local key = vim.api.nvim_replace_termcodes("<CR>", true, false, true)
if vim.fn.mode() == "i" and loc == REL_POS.below then
if list_item == nil then
vim.api.nvim_feedkeys(key, "n", true)
return
elseif list_item ~= nil then
if vim.fn.col("$") == 1 then
vim.cmd("normal! o")
vim.cmd("startinsert!")
return
elseif vim.fn.col(".") < vim.fn.col("$") then
vim.api.nvim_feedkeys(key, "n", true)
return
end
end
end

local marker = list_item:named_child(0)
Expand Down
38 changes: 38 additions & 0 deletions tests/list_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ describe("list", function()
" l4 continued",
" + aa",
"- l7",
"",
"unlisted item",
})
insert_li_above({ 1, 0 }, "l2")
insert_li_below({ 2, 0 }, "l3")
Expand All @@ -115,6 +117,8 @@ describe("list", function()
insert_li_below({ 13, 5 }, "cc")
insert_li_above({ 15, 0 }, "l8")
insert_li_below({ 16, 0 }, "l9")
insert_li_above({ 19, 0 }, "abc")
insert_li_below({ 20, 0 }, "xyz")
assert_buf_eq(bufnr, {
"- l2",
"- l1",
Expand All @@ -133,6 +137,10 @@ describe("list", function()
"- l8",
"- l7",
"- l9",
"",
"abc",
"unlisted item",
"xyz",
})
end)

Expand Down Expand Up @@ -295,4 +303,34 @@ describe("list", function()
})
end)
end)

it("insert mode support", function()
local bufnr = new_md_buf()
set_buf(bufnr, {
"- l1",
"",
"unlisted item",
})
local cr = vim.api.nvim_replace_termcodes("<CR>", true, false, true)
local br = vim.api.nvim_replace_termcodes("<C-h>", true, false, true)
insert_li_below({ 1, 3 }, "l2")
api.nvim_win_set_cursor(0, { 2, 3 })
vim.cmd("normal i" .. cr)
api.nvim_win_set_cursor(0, { 3, 1 })
vim.cmd("normal a" .. br .. cr)
api.nvim_win_set_cursor(0, { 6, 1 })
vim.cmd("normal i" .. cr)
api.nvim_win_set_cursor(0, { 7, 12 })
vim.cmd("normal a" .. cr .. "xyz")
assert_buf_eq(bufnr, {
"- l1",
"- l",
"",
"",
"",
"u",
"nlisted item",
"xyz",
})
end)
end)