From 5db20bbb890d066480d8818e2dd4079c6d2e382b Mon Sep 17 00:00:00 2001 From: Diogo Pombeiro Date: Sat, 14 Nov 2026 11:59:00 +0100 Subject: [PATCH] feat: cycle checkboxes through every marker Add :CycleCheckbox and (bullets-cycle-checkbox), which advance a checkbox through all configured markers, partial ones included, and wrap from checked to unchecked. With nested checkboxes, cycling to checked or unchecked updates children, cycling to a partial marker keeps them, and ancestors are recomputed. Parents with no checked children but at least one partial child now use the first partial marker instead of unchecked. --- README.md | 8 ++ lua/bullets/actions.lua | 72 +++++++++-- lua/bullets/init.lua | 6 + openspec/specs/checkbox-state/spec.md | 50 ++++++++ test/checkboxes_spec.lua | 164 ++++++++++++++++++++++++++ 5 files changed, 289 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 5d1866f..126f9fd 100644 --- a/README.md +++ b/README.md @@ -80,12 +80,19 @@ The plugin defines these commands: | `:RenumberList` | Renumber the current list. | | `:RenumberSelection` | Renumber the selected range. | | `:ToggleCheckbox` | Toggle the checkbox on the current item. | +| `:CycleCheckbox` | Cycle the checkbox on the current item. | | `:RecomputeCheckboxes` | Recompute nested checkbox states. | | `:BulletDemote` | Demote the current item. | | `:BulletPromote` | Promote the current item. | | `:BulletDemoteVisual` | Demote the selected range. | | `:BulletPromoteVisual` | Promote the selected range. | +`:CycleCheckbox` moves the checkbox through every marker in `checkbox_markers`, partial ones included, and wraps +from checked back to unchecked. With `checkbox_markers = " /x"` it cycles `[ ]` → `[/]` → `[x]` → `[ ]`, which suits +"in progress" tracking. When `nested_checkboxes` is enabled, cycling to checked or unchecked updates the children, +cycling to a partial marker leaves them alone, and ancestors are recomputed. It has no default mapping; map +`(bullets-cycle-checkbox)` to use it. + These selection commands are registered but not implemented yet: `:SelectCheckbox`, `:SelectCheckboxInside`, `:SelectBullet`, and `:SelectBulletText`. @@ -114,6 +121,7 @@ The plugin also defines these `` mappings for custom mappings: | `(bullets-newline)` | Insert, Normal | | `(bullets-renumber)` | Normal, Visual | | `(bullets-toggle-checkbox)` | Normal | +| `(bullets-cycle-checkbox)` | Normal | | `(bullets-recompute-checkboxes)` | Normal | | `(bullets-demote)` | Insert, Normal, Visual | | `(bullets-promote)` | Insert, Normal, Visual | diff --git a/lua/bullets/actions.lua b/lua/bullets/actions.lua index 6e0ccb5..56c3e09 100644 --- a/lua/bullets/actions.lua +++ b/lua/bullets/actions.lua @@ -982,6 +982,7 @@ local function checkbox_tree(first, last) table.remove(stack) end if #stack > 0 then + item.parent = stack[#stack] table.insert(stack[#stack].children, item) end table.insert(stack, item) @@ -1008,21 +1009,33 @@ local function partial_marker(markers, checked, total) return markers[index] end +local function recompute_parent(item) + local checked = 0 + local in_progress = false + for _, child in ipairs(item.children) do + local state = checkbox_state(child.checkbox) + if state == 'checked' then + checked = checked + 1 + elseif state == 'partial' then + in_progress = true + end + end + + local markers = item.checkbox.markers + local marker = partial_marker(markers, checked, #item.children) + if checked == 0 and in_progress and #markers > 2 then + marker = markers[2] + end + item.checkbox.marker = marker + item.checkbox.index = checkbox_marker_index(marker, markers) + set_checkbox_marker(item.lnum, item.bullet, item.checkbox, marker) +end + local function recompute_items(items) for index = #items, 1, -1 do local item = items[index] if #item.children > 0 then - local checked = 0 - for _, child in ipairs(item.children) do - if checkbox_state(child.checkbox) == 'checked' then - checked = checked + 1 - end - end - - local marker = partial_marker(item.checkbox.markers, checked, #item.children) - item.checkbox.marker = marker - item.checkbox.index = checkbox_marker_index(marker, item.checkbox.markers) - set_checkbox_marker(item.lnum, item.bullet, item.checkbox, marker) + recompute_parent(item) elseif checkbox_state(item.checkbox) == 'partial' then local marker = item.checkbox.markers[1] item.checkbox.marker = marker @@ -1096,6 +1109,43 @@ function M.toggle_checkbox() end end +function M.cycle_checkbox() + local lnum = vim.api.nvim_win_get_cursor(0)[1] + local item = checkbox_item(lnum) + if not item or not item.checkbox then + return + end + + local markers = item.checkbox.markers + local index = item.checkbox.index % #markers + 1 + local marker = markers[index] + item.checkbox.marker = marker + item.checkbox.index = index + set_checkbox_marker(lnum, item.bullet, item.checkbox, marker) + + if not config.options.nested_checkboxes then + return + end + + -- Partial markers mean "in progress" for this item only, so children keep their state. + if checkbox_state(item.checkbox) ~= 'partial' then + set_descendant_checkboxes(lnum, item.bullet, marker) + end + + -- Only ancestors are recomputed: recomputing the cycled item would override its new marker. + local first, last = checkbox_range(lnum) + for _, tree_item in ipairs(checkbox_tree(first, last)) do + if tree_item.lnum == lnum then + local parent = tree_item.parent + while parent do + recompute_parent(parent) + parent = parent.parent + end + return + end + end +end + function M.recompute_checkboxes() local lnum = vim.api.nvim_win_get_cursor(0)[1] local first, last = checkbox_range(lnum) diff --git a/lua/bullets/init.lua b/lua/bullets/init.lua index 701ca21..10a60e9 100644 --- a/lua/bullets/init.lua +++ b/lua/bullets/init.lua @@ -38,6 +38,9 @@ local function add_commands() command('ToggleCheckbox', function() actions().toggle_checkbox() end) + command('CycleCheckbox', function() + actions().cycle_checkbox() + end) command('RecomputeCheckboxes', function() actions().recompute_checkboxes() end) @@ -81,6 +84,9 @@ local function add_plug_mappings() map('n', '(bullets-toggle-checkbox)', function() actions().toggle_checkbox() end) + map('n', '(bullets-cycle-checkbox)', function() + actions().cycle_checkbox() + end) map('n', '(bullets-recompute-checkboxes)', function() actions().recompute_checkboxes() end) diff --git a/openspec/specs/checkbox-state/spec.md b/openspec/specs/checkbox-state/spec.md index bb333fc..b7789c1 100644 --- a/openspec/specs/checkbox-state/spec.md +++ b/openspec/specs/checkbox-state/spec.md @@ -44,6 +44,49 @@ The plugin SHALL toggle recognized checkbox markers according to configured mark - WHEN checkbox items are toggled or inserted - THEN the configured markers are used for checkbox state +### Requirement: Cycle checkbox markers + +The plugin SHALL cycle a checkbox through every configured marker, including partial markers, when checkbox cycling is triggered. + +#### Scenario: Cycle to the next configured marker {#CS-013} + +- GIVEN the current line contains a recognized checkbox marker that is not the checked marker +- WHEN checkbox cycling is triggered +- THEN the marker advances to the next configured marker + +#### Scenario: Cycle wraps after the checked marker {#CS-014} + +- GIVEN the current line contains the checked marker +- WHEN checkbox cycling is triggered +- THEN the marker becomes the unchecked marker + +#### Scenario: Cycling to a partial marker keeps children {#CS-015} + +- GIVEN nested checkboxes are enabled +- AND a parent checkbox has child checkboxes +- WHEN checkbox cycling moves the parent to a partial marker +- THEN the parent keeps the partial marker and child checkbox markers are unchanged + +#### Scenario: Cycling to checked or unchecked updates children {#CS-016} + +- GIVEN nested checkboxes are enabled +- AND a parent checkbox has child checkboxes +- WHEN checkbox cycling moves the parent to the checked or unchecked marker +- THEN child checkbox markers follow the parent marker + +#### Scenario: Cycling a child updates ancestors {#CS-017} + +- GIVEN nested checkboxes are enabled +- AND a child checkbox has ancestor checkboxes +- WHEN checkbox cycling changes the child marker +- THEN each ancestor checkbox marker reflects its children + +#### Scenario: Cycling without nested checkboxes {#CS-018} + +- GIVEN nested checkboxes are disabled +- WHEN checkbox cycling is triggered on a parent checkbox +- THEN only the current checkbox marker changes + ### Requirement: Propagate nested checkbox state The plugin SHALL update related parent and child checkboxes when nested checkbox state changes. @@ -72,6 +115,13 @@ The plugin SHALL update related parent and child checkboxes when nested checkbox - WHEN checkbox state is recomputed - THEN parent checkbox markers indicate partial completion +#### Scenario: In-progress children mark parents partial {#CS-019} + +- GIVEN a parent checkbox has no checked children +- AND at least one child checkbox has a partial marker +- WHEN parent checkbox state is updated from its children +- THEN the parent checkbox uses the first partial marker + ### Requirement: Recompute checkbox state The plugin SHALL recompute nested checkbox state on demand. diff --git a/test/checkboxes_spec.lua b/test/checkboxes_spec.lua index fcd94b4..eb2b82d 100644 --- a/test/checkboxes_spec.lua +++ b/test/checkboxes_spec.lua @@ -341,4 +341,168 @@ describe('checkboxes', function() }, helpers.get_lines()) end) end) + + describe('cycling checkboxes', function() + before_each(function() + helpers.reset_config() + require('bullets').setup { checkbox_markers = ' /x' } + end) + + it('cycles to the next configured marker #CS-013', function() + helpers.new_buffer { '- [ ] task' } + + vim.cmd 'CycleCheckbox' + assert.are.same({ '- [/] task' }, helpers.get_lines()) + + vim.cmd 'CycleCheckbox' + assert.are.same({ '- [x] task' }, helpers.get_lines()) + end) + + it('cycles with the plug mapping #CS-013', function() + helpers.new_buffer { '- [ ] task' } + + helpers.feedkeys '(bullets-cycle-checkbox)' + + assert.are.same({ '- [/] task' }, helpers.get_lines()) + end) + + it('wraps from checked to unchecked #CS-014', function() + helpers.new_buffer { '- [x] done', '- [X] also done' } + + helpers.feedkeys 'gg' + vim.cmd 'CycleCheckbox' + helpers.feedkeys 'j' + vim.cmd 'CycleCheckbox' + + assert.are.same({ '- [ ] done', '- [ ] also done' }, helpers.get_lines()) + end) + + it('keeps children when cycling a parent to a partial marker #CS-015', function() + helpers.new_buffer { + '- [ ] parent', + ' - [ ] first child', + ' - [ ] second child', + } + + helpers.feedkeys 'gg' + vim.cmd 'CycleCheckbox' + + assert.are.same({ + '- [/] parent', + ' - [ ] first child', + ' - [ ] second child', + }, helpers.get_lines()) + end) + + it('updates children when cycling a parent to checked or unchecked #CS-016', function() + helpers.new_buffer { + '- [/] parent', + ' - [/] first child', + ' - [ ] second child', + } + + helpers.feedkeys 'gg' + vim.cmd 'CycleCheckbox' + assert.are.same({ + '- [x] parent', + ' - [x] first child', + ' - [x] second child', + }, helpers.get_lines()) + + vim.cmd 'CycleCheckbox' + assert.are.same({ + '- [ ] parent', + ' - [ ] first child', + ' - [ ] second child', + }, helpers.get_lines()) + end) + + it('updates ancestors when cycling a child #CS-017', function() + helpers.new_buffer { + '- [ ] grandparent', + ' - [ ] parent', + ' - [ ] first child', + ' - [ ] second child', + ' - [x] done sibling', + } + + helpers.feedkeys 'gg2j' + vim.cmd 'CycleCheckbox' + vim.cmd 'CycleCheckbox' + assert.are.same({ + '- [/] grandparent', + ' - [/] parent', + ' - [x] first child', + ' - [ ] second child', + ' - [x] done sibling', + }, helpers.get_lines()) + + helpers.feedkeys 'j' + vim.cmd 'CycleCheckbox' + vim.cmd 'CycleCheckbox' + assert.are.same({ + '- [x] grandparent', + ' - [x] parent', + ' - [x] first child', + ' - [x] second child', + ' - [x] done sibling', + }, helpers.get_lines()) + end) + + it('only changes the current checkbox without nested checkboxes #CS-018', function() + require('bullets').setup { checkbox_markers = ' /x', nested_checkboxes = false } + helpers.new_buffer { + '- [ ] parent', + ' - [ ] child', + } + + helpers.feedkeys 'gg' + vim.cmd 'CycleCheckbox' + vim.cmd 'CycleCheckbox' + + assert.are.same({ + '- [x] parent', + ' - [ ] child', + }, helpers.get_lines()) + end) + + it('marks a parent partial when a child is in progress #CS-019', function() + helpers.new_buffer { + '- [ ] parent', + ' - [ ] first child', + ' - [ ] second child', + } + + helpers.feedkeys 'ggj' + vim.cmd 'CycleCheckbox' + + assert.are.same({ + '- [/] parent', + ' - [/] first child', + ' - [ ] second child', + }, helpers.get_lines()) + end) + + it('recomputes parents with in-progress children as partial #CS-019', function() + require('bullets').setup { checkbox_markers = ' .oOX' } + helpers.new_buffer { + '- [ ] grandparent', + ' - [ ] parent', + ' - [X] first child', + ' - [ ] second child', + ' - [ ] sibling', + } + + helpers.feedkeys 'gg' + vim.cmd 'RecomputeCheckboxes' + + assert.are.same({ + '- [.] grandparent', + ' - [o] parent', + ' - [X] first child', + ' - [ ] second child', + ' - [ ] sibling', + }, helpers.get_lines()) + end) + end) end)