From 1a5ba4378cadd6d46c1a4ae5becb8f8cf520d170 Mon Sep 17 00:00:00 2001 From: "borja (aider)" Date: Wed, 30 Apr 2025 09:57:56 +0200 Subject: [PATCH] feat: Outdent empty TODO lines on Enter press --- lua/todoer/init.lua | 52 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/lua/todoer/init.lua b/lua/todoer/init.lua index cdefb69..e44c104 100644 --- a/lua/todoer/init.lua +++ b/lua/todoer/init.lua @@ -140,17 +140,47 @@ local function setup_buffer_keymaps() return "" elseif details.is_todo and not has_content then - -- Case 2: Empty TODO line (e.g., "- [ ]") - -- TODO: Implement Step 3 logic (outdenting) here later. - -- For now, keep the old (potentially problematic) feedkeys behavior - -- or simply clear the line and let Neovim handle Enter. - -- Let's try clearing the line and returning for now. - vim.api.nvim_buf_set_lines(0, lnum - 1, lnum, false, { details.indent }) -- Replace with just indent - return vim.api.nvim_replace_termcodes("", true, false, true) -- Let Neovim handle newline - - -- Old feedkeys behavior (kept for reference, but commented out): - -- vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("S", true, false, true), "n", false) - -- return "" -- Feedkeys handles it + -- Case 2: Empty TODO line (e.g., "- [ ]") - Implement Outdenting + local current_indent = details.indent + local current_indent_len = #current_indent + local sw = vim.bo.shiftwidth -- Get buffer's shiftwidth + local et = vim.bo.expandtab -- Get buffer's expandtab setting + + if current_indent_len > 0 then + -- Line is indented, calculate new indentation + local new_indent = "" + if et then -- Using spaces + local target_indent_len = math.max(0, current_indent_len - sw) + new_indent = string.rep(" ", target_indent_len) + else -- Using tabs + -- Remove the first tab character found + new_indent = string.gsub(current_indent, "^\t", "", 1) + -- Safety check: if gsub didn't change anything (e.g., indent was spaces), + -- try removing spaces as a fallback, though this is less ideal with noexpandtab + if new_indent == current_indent and current_indent_len > 0 then + local target_indent_len = math.max(0, current_indent_len - sw) + new_indent = string.rep(" ", target_indent_len) + end + end + + -- Replace the current line with the outdented version + local new_marker = details.marker or "-" + local new_line_content = new_indent .. new_marker .. " [ ] " + vim.api.nvim_buf_set_lines(0, lnum - 1, lnum, false, { new_line_content }) + + -- Move cursor after the "] " on the modified line + local cursor_col_bytes = #new_line_content + vim.api.nvim_win_set_cursor(0, { lnum, cursor_col_bytes }) + + -- Action handled + return "" + else + -- Line is not indented, terminate the list + -- Replace the line with an empty string + vim.api.nvim_buf_set_lines(0, lnum - 1, lnum, false, { "" }) + -- Let Neovim handle inserting a newline below the now-empty line + return vim.api.nvim_replace_termcodes("", true, false, true) + end else -- Case 3: Not a TODO line