Skip to content

Commit 3194dad

Browse files
committed
Add support for percentage cookies
1 parent 71f38a7 commit 3194dad

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

lua/orgmode/treesitter/headline.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ function Headline:remove_closed_date()
121121
end
122122

123123
function Headline:cookie()
124-
return self:parse('%[%d?/%d?%]')
124+
local cookie = self:parse('%[%d?/%d?%]')
125+
if cookie then
126+
return cookie
127+
end
128+
return self:parse('%[%d?%d?%d?%%%]')
125129
end
126130

127131
-- @return tsnode, string

lua/orgmode/treesitter/list.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ function List:parent_cookie()
2323
local content = top_item:field('contents')[1]
2424
-- The cookie should be the last thing on the line
2525
local cookie_node = content:named_child(content:named_child_count() - 1)
26-
if query.get_node_text(cookie_node, 0):match('%[%d?/%d?%]') then
26+
27+
local text = query.get_node_text(cookie_node, 0)
28+
if text:match('%[%d?/%d?%]') or text:match('%[%d?%d?%d?%%%]') then
2729
return cookie_node
2830
end
2931
end
@@ -42,7 +44,12 @@ function List:update_parent_cookie()
4244
local checked_boxes = vim.tbl_filter(function(box)
4345
return box:match('%[%w%]')
4446
end, checkboxes)
45-
local new_status = ('[%d/%d]'):format(#checked_boxes, #checkboxes)
47+
local new_status
48+
if query.get_node_text(parent_cookie, 0):find('%%') then
49+
new_status = ('[%d%%]'):format((#checked_boxes/#checkboxes) * 100)
50+
else
51+
new_status = ('[%d/%d]'):format(#checked_boxes, #checkboxes)
52+
end
4653
tree_utils.set_node_text(parent_cookie, new_status)
4754
end
4855

lua/orgmode/utils/treesitter.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local ts_utils = require('nvim-treesitter.ts_utils')
22
local config = require('orgmode.config')
3+
local query = vim.treesitter.query
34
local M = {}
45

56
function M.current_node()

tests/plenary/ui/mappings_spec.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,4 +1618,27 @@ describe('Mappings', function()
16181618
vim.cmd([[exe "norm \<C-space>"]])
16191619
assert.are.same('- Test orgmode [1/2]', vim.fn.getline(1))
16201620
end)
1621+
1622+
it('should update the checklist cookies with a percentage within a headline', function()
1623+
helpers.load_file_content({
1624+
'* Test orgmode [%]',
1625+
'- [ ] checkbox item',
1626+
'- [ ] checkbox item',
1627+
})
1628+
vim.fn.cursor(2, 1)
1629+
vim.cmd([[exe "norm \<C-space>"]])
1630+
assert.are.same('* Test orgmode [50%]', vim.fn.getline(1))
1631+
end)
1632+
1633+
it('should update the checklist cookies with a percentage within a nested list', function()
1634+
helpers.load_file_content({
1635+
'- Test orgmode [%]',
1636+
' - [ ] checkbox item',
1637+
' - [ ] checkbox item',
1638+
' - [ ] checkbox item',
1639+
})
1640+
vim.fn.cursor(2, 1)
1641+
vim.cmd([[exe "norm \<C-space>"]])
1642+
assert.are.same('- Test orgmode [33%]', vim.fn.getline(1))
1643+
end)
16211644
end)

0 commit comments

Comments
 (0)