Skip to content

Commit

Permalink
feat: added new gtd pickers
Browse files Browse the repository at this point in the history
  • Loading branch information
max397574 committed Mar 15, 2022
1 parent 7dc894d commit 5e144d5
Show file tree
Hide file tree
Showing 5 changed files with 761 additions and 4 deletions.
26 changes: 22 additions & 4 deletions lua/neorg/modules/core/integrations/telescope/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ module.load = function()

telescope.load_extension("neorg")

module.required["core.keybinds"].register_keybinds(
module.name,
{ "find_linkable", "insert_link", "insert_file_link", "search_headings", "find_project_tasks" }
)
module.required["core.keybinds"].register_keybinds(module.name, {
"find_linkable",
"insert_link",
"insert_file_link",
"search_headings",
"find_project_tasks",
"find_aof_project_tasks",
"find_aof_tasks",
"find_context_tasks",
})
end

module.public = {
Expand All @@ -29,6 +35,9 @@ module.public = {
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"),
find_context_tasks = require("telescope._extensions.neorg.find_context_tasks"),
find_aof_tasks = require("telescope._extensions.neorg.find_aof_tasks"),
find_aof_project_tasks = require("telescope._extensions.neorg.find_aof_project_tasks"),
}

module.on_event = function(event)
Expand All @@ -42,6 +51,12 @@ module.on_event = function(event)
module.public.search_headings()
elseif event.split_type[2] == "core.integrations.telescope.find_project_tasks" then
module.public.find_project_tasks()
elseif event.split_type[2] == "core.integrations.telescope.find_aof_tasks" then
module.public.find_aof_tasks()
elseif event.split_type[2] == "core.integrations.telescope.find_aof_project_tasks" then
module.public.find_aof_project_tasks()
elseif event.split_type[2] == "core.integrations.telescope.find_context_tasks" then
module.public.find_context_tasks()
end
end

Expand All @@ -52,6 +67,9 @@ module.events.subscribed = {
["core.integrations.telescope.insert_file_link"] = true,
["core.integrations.telescope.search_headings"] = true,
["core.integrations.telescope.find_project_tasks"] = true,
["core.integrations.telescope.find_context_tasks"] = true,
["core.integrations.telescope.find_aof_tasks"] = true,
["core.integrations.telescope.find_aof_project_tasks"] = true,
},
}

Expand Down
3 changes: 3 additions & 0 deletions lua/telescope/_extensions/neorg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ return require("telescope").register_extension({
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_context_tasks = require("neorg.modules.core.integrations.telescope.module").public.find_context_tasks,
find_project_tasks = require("neorg.modules.core.integrations.telescope.module").public.find_project_tasks,
find_aof_tasks = require("neorg.modules.core.integrations.telescope.module").public.find_aof_tasks,
find_aof_project_tasks = require("neorg.modules.core.integrations.telescope.module").public.find_aof_project_tasks,
},
})
303 changes: 303 additions & 0 deletions lua/telescope/_extensions/neorg/find_aof_project_tasks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,303 @@
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 previewers = require("telescope.previewers")
local entry_display = require("telescope.pickers.entry_display")

local ns = vim.api.nvim_create_namespace("neorg-telescope_project_tasks")

local neorg_loaded, _ = pcall(require, "neorg.modules")

assert(neorg_loaded, "Neorg is not loaded - please make sure to load Neorg first")

local states = {
["undone"] = { "-[ ] ", "NeorgTodoItem1Undone" },
["done"] = { "-[x] ", "NeorgTodoItem1Done" },
["pending"] = { "-[-] ", "NeorgTodoItem1Pending" },
["cancelled"] = { "-[_] ", "NeorgTodoItem1Cancelled" },
["uncertain"] = { "-[?] ", "NeorgTodoItem1Uncertain" },
["urgent"] = { "-[!] ", "NeorgTodoItem1Urgent" },
["recurring"] = { "-[+] ", "NeorgTodoItem1Recurring" },
["on_hold"] = { "-[=] ", "NeorgTodoItem1OnHold" },
}

local function get_project_tasks()
local tasks_raw = neorg.modules.get_module("core.gtd.queries").get("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")
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)
local displayer = entry_display.create({
items = {
{ width = 100 },
},
})
local function make_display(ent)
return displayer({
{
entry.content,
function()
return { { { 0, 100 }, states[entry.state][2] } }
end,
},
})
end

