-
Notifications
You must be signed in to change notification settings - Fork 8
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
Conversation
👍 supporting adding a new line (or just falling back to the default vim mapping) when the commands for adding a list item above/below are used outside of a list would be a nice addition, as would supporting insert mode. The ideal usage for this feature for me is to be able to set mappings to make it work seamlessly during normal text editing. For that, I would map
Another way to support the "what do do when not in a list" scenario without modifying the existing logic for the commands might be to expose a helper |
This gets pretty darned close, based on what I found in the plugin code and this PR. local function is_list_item()
local md_ts = require("markdown.treesitter")
local curr_row = vim.api.nvim_win_get_cursor(0)[1] - 1
local curr_eol = vim.fn.col("$") - 1
return md_ts.find_node(function(node)
return node:type() == "list_item"
end, { pos = { curr_row, curr_eol } })
end
return {
"tadmccorkle/markdown.nvim",
ft = "markdown",
opts = {
on_attach = function(bufnr)
-- New list item below on `o` if in a list
vim.keymap.set("n", "o", function()
if is_list_item() then
vim.cmd("MDListItemBelow")
return
end
vim.api.nvim_feedkeys("o", "n", true)
end, { buffer = bufnr, desc = "Insert list item below" })
-- New list item above on `O` if in a list
vim.keymap.set("n", "O", function()
if is_list_item() then
vim.cmd("MDListItemAbove")
return
end
vim.api.nvim_feedkeys("O", "n", true)
end, { buffer = bufnr, desc = "Insert list item above" })
-- New list item below on `<cr>` if in a list and at the end of the line
vim.keymap.set("i", "<cr>", function()
local is_eol = vim.fn.col(".") == vim.fn.col("$")
local in_list = is_list_item()
if is_eol and in_list then
vim.cmd("MDListItemBelow")
return
end
local key = vim.api.nvim_replace_termcodes("<cr>", true, false, true)
vim.api.nvim_feedkeys(key, "n", false)
end, { buffer = bufnr, desc = "Insert list item below" })
end,
},
}
|
@mmirus Thank you for your advice. Your code is very simple and needless to fix plugin code, so I close this PR. |
Glad that's helpful for you! Still, if there is some reasonable way for the plugin to either directly support this workflow or offer APIs that would make it easier for users to achieve it through mappings, that would be helpful. All up to @tadmccorkle, of course, based on his goals and philosophy for the plugin and how much he wants to support. |
@masa0x80 Sorry for the radio silence on this PR. And thanks @mmirus for the suggestions. Because there are potentially various desired behaviors, the intended use case for the list item feature was to support unique mappings to That said, I've played around with different ways of supporting this workflow based on your suggestions. Here's what I've settled on for now:
Any feedback on what I've outlined above is welcome! |
Thank you, @tadmccorkle! That's great. In case it's helpful for anyone else, in my own config, I extracted the on_attach = function(bufnr)
local function buffer_map(mode, lhs, rhs, desc)
vim.keymap.set(mode, lhs, rhs, { buffer = bufnr, desc = desc, silent = true })
end
local md_list = require("markdown.list")
-- New list item below on `o` if in a list
buffer_map("n", "o", function()
local list_item_inserted = md_list.insert_list_item_below()
if not list_item_inserted then
vim.api.nvim_feedkeys("o", "n", false)
end
end, "Insert list item below")
-- ...
end,
Yeah, that's what I was obliquely referring to when I said "This gets pretty darned close" in my comment above. 😁 I didn't find a solution for it, but so far it doesn't seem like a huge annoyance. Thank you for the plugin update and your comment! This solution feels pretty good. Might take users slightly out of their existing knowledge (feedkeys), which isn't bad, but doesn't require them to figure out how to find list item nodes with treesitter (even using the helper from the plugin). |
It turned out to be pretty annoying. 😂 This seems like it might be an okay workaround? Haven't used it much yet. on_attach = function(bufnr)
local function buffer_map(mode, lhs, rhs, desc)
vim.keymap.set(mode, lhs, rhs, { buffer = bufnr, desc = desc, silent = true })
end
-- ⭐⭐⭐ This is the new bit:
-- Attempt to insert a list item if the current line is not empty
local md_list = require("markdown.list")
local function insert_list_item(direction)
local line_contents = vim.trim(vim.fn.getline("."))
if vim.fn.empty(line_contents) == 1 then
return false
end
return direction == "above" and md_list.insert_list_item_above() or md_list.insert_list_item_below()
end
-- New list item below on `o` if in a list
buffer_map("n", "o", function()
-- ⭐⭐⭐ Example usage:
local list_item_inserted = insert_list_item()
if not list_item_inserted then
vim.api.nvim_feedkeys("o", "n", false)
end
end, "Insert list item below")
-- New list item above on `O` if in a list
buffer_map("n", "O", function()
local list_item_inserted = insert_list_item("above")
if not list_item_inserted then
vim.api.nvim_feedkeys("O", "n", false)
end
end, "Insert list item above")
-- New list item below on `<cr>` if in a list and at the end of the line
buffer_map("i", "<cr>", function()
local is_eol = vim.fn.col(".") == vim.fn.col("$")
local list_item_inserted = is_eol and insert_list_item()
if not list_item_inserted then
local key = vim.api.nvim_replace_termcodes("<cr>", true, false, true)
vim.api.nvim_feedkeys(key, "n", false)
end
end, "Insert list item below")
|
I wanted to map
MDListItemBelow
to in insert mode, so I modified it to work in insert mode as well.Demo
![demo](https://private-user-images.githubusercontent.com/86851/329822777-6015a71c-d80a-439a-8381-7029e52761db.gif?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTkxODY5MjAsIm5iZiI6MTcxOTE4NjYyMCwicGF0aCI6Ii84Njg1MS8zMjk4MjI3NzctNjAxNWE3MWMtZDgwYS00MzlhLTgzODEtNzAyOWU1Mjc2MWRiLmdpZj9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA2MjMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNjIzVDIzNTAyMFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWJhYmQ0NzExZTIwYmIzOTNlMjM1NzU0MjQzNTI2ZjZhOTE5ODA4MGNkYjQ5YzE5ZDE5YTE5YjViNWUyMGVhYmMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.kIO8CsXDOTvOlxgurfXlZPUUo6O5KVHCdMZKt8vFmUc)