@@ -23,7 +23,7 @@ Parent items of the same type and children items of the same type are update acc
23
23
--]]
24
24
25
25
local neorg = require (" neorg.core" )
26
- local log , modules = neorg .log , neorg .modules
26
+ local log , modules = neorg .log , neorg .modules --[[ @as neorg.modules ]]
27
27
28
28
local module = modules .create (" core.qol.todo_items" )
29
29
@@ -117,6 +117,26 @@ module.config.public = {
117
117
--- | " uncertain"
118
118
119
119
module .private = {
120
+ names = {
121
+ [" x" ] = " done" ,
122
+ [" " ] = " undone" ,
123
+ [" -" ] = " pending" ,
124
+ [" =" ] = " on_hold" ,
125
+ [" _" ] = " cancelled" ,
126
+ [" !" ] = " important" ,
127
+ [" +" ] = " recurring" ,
128
+ [" ?" ] = " ambiguous" ,
129
+ },
130
+ fire_update_event = function (char , line )
131
+ local ev = modules .create_event (
132
+ module ,
133
+ module .events .defined [" todo-changed" ].type ,
134
+ { char = char , line = line }
135
+ )
136
+ if ev then
137
+ modules .broadcast_event (ev )
138
+ end
139
+ end ,
120
140
--- Updates the parent todo item for the current todo item if it exists
121
141
--- @param recursion_level number the index of the parent to change. The higher the number the more the code will traverse up the syntax tree.
122
142
update_parent = function (buf , line , recursion_level )
@@ -223,6 +243,8 @@ module.private = {
223
243
224
244
vim .api .nvim_buf_set_text (buf , row , column , row , column , { " (" .. resulting_char .. " ) " })
225
245
246
+ module .private .fire_update_event (resulting_char , row )
247
+
226
248
module .private .update_parent (buf , line , recursion_level + 1 )
227
249
return
228
250
end
@@ -239,6 +261,8 @@ module.private = {
239
261
{ resulting_char }
240
262
)
241
263
264
+ module .private .fire_update_event (resulting_char , range .row_start )
265
+
242
266
module .private .update_parent (buf , line , recursion_level + 1 )
243
267
end ,
244
268
@@ -336,6 +360,8 @@ module.private = {
336
360
else
337
361
local range = module .required [" core.integrations.treesitter" ].get_node_range (first_status_extension )
338
362
363
+ module .private .fire_update_event (char , range .row_start )
364
+
339
365
vim .api .nvim_buf_set_text (
340
366
buf ,
341
367
range .row_start ,
@@ -404,31 +430,44 @@ module.private = {
404
430
end ,
405
431
}
406
432
407
- local function task_set (character , name )
408
- return neorg .utils .wrap_dotrepeat (function ()
409
- local buffer = vim .api .nvim_get_current_buf ()
410
- local cursor = vim .api .nvim_win_get_cursor (0 )
433
+ --- Set the todo item in the given buffer at the given line
434
+ --- @param buffer number 0 for current
435
+ --- @param line number 1 based line number , 0 for current
436
+ --- @param character string
437
+ local function task_set_at (buffer , line , character )
438
+ local name = module .private .names [character ]
439
+ if buffer == 0 then
440
+ buffer = vim .api .nvim_get_current_buf ()
441
+ end
442
+ if line == 0 then
443
+ line = vim .api .nvim_win_get_cursor (0 )[1 ]
444
+ end
445
+ local todo_item_at_cursor = module .private .get_todo_item_from_cursor (buffer , line - 1 )
411
446
412
- local todo_item_at_cursor = module .private .get_todo_item_from_cursor (buffer , cursor [1 ] - 1 )
447
+ if not todo_item_at_cursor then
448
+ return
449
+ end
413
450
414
- if not todo_item_at_cursor then
415
- return
416
- end
451
+ module .private .make_all (buffer , todo_item_at_cursor , name , character )
452
+ end
417
453
418
- module .private .make_all (buffer , todo_item_at_cursor , name , character )
454
+ local function task_set (character )
455
+ return neorg .utils .wrap_dotrepeat (function ()
456
+ task_set_at (0 , 0 , character )
419
457
end )
420
458
end
421
459
422
460
--- @class core.qol.todo_items
423
461
module .public = {
424
- [" task-done" ] = task_set (" x" , " done" ),
425
- [" task-undone" ] = task_set (" " , " undone" ),
426
- [" task-pending" ] = task_set (" -" , " pending" ),
427
- [" task-on-hold" ] = task_set (" =" , " on_hold" ),
428
- [" task-cancelled" ] = task_set (" _" , " cancelled" ),
429
- [" task-important" ] = task_set (" !" , " important" ),
430
- [" task-recurring" ] = task_set (" +" , " recurring" ),
431
- [" task-ambiguous" ] = task_set (" ?" , " ambiguous" ),
462
+ [" set_at" ] = task_set_at ,
463
+ [" task-done" ] = task_set (" x" ),
464
+ [" task-undone" ] = task_set (" " ),
465
+ [" task-pending" ] = task_set (" -" ),
466
+ [" task-on-hold" ] = task_set (" =" ),
467
+ [" task-cancelled" ] = task_set (" _" ),
468
+ [" task-important" ] = task_set (" !" ),
469
+ [" task-recurring" ] = task_set (" +" ),
470
+ [" task-ambiguous" ] = task_set (" ?" ),
432
471
[" task-cycle" ] = function ()
433
472
local buffer = vim .api .nvim_get_current_buf ()
434
473
local cursor = vim .api .nvim_win_get_cursor (0 )
@@ -453,4 +492,8 @@ module.public = {
453
492
end ,
454
493
}
455
494
495
+ module .events .defined = {
496
+ [" todo-changed" ] = modules .define_event (module , " todo-changed" ),
497
+ }
498
+
456
499
return module
0 commit comments