Skip to content

Commit

Permalink
feat: initial project tasks picker
Browse files Browse the repository at this point in the history
  • Loading branch information
max397574 committed Mar 12, 2022
1 parent 4e78fc6 commit d067b4f
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 30 deletions.
55 changes: 31 additions & 24 deletions lua/neorg/modules/core/integrations/telescope/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 7 additions & 6 deletions lua/telescope/_extensions/neorg.lua
Original file line number Diff line number Diff line change
@@ -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,
},
})
83 changes: 83 additions & 0 deletions lua/telescope/_extensions/neorg/find_project_tasks.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d067b4f

Please sign in to comment.