Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
`<Plug>(bullets-cycle-checkbox)` to use it.

These selection commands are registered but not implemented yet: `:SelectCheckbox`,
`:SelectCheckboxInside`, `:SelectBullet`, and `:SelectBulletText`.

Expand Down Expand Up @@ -114,6 +121,7 @@ The plugin also defines these `<Plug>` mappings for custom mappings:
| `<Plug>(bullets-newline)` | Insert, Normal |
| `<Plug>(bullets-renumber)` | Normal, Visual |
| `<Plug>(bullets-toggle-checkbox)` | Normal |
| `<Plug>(bullets-cycle-checkbox)` | Normal |
| `<Plug>(bullets-recompute-checkboxes)` | Normal |
| `<Plug>(bullets-demote)` | Insert, Normal, Visual |
| `<Plug>(bullets-promote)` | Insert, Normal, Visual |
Expand Down
72 changes: 61 additions & 11 deletions lua/bullets/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions lua/bullets/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -81,6 +84,9 @@ local function add_plug_mappings()
map('n', '<Plug>(bullets-toggle-checkbox)', function()
actions().toggle_checkbox()
end)
map('n', '<Plug>(bullets-cycle-checkbox)', function()
actions().cycle_checkbox()
end)
map('n', '<Plug>(bullets-recompute-checkboxes)', function()
actions().recompute_checkboxes()
end)
Expand Down
50 changes: 50 additions & 0 deletions openspec/specs/checkbox-state/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
164 changes: 164 additions & 0 deletions test/checkboxes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<Plug>(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)