mirror of
https://github.com/opdavies/toggle-checkbox.nvim.git
synced 2025-01-22 20:07:32 +00:00
feat: make checkbox first on line without checkbox
This commit is contained in:
parent
856b3d99cd
commit
58f958a2dc
|
@ -3,17 +3,39 @@ local checked_character = "x"
|
||||||
local checked_checkbox = "%[" .. checked_character .. "%]"
|
local checked_checkbox = "%[" .. checked_character .. "%]"
|
||||||
local unchecked_checkbox = "%[ %]"
|
local unchecked_checkbox = "%[ %]"
|
||||||
|
|
||||||
local line_contains_an_unchecked_checkbox = function(line)
|
local line_contains_unchecked = function(line)
|
||||||
return string.find(line, unchecked_checkbox)
|
return line:find(unchecked_checkbox)
|
||||||
|
end
|
||||||
|
|
||||||
|
local line_contains_checked = function(line)
|
||||||
|
return line:find(checked_checkbox)
|
||||||
|
end
|
||||||
|
|
||||||
|
local line_with_checkbox = function(line)
|
||||||
|
-- return not line_contains_a_checked_checkbox(line) and not line_contains_an_unchecked_checkbox(line)
|
||||||
|
return line:find("^%s*- " .. checked_checkbox)
|
||||||
|
or line:find("^%s*- " .. unchecked_checkbox)
|
||||||
|
or line:find("^%s*%d%. " .. checked_checkbox)
|
||||||
|
or line:find("^%s*%d%. " .. unchecked_checkbox)
|
||||||
end
|
end
|
||||||
|
|
||||||
local checkbox = {
|
local checkbox = {
|
||||||
check = function(line)
|
check = function(line)
|
||||||
return line:gsub(unchecked_checkbox, checked_checkbox)
|
return line:gsub(unchecked_checkbox, checked_checkbox, 1)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
uncheck = function(line)
|
uncheck = function(line)
|
||||||
return line:gsub(checked_checkbox, unchecked_checkbox)
|
return line:gsub(checked_checkbox, unchecked_checkbox, 1)
|
||||||
|
end,
|
||||||
|
|
||||||
|
make_checkbox = function(line)
|
||||||
|
if not line:match("^%s*-%s.*$") and not line:match("^%s*%d%s.*$") then
|
||||||
|
-- "xxx" -> "- [ ] xxx"
|
||||||
|
return line:gsub("(%S+)", "- [ ] %1", 1)
|
||||||
|
else
|
||||||
|
-- "- xxx" -> "- [ ] xxx", "3. xxx" -> "3. [ ] xxx"
|
||||||
|
return line:gsub("(%s*- )(.*)", "%1[ ] %2", 1):gsub("(%s*%d%. )(.*)", "%1[ ] %2", 1)
|
||||||
|
end
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,9 +50,12 @@ M.toggle = function()
|
||||||
-- If the line contains a checked checkbox then uncheck it.
|
-- If the line contains a checked checkbox then uncheck it.
|
||||||
-- Otherwise, if it contains an unchecked checkbox, check it.
|
-- Otherwise, if it contains an unchecked checkbox, check it.
|
||||||
local new_line = ""
|
local new_line = ""
|
||||||
if line_contains_an_unchecked_checkbox(current_line) then
|
|
||||||
|
if not line_with_checkbox(current_line) then
|
||||||
|
new_line = checkbox.make_checkbox(current_line)
|
||||||
|
elseif line_contains_unchecked(current_line) then
|
||||||
new_line = checkbox.check(current_line)
|
new_line = checkbox.check(current_line)
|
||||||
else
|
elseif line_contains_checked(current_line) then
|
||||||
new_line = checkbox.uncheck(current_line)
|
new_line = checkbox.uncheck(current_line)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -38,6 +63,6 @@ M.toggle = function()
|
||||||
vim.api.nvim_win_set_cursor(0, cursor)
|
vim.api.nvim_win_set_cursor(0, cursor)
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.api.nvim_create_user_command("ToggleCheckbox", M.toggle, {});
|
vim.api.nvim_create_user_command("ToggleCheckbox", M.toggle, {})
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
Loading…
Reference in a new issue