Skip to content

Commit 5f0195d

Browse files
authored
feat(todo_items): todo-changed event (#1651)
1 parent ef2255e commit 5f0195d

File tree

2 files changed

+62
-18
lines changed

2 files changed

+62
-18
lines changed

lua/neorg/core/modules.lua

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ local utils = require("neorg.core.utils")
8787
--- @field replaced? boolean If `true`, this means the module is a replacement for a core module. This flag is set automatically whenever `setup().replaces` is set to a value.
8888
--- @field on_event fun(event: neorg.event) A callback that is invoked any time an event the module has subscribed to has fired.
8989

90+
---@class neorg.modules
9091
local modules = {}
9192

9293
--- Returns a new Neorg module, exposing all the necessary function and variables.

lua/neorg/modules/core/qol/todo_items/module.lua

+61-18
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Parent items of the same type and children items of the same type are update acc
2323
--]]
2424

2525
local neorg = require("neorg.core")
26-
local log, modules = neorg.log, neorg.modules
26+
local log, modules = neorg.log, neorg.modules --[[@as neorg.modules]]
2727

2828
local module = modules.create("core.qol.todo_items")
2929

@@ -117,6 +117,26 @@ module.config.public = {
117117
---|"uncertain"
118118

119119
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,
120140
--- Updates the parent todo item for the current todo item if it exists
121141
---@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.
122142
update_parent = function(buf, line, recursion_level)
@@ -223,6 +243,8 @@ module.private = {
223243

224244
vim.api.nvim_buf_set_text(buf, row, column, row, column, { "(" .. resulting_char .. ") " })
225245

246+
module.private.fire_update_event(resulting_char, row)
247+
226248
module.private.update_parent(buf, line, recursion_level + 1)
227249
return
228250
end
@@ -239,6 +261,8 @@ module.private = {
239261
{ resulting_char }
240262
)
241263

264+
module.private.fire_update_event(resulting_char, range.row_start)
265+
242266
module.private.update_parent(buf, line, recursion_level + 1)
243267
end,
244268

@@ -336,6 +360,8 @@ module.private = {
336360
else
337361
local range = module.required["core.integrations.treesitter"].get_node_range(first_status_extension)
338362

363+
module.private.fire_update_event(char, range.row_start)
364+
339365
vim.api.nvim_buf_set_text(
340366
buf,
341367
range.row_start,
@@ -404,31 +430,44 @@ module.private = {
404430
end,
405431
}
406432

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)
411446

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
413450

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
417453

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)
419457
end)
420458
end
421459

422460
---@class core.qol.todo_items
423461
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("?"),
432471
["task-cycle"] = function()
433472
local buffer = vim.api.nvim_get_current_buf()
434473
local cursor = vim.api.nvim_win_get_cursor(0)
@@ -453,4 +492,8 @@ module.public = {
453492
end,
454493
}
455494

495+
module.events.defined = {
496+
["todo-changed"] = modules.define_event(module, "todo-changed"),
497+
}
498+
456499
return module

0 commit comments

Comments
 (0)