Implement todo toggle.

This commit is contained in:
CronyAkatsuki 2023-12-28 14:20:46 +01:00
parent 5d68442624
commit 5be431132e
2 changed files with 54 additions and 1 deletions

View File

@ -29,6 +29,16 @@ vim.keymap.set("n", "mp", "<cmd>HeaderPromote<cr>", { desc = "Promote markdown h
You can also just use something like lazy.nvim and load the plugin only on markdown and have this be set in config. But that will leave this binding on even in a non markdown file tho.
### Toggle to do items
Very simple function that toggles existing todo item's. It doesn't go out of it's way to create new todo item's or anythin like that, but just toggles existing toggle items. It also doesn't go out of it's way to toggle the paren't todo ( it's just a simple jump people ).
> example mapping
```lua
vim.keymap.set("n", "mt", "<cmd>ToDoToggle<cr>", { desc = "Toggle Existing Todo Item" })
```
## Small feature set
I plan for this plugin to be very small and only have a small amount of feature's because there are already a lot of really good neovim plugins for writting markdown but some of them are really big and do a lot of stuff you might don't want them to do.

View File

@ -1,5 +1,32 @@
local M = {}
local header_regex = "^(#+) (.+)"
local todo_symbols = { " ", "-", "X", "D" }
local table_lenght = function(T)
local count = 0
for _ in pairs(T) do
count = count + 1
end
return count
end
local get_todo = function(line)
local todo = nil
local index = nil
for n, v in ipairs(todo_symbols) do
if v == "-" then
v = "%-"
end
local ul = "^%s*[+*-]%s+%[" .. v .. "%]%s+"
local ol = "^%s*%d+%.%s+%[" .. v .. "%]%s+"
if line:match(ul, nil) or line:match(ol, nil) then
todo = v
index = n
end
end
return todo, index
end
local M = {}
M.headerControl = function(action)
local current_line = vim.api.nvim_get_current_line()
@ -22,4 +49,20 @@ M.headerControl = function(action)
end
end
M.toggleTodo = function()
local current_line = vim.api.nvim_get_current_line()
local row = vim.api.nvim_win_get_cursor(0)[1]
local todo, index = get_todo(current_line)
if todo then
local symbol = todo_symbols[index + 1]
-- make sure symbol is set to first character if index is too big
if index == table_lenght(todo_symbols) then
symbol = todo_symbols[1]
end
local first, last = current_line:find("%[" .. todo .. "%]")
vim.api.nvim_buf_set_text(0, row - 1, first, row - 1, last - 1, { symbol })
end
end
return M