Skip to content

Commit 4b92931

Browse files
committed
feat(todo_items): todo-changed event
1 parent ef2255e commit 4b92931

File tree

2 files changed

+73
-21
lines changed

2 files changed

+73
-21
lines changed

lua/neorg/core/modules.lua

Lines changed: 1 addition & 0 deletions
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

Lines changed: 72 additions & 21 deletions
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

@@ -58,8 +58,8 @@ module.config.public = {
5858
--
5959
-- Defaults to the following order: `undone`, `done`, `pending`.
6060
order = {
61-
{ "undone", " " },
62-
{ "done", "x" },
61+
{ "undone", " " },
62+
{ "done", "x" },
6363
{ "pending", "-" },
6464
},
6565

@@ -72,7 +72,7 @@ module.config.public = {
7272
-- Defaults to the following order: `undone`, `done`.
7373
order_with_children = {
7474
{ "undone", " " },
75-
{ "done", "x" },
75+
{ "done", "x" },
7676
},
7777

7878
-- When set to `true`, will automatically convert parent
@@ -117,6 +117,16 @@ 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+
},
120130
--- Updates the parent todo item for the current todo item if it exists
121131
---@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.
122132
update_parent = function(buf, line, recursion_level)
@@ -223,6 +233,12 @@ module.private = {
223233

224234
vim.api.nvim_buf_set_text(buf, row, column, row, column, { "(" .. resulting_char .. ") " })
225235

236+
local ev =
237+
modules.create_event(module, "core.qol.todo-items.task-update", { char = resulting_char, line = row })
238+
if ev then
239+
modules.broadcast_event(ev)
240+
end
241+
226242
module.private.update_parent(buf, line, recursion_level + 1)
227243
return
228244
end
@@ -239,6 +255,15 @@ module.private = {
239255
{ resulting_char }
240256
)
241257

258+
local ev = modules.create_event(
259+
module,
260+
"core.qol.todo-items.task-update",
261+
{ char = resulting_char, line = range.row_start }
262+
)
263+
if ev then
264+
modules.broadcast_event(ev)
265+
end
266+
242267
module.private.update_parent(buf, line, recursion_level + 1)
243268
end,
244269

@@ -336,6 +361,15 @@ module.private = {
336361
else
337362
local range = module.required["core.integrations.treesitter"].get_node_range(first_status_extension)
338363

364+
local ev = modules.create_event(
365+
module,
366+
module.events.defined["todo-changed"].type,
367+
{ char = char, line = range.row_start }
368+
)
369+
if ev then
370+
modules.broadcast_event(ev)
371+
end
372+
339373
vim.api.nvim_buf_set_text(
340374
buf,
341375
range.row_start,
@@ -404,31 +438,44 @@ module.private = {
404438
end,
405439
}
406440

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)
441+
---Set the todo item in the given buffer at the given line
442+
---@param buffer number 0 for current
443+
---@param line number 1 based line number, 0 for current
444+
---@param character string
445+
local function task_set_at(buffer, line, character)
446+
local name = module.private.names[character]
447+
if buffer == 0 then
448+
buffer = vim.api.nvim_get_current_buf()
449+
end
450+
if line == 0 then
451+
line = vim.api.nvim_win_get_cursor(0)[1]
452+
end
453+
local todo_item_at_cursor = module.private.get_todo_item_from_cursor(buffer, line - 1)
411454

412-
local todo_item_at_cursor = module.private.get_todo_item_from_cursor(buffer, cursor[1] - 1)
455+
if not todo_item_at_cursor then
456+
return
457+
end
413458

414-
if not todo_item_at_cursor then
415-
return
416-
end
459+
module.private.make_all(buffer, todo_item_at_cursor, name, character)
460+
end
417461

418-
module.private.make_all(buffer, todo_item_at_cursor, name, character)
462+
local function task_set(character)
463+
return neorg.utils.wrap_dotrepeat(function()
464+
task_set_at(0, 0, character)
419465
end)
420466
end
421467

422468
---@class core.qol.todo_items
423469
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"),
470+
["set_at"] = task_set_at,
471+
["task-done"] = task_set("x"),
472+
["task-undone"] = task_set(" "),
473+
["task-pending"] = task_set("-"),
474+
["task-on-hold"] = task_set("="),
475+
["task-cancelled"] = task_set("_"),
476+
["task-important"] = task_set("!"),
477+
["task-recurring"] = task_set("+"),
478+
["task-ambiguous"] = task_set("?"),
432479
["task-cycle"] = function()
433480
local buffer = vim.api.nvim_get_current_buf()
434481
local cursor = vim.api.nvim_win_get_cursor(0)
@@ -453,4 +500,8 @@ module.public = {
453500
end,
454501
}
455502

503+
module.events.defined = {
504+
["todo-changed"] = modules.define_event(module, "todo-changed"),
505+
}
506+
456507
return module

0 commit comments

Comments
 (0)