return {
value = entry,
display = function(tbl)
return make_display(tbl.value)
end,
ordinal = entry.content,
}
end,
}),
previewer = previewers.new_buffer_previewer({
define_preview = function(self, entry, status)
local lines = {}
local line_nr = 1
local special_lines = {}
if entry.value.contexts then
table.insert(lines, "Contexts:")
table.insert(special_lines, line_nr)
line_nr = line_nr + 1
for _, context in ipairs(entry.value.contexts) do
table.insert(lines, context)
line_nr = line_nr + 1
end
end
if entry.value["waiting.for"] then
table.insert(lines, "Waiting for:")
table.insert(special_lines, line_nr)
line_nr = line_nr + 1
for _, waiting_for in ipairs(entry.value["waiting.for"]) do
table.insert(lines, waiting_for)
line_nr = line_nr + 1
end
end
if entry.value["time.start"] then
table.insert(lines, "Time start:")
table.insert(special_lines, line_nr)
line_nr = line_nr + 1
table.insert(lines, entry.value["time.start"][1])
line_nr = line_nr + 1
end
if entry.value["time.due"] then
table.insert(lines, "Time due:")
table.insert(special_lines, line_nr)
line_nr = line_nr + 1
table.insert(lines, entry.value["time.due"][1])
line_nr = line_nr + 1
end
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, true, lines)
for _, line_number in ipairs(special_lines) do
vim.api.nvim_buf_add_highlight(self.state.bufnr, ns, "Special", line_number - 1, 0, -1)
end
end,
}),

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)
neorg.modules.get_module("core.gtd.ui").callbacks.goto_task_function(entry.value)
end)
return true
end,
}):find()
end

local function get_task_list(project)
local project_tasks = get_project_tasks()
local raw_tasks = project_tasks[project.uuid]
local tasks = {}
local highlights = {}
if raw_tasks == {} or not raw_tasks then
return {}, {}
end
for _, task in ipairs(raw_tasks) do
table.insert(tasks, states[task.state][1] .. task.content)
table.insert(highlights, states[task.state][2])
end
return tasks, highlights
end

local function get_aof_projects()
local projects = get_projects()
local projects_by_aof = neorg.modules.get_module("core.gtd.queries").sort_by("area_of_focus", projects)
return projects_by_aof
end

local function get_aofs()
local aofs = vim.tbl_keys(get_aof_projects())
return aofs
end

local function pick_projects(aof)
local projects = get_projects()
local projects_by_aof = neorg.modules.get_module("core.gtd.queries").sort_by("area_of_focus", projects)

local opts = {}

pickers.new(opts, {
prompt_title = "Pick Neorg Gtd Projects",
results_title = "Projects from ",
finder = finders.new_table({
results = projects_by_aof[aof],
entry_maker = function(entry)
local displayer = entry_display.create({
items = {
{ width = 100 },
},
})
local function make_display(ent)
return displayer({
{
entry.content,
function()
--- check if there are no tasks in the project
local tasks = get_task_list(ent)
if #tasks == 0 then
--- If no tasks highlight with "Comment"
return { { { 0, 100 }, "Comment" } }
--- Highlight with "Special"
else
return { { { 0, 100 }, "Special" } }
end
end,
},
})
end

return {
value = entry,
display = function(tbl)
return make_display(tbl.value)
end,
ordinal = entry.content,
}
end,
}),
previewer = previewers.new_buffer_previewer({
define_preview = function(self, entry, status)
local tasks, highlights = get_task_list(entry.value)
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, true, tasks)
vim.bo[self.state.bufnr].filetype = "norg"
for i, highlight in ipairs(highlights) do
vim.api.nvim_buf_add_highlight(self.state.bufnr, ns, highlight, i - 1, 0, 5)
end
end,
}),
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)
local tasks = get_task_list(entry.value)
if #tasks == 0 then
neorg.modules.get_module("core.gtd.ui").callbacks.goto_task_function(entry.value)
return true
end

pick_tasks(entry.value)
end)
return true
end,
}):find()
end

local function get_project_list(aof)
local aof_projects = get_aof_projects()
local projects = aof_projects[aof]
local project_names = {}
for _, project in ipairs(projects) do
table.insert(project_names, project.content)
end
return project_names
end

return function(opts)
opts = opts or {}

pickers.new(opts, {
prompt_title = "Pick Area Of Focus",
results_title = "AOFs",
finder = finders.new_table({
results = get_aofs(),
entry_maker = function(entry)
local displayer = entry_display.create({
items = {
{ width = 100 },
},
})
local function make_display(ent)
local display = ent.value
if ent.value == "_" then
display = "Projects without an aof"
end
return displayer({
{
display,
function()
--- check if there are no tasks in the project
local aof_projects = get_aof_projects()
if #aof_projects[ent.value] == 0 then
--- If no tasks highlight with "Comment"
return { { { 0, 100 }, "Comment" } }
--- Highlight with "Special"
else
return { { { 0, 100 }, "Special" } }
end
end,
},
})
end

return {
value = entry,
display = make_display,
ordinal = entry,
}
end,
}),
previewer = previewers.new_buffer_previewer({
define_preview = function(self, entry, status)
local projects_preview = get_project_list(entry.value)
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, true, projects_preview)
vim.bo[self.state.bufnr].filetype = "norg"
end,
}),
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_projects(entry.value)
end)
return true
end,
}):find()
end
Loading

0 comments on commit 5e144d5

Please sign in to comment.