Skip to content

Commit

Permalink
feat(todo_items): todo-changed event (#1651)
Browse files Browse the repository at this point in the history
  • Loading branch information
benlubas authored Feb 20, 2025
1 parent ef2255e commit 5f0195d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 18 deletions.
1 change: 1 addition & 0 deletions lua/neorg/core/modules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ local utils = require("neorg.core.utils")
--- @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.
--- @field on_event fun(event: neorg.event) A callback that is invoked any time an event the module has subscribed to has fired.

---@class neorg.modules
local modules = {}

--- Returns a new Neorg module, exposing all the necessary function and variables.
Expand Down
79 changes: 61 additions & 18 deletions lua/neorg/modules/core/qol/todo_items/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Parent items of the same type and children items of the same type are update acc
--]]

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

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

Expand Down Expand Up @@ -117,6 +117,26 @@ module.config.public = {
---|"uncertain"

module.private = {
names = {
["x"] = "done",
[" "] = "undone",
["-"] = "pending",
["="] = "on_hold",
["_"] = "cancelled",
["!"] = "important",
["+"] = "recurring",
["?"] = "ambiguous",
},
fire_update_event = function(char, line)
local ev = modules.create_event(
module,
module.events.defined["todo-changed"].type,
{ char = char, line = line }
)
if ev then
modules.broadcast_event(ev)
end
end,
--- Updates the parent todo item for the current todo item if it exists
---@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.
update_parent = function(buf, line, recursion_level)
Expand Down Expand Up @@ -223,6 +243,8 @@ module.private = {

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

module.private.fire_update_event(resulting_char, row)

module.private.update_parent(buf, line, recursion_level + 1)
return
end
Expand All @@ -239,6 +261,8 @@ module.private = {
{ resulting_char }
)

module.private.fire_update_event(resulting_char, range.row_start)

module.private.update_parent(buf, line, recursion_level + 1)
end,

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

module.private.fire_update_event(char, range.row_start)

vim.api.nvim_buf_set_text(
buf,
range.row_start,
Expand Down Expand Up @@ -404,31 +430,44 @@ module.private = {
end,
}

local function task_set(character, name)
return neorg.utils.wrap_dotrepeat(function()
local buffer = vim.api.nvim_get_current_buf()
local cursor = vim.api.nvim_win_get_cursor(0)
---Set the todo item in the given buffer at the given line
---@param buffer number 0 for current
---@param line number 1 based line number, 0 for current
---@param character string
local function task_set_at(buffer, line, character)
local name = module.private.names[character]
if buffer == 0 then
buffer = vim.api.nvim_get_current_buf()
end
if line == 0 then
line = vim.api.nvim_win_get_cursor(0)[1]
end
local todo_item_at_cursor = module.private.get_todo_item_from_cursor(buffer, line - 1)

local todo_item_at_cursor = module.private.get_todo_item_from_cursor(buffer, cursor[1] - 1)
if not todo_item_at_cursor then
return
end

if not todo_item_at_cursor then
return
end
module.private.make_all(buffer, todo_item_at_cursor, name, character)
end

module.private.make_all(buffer, todo_item_at_cursor, name, character)
local function task_set(character)
return neorg.utils.wrap_dotrepeat(function()
task_set_at(0, 0, character)
end)
end

---@class core.qol.todo_items
module.public = {
["task-done"] = task_set("x", "done"),
["task-undone"] = task_set(" ", "undone"),
["task-pending"] = task_set("-", "pending"),
["task-on-hold"] = task_set("=", "on_hold"),
["task-cancelled"] = task_set("_", "cancelled"),
["task-important"] = task_set("!", "important"),
["task-recurring"] = task_set("+", "recurring"),
["task-ambiguous"] = task_set("?", "ambiguous"),
["set_at"] = task_set_at,
["task-done"] = task_set("x"),
["task-undone"] = task_set(" "),
["task-pending"] = task_set("-"),
["task-on-hold"] = task_set("="),
["task-cancelled"] = task_set("_"),
["task-important"] = task_set("!"),
["task-recurring"] = task_set("+"),
["task-ambiguous"] = task_set("?"),
["task-cycle"] = function()
local buffer = vim.api.nvim_get_current_buf()
local cursor = vim.api.nvim_win_get_cursor(0)
Expand All @@ -453,4 +492,8 @@ module.public = {
end,
}

module.events.defined = {
["todo-changed"] = modules.define_event(module, "todo-changed"),
}

return module

0 comments on commit 5f0195d

Please sign in to comment.