mirror of
https://github.com/opdavies/toggle-checkbox.nvim.git
synced 2025-01-22 20:07:32 +00:00
feat: check or uncheck the current line
This commit is contained in:
parent
805aeab265
commit
c8da955004
|
@ -1,17 +1,26 @@
|
||||||
local checked_character = "x"
|
local checked_character = "x"
|
||||||
|
|
||||||
local line_contains_a_checked_checkbox = function(line)
|
local checked_checkbox = "[" .. checked_character .. "]"
|
||||||
return string.find(line, "[" .. checked_character .. "]")
|
local unchecked_checkbox = "[ ]"
|
||||||
end
|
|
||||||
|
local bufnr = vim.api.nvim_buf_get_number(0)
|
||||||
|
local cursor = vim.api.nvim_win_get_cursor(0)
|
||||||
|
local current_line = vim.api.nvim_buf_get_lines(bufnr, cursor[1] - 1, cursor[1], false)[1] or ""
|
||||||
|
|
||||||
|
-- local line_contains_a_checked_checkbox = function(line)
|
||||||
|
-- return string.find(line, checked_checkbox)
|
||||||
|
-- end
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
M.check = function(line)
|
M.check = function()
|
||||||
return line:gsub("%[ %]", "[" .. checked_character .. "]")
|
local new_line = current_line:gsub("%[ %]", checked_checkbox)
|
||||||
|
vim.api.nvim_buf_set_lines(bufnr, cursor[1] - 1, cursor[1], false, { new_line })
|
||||||
end
|
end
|
||||||
|
|
||||||
M.uncheck = function(line)
|
M.uncheck = function()
|
||||||
return line:gsub("%[" .. checked_character .. "%]", "[]")
|
local new_line = current_line:gsub("%[" .. checked_character .. "%]", unchecked_checkbox)
|
||||||
|
vim.api.nvim_buf_set_lines(bufnr, cursor[1] - 1, cursor[1], false, { new_line })
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
Loading…
Reference in a new issue