diff --git a/lua/neorg/modules/core/integrations/telescope/module.lua b/lua/neorg/modules/core/integrations/telescope/module.lua index 9265254..a8fd681 100644 --- a/lua/neorg/modules/core/integrations/telescope/module.lua +++ b/lua/neorg/modules/core/integrations/telescope/module.lua @@ -7,45 +7,52 @@ require("neorg.modules.base") local module = neorg.modules.create("core.integrations.telescope") module.setup = function() - return { success = true, requires = { "core.keybinds", "core.norg.dirman" } } + return { success = true, requires = { "core.keybinds", "core.norg.dirman" } } end module.load = function() - local telescope_loaded, telescope = pcall(require, "telescope") + local telescope_loaded, telescope = pcall(require, "telescope") - assert(telescope_loaded, telescope) + assert(telescope_loaded, telescope) - telescope.load_extension("neorg") + telescope.load_extension("neorg") - module.required["core.keybinds"].register_keybinds(module.name, { "find_linkable", "insert_link", "insert_file_link", "search_headings" }) + module.required["core.keybinds"].register_keybinds( + module.name, + { "find_linkable", "insert_link", "insert_file_link", "search_headings", "find_project_tasks" } + ) end module.public = { - find_linkable = require("telescope._extensions.neorg.find_linkable"), - insert_link = require("telescope._extensions.neorg.insert_link"), - insert_file_link = require("telescope._extensions.neorg.insert_file_link"), - search_headings = require("telescope._extensions.neorg.search_headings"), + find_linkable = require("telescope._extensions.neorg.find_linkable"), + insert_link = require("telescope._extensions.neorg.insert_link"), + insert_file_link = require("telescope._extensions.neorg.insert_file_link"), + search_headings = require("telescope._extensions.neorg.search_headings"), + find_project_tasks = require("telescope._extensions.neorg.find_project_tasks"), } module.on_event = function(event) - if event.split_type[2] == "core.integrations.telescope.find_linkable" then - module.public.find_linkable() - elseif event.split_type[2] == "core.integrations.telescope.insert_link" then - module.public.insert_link() - elseif event.split_type[2] == "core.integrations.telescope.insert_file_link" then - module.public.insert_file_link() - elseif event.split_type[2] == "core.integrations.telescope.search_headings" then - module.public.search_headings() - end + if event.split_type[2] == "core.integrations.telescope.find_linkable" then + module.public.find_linkable() + elseif event.split_type[2] == "core.integrations.telescope.insert_link" then + module.public.insert_link() + elseif event.split_type[2] == "core.integrations.telescope.insert_file_link" then + module.public.insert_file_link() + elseif event.split_type[2] == "core.integrations.telescope.search_headings" then + module.public.search_headings() + elseif event.split_type[2] == "core.integrations.telescope.find_project_tasks" then + module.public.find_project_tasks() + end end module.events.subscribed = { - ["core.keybinds"] = { - ["core.integrations.telescope.find_linkable"] = true, - ["core.integrations.telescope.insert_link"] = true, - ["core.integrations.telescope.insert_file_link"] = true, - ["core.integrations.telescope.search_headings"] = true, - }, + ["core.keybinds"] = { + ["core.integrations.telescope.find_linkable"] = true, + ["core.integrations.telescope.insert_link"] = true, + ["core.integrations.telescope.insert_file_link"] = true, + ["core.integrations.telescope.search_headings"] = true, + ["core.integrations.telescope.find_project_tasks"] = true, + }, } return module diff --git a/lua/telescope/_extensions/neorg.lua b/lua/telescope/_extensions/neorg.lua index 32efcec..bd36fa2 100644 --- a/lua/telescope/_extensions/neorg.lua +++ b/lua/telescope/_extensions/neorg.lua @@ -1,8 +1,9 @@ return require("telescope").register_extension({ - exports = { - find_linkable = require("neorg.modules.core.integrations.telescope.module").public.find_linkable, - insert_link = require("neorg.modules.core.integrations.telescope.module").public.insert_link, - insert_file_link = require("neorg.modules.core.integrations.telescope.module").public.insert_file_link, - search_headings = require("neorg.modules.core.integrations.telescope.module").public.search_headings, - }, + exports = { + find_linkable = require("neorg.modules.core.integrations.telescope.module").public.find_linkable, + insert_link = require("neorg.modules.core.integrations.telescope.module").public.insert_link, + insert_file_link = require("neorg.modules.core.integrations.telescope.module").public.insert_file_link, + search_headings = require("neorg.modules.core.integrations.telescope.module").public.search_headings, + find_project_tasks = require("neorg.modules.core.integrations.telescope.module").public.find_project_tasks, + }, }) diff --git a/lua/telescope/_extensions/neorg/find_project_tasks.lua b/lua/telescope/_extensions/neorg/find_project_tasks.lua new file mode 100644 index 0000000..15ba0d1 --- /dev/null +++ b/lua/telescope/_extensions/neorg/find_project_tasks.lua @@ -0,0 +1,83 @@ +local actions = require("telescope.actions") +local actions_set = require("telescope.actions.set") +local state = require("telescope.actions.state") +local finders = require("telescope.finders") +local pickers = require("telescope.pickers") +local conf = require("telescope.config").values + +local neorg_loaded, _ = pcall(require, "neorg.modules") + +assert(neorg_loaded, "Neorg is not loaded - please make sure to load Neorg first") + +local function get_project_tasks() + local tasks_raw = neorg.modules.get_module("core.gtd.queries").get("tasks") -- or "tasks" + tasks_raw = neorg.modules.get_module("core.gtd.queries").add_metadata(tasks_raw, "task") + local projects_tasks = neorg.modules.get_module("core.gtd.queries").sort_by("project_uuid", tasks_raw) + return projects_tasks +end + +local function get_projects() + local projects_raw = neorg.modules.get_module("core.gtd.queries").get("projects") -- or "projects" + projects_raw = neorg.modules.get_module("core.gtd.queries").add_metadata(projects_raw, "project") + return projects_raw +end + +local function pick_tasks(project) + local project_tasks = get_project_tasks() + local tasks = project_tasks[project.uuid] + local opts = {} + + pickers.new(opts, { + prompt_title = "Picker Project Tasks:" .. project.content, + results_title = "Tasks", + finder = finders.new_table({ + results = tasks, + entry_maker = function(entry) + return { + value = entry, + display = entry.content, + ordinal = entry.content, + } + end, + }), + previewer = nil, + sorter = conf.generic_sorter(opts), + attach_mappings = function(prompt_bufnr) + actions_set.select:replace(function() + local entry = state.get_selected_entry() + actions.close(prompt_bufnr) + dump(entry.value) + end) + return true + end, + }):find() +end + +return function(opts) + opts = opts or {} + + pickers.new(opts, { + prompt_title = "Pick Neorg Gtd Projects", + results_title = "Projects", + finder = finders.new_table({ + results = get_projects(), + entry_maker = function(entry) + return { + value = entry, + display = entry.content, + ordinal = entry.content, + } + end, + }), + previewer = nil, + sorter = conf.generic_sorter(opts), + attach_mappings = function(prompt_bufnr) + actions_set.select:replace(function() + local entry = state.get_selected_entry() + actions.close(prompt_bufnr) + pick_tasks(entry.value) + end) + return true + end, + }):find() +end