From 93715a3d6b59e4bd5531051fe79658cedd9c2c6e Mon Sep 17 00:00:00 2001 From: borja Date: Mon, 27 Nov 2023 18:58:59 +0100 Subject: [PATCH] =?UTF-8?q?cambia=20el=20reconocimiento=20por=20patrones?= =?UTF-8?q?=20que=20deber=C3=ADan=20aceptar=20los=20espacios=20iniciales?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/todoer.lua | 52 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/plugin/todoer.lua b/plugin/todoer.lua index c881c5a..acf81a1 100644 --- a/plugin/todoer.lua +++ b/plugin/todoer.lua @@ -26,25 +26,53 @@ end vim.keymap.set("i", "", press_enter, { desc = "On enter", noremap = true, expr = true }) -- function that checks if the current line starts with the string "- [ ]" or "- [x]" and toggles the x + local function toggle_todo() + -- create a variable called openpattern with a pattern that matches a string that starts with any amount of spaces or tabs followed by "- [ ]" + local openpattern = "^%s*%- %[[ x]%]" + + -- create a variable called closepattern with a pattern that matches a string that starts with any amount of spaces or tabs followed by "- [x]" + local closedpattern = "^%s*%- %[[ x]%]" + + -- create a variable called titleopenpattern with a pattern that matches a string that starts with "# [ ]" + local titleopenpattern = "^# [ ]" + + -- create a variable called titleclosedpattern with a pattern that matches a string that starts with "# [x]" + local titleclosedpattern = "^# [x]" + local line = vim.api.nvim_get_current_line() - if string.sub(line, 1, 5) == "- [ ]" then - line = string.sub(line, 6, -1) - line = "- [x]" .. line + + if string.match(line, openpattern) then + line = string.gsub(line, openpattern, "- [x]") vim.api.nvim_set_current_line(line) - elseif string.sub(line, 1, 5) == "- [x]" then - line = string.sub(line, 6, -1) - line = "- [ ]" .. line + elseif string.match(line, closedpattern) then + line = string.gsub(line, closedpattern, "- [ ]") vim.api.nvim_set_current_line(line) - elseif string.sub(line, 1, 5) == "# [ ]" then - line = string.sub(line, 6, -1) - line = "# [x]" .. line + elseif string.match(line, titleopenpattern) then + line = string.gsub(line, titleopenpattern, "# [x]") vim.api.nvim_set_current_line(line) - elseif string.sub(line, 1, 5) == "# [x]" then - line = string.sub(line, 6, -1) - line = "# [ ]" .. line + elseif string.match(line, titleclosedpattern) then + line = string.gsub(line, titleclosedpattern, "# [ ]") vim.api.nvim_set_current_line(line) end + + -- if string.sub(line, 1, 5) == "- [ ]" then + -- line = string.sub(line, 6, -1) + -- line = "- [x]" .. line + -- vim.api.nvim_set_current_line(line) + -- elseif string.sub(line, 1, 5) == "- [x]" then + -- line = string.sub(line, 6, -1) + -- line = "- [ ]" .. line + -- vim.api.nvim_set_current_line(line) + -- elseif string.sub(line, 1, 5) == "# [ ]" then + -- line = string.sub(line, 6, -1) + -- line = "# [x]" .. line + -- vim.api.nvim_set_current_line(line) + -- elseif string.sub(line, 1, 5) == "# [x]" then + -- line = string.sub(line, 6, -1) + -- line = "# [ ]" .. line + -- vim.api.nvim_set_current_line(line) + -- end end vim.keymap.set("n", "tt", toggle_todo, { desc = "Toggle todo" })