Skip to content

feat: add tasks command #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open

Conversation

andrewinci
Copy link

@andrewinci andrewinci commented Mar 21, 2025

Add the command ObsidianTasks to allow users to quickly filter all the todos.

Changes:

  • Allow user to specify the name of a status for tasks
  • Use the picker to list all tasks by status name

Copy link

@sotte sotte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just left some comments.

I have a similar, albeit much simpler, rg command to look for various 'tasks'. Having this in the core makes sense IMO.

Side note:

Maybe it is worth discussing what tasks feature obsidian.nvim wants to implement in the long-term.
Obsidian comes with the fairly popular tasks plugin: https://github.com/obsidian-tasks-group/obsidian-tasks
And there are a few issues/PR related to task in the original repo.


---@return obsidian.Task[]
Client.find_tasks = function(self)
local openTasks = search.search(self.dir, "- \\[(.)\\]", search.SearchOpts.default())
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tasks can also be * [ ] like this.

One probably wants to adjust where to search for tasks: specific file, current file, folder, regex. Not everybody closes or moves their tasks.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And since recently also <number> [ ] like this. We probably want something like this:

'([-+*]|[0-9]+\.) \[[- x/<>?!*"lb~]\] .+'

That's grep -E syntax, I am not sure how it correctly translates to lua regexp string. The various states I got from https://publish.obsidian.md/tasks/Getting+Started/Statuses, perhaps that could be reduced to the ones supported by obsidian.nvim. I also included ~, which is supported by render-markdown. I used this to test the regex:

$ cat test.md
- [ ] normal
+ [ ] plus
* [ ] star
- [x] normal x-ed
- [!] normal !-ed
- [~] normal ~-ed (render-markdown)
1. [ ] ordered
2. [x] ordered x-ed

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are actually quite a few ways to define checkbox lists. We should consolidate once this #48 is merged.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The different types of list items should be supported now. I'm not restricting the supported statuses beside needing to be only one char since they are configurable in the ui.checkboxes config

@sotte
Copy link

sotte commented Apr 6, 2025

@andrewinci I just gave this a test run. With snacks.picker it works as advertised.

With telescope as picker it causes problems:

   Error  08:24:54 msg_show.lua_error Error executing vim.schedule lua callback: ...share/nvim/lazy/telescope.nvim/lua/telescope/pickers.lua:1468: Cursor position outside buffer
stack traceback:
	[C]: in function 'nvim_win_set_cursor'
	...share/nvim/lazy/telescope.nvim/lua/telescope/pickers.lua:1468: in function ''
	vim/_editor.lua: in function <vim/_editor.lua:0>

@sstark
Copy link

sstark commented Apr 7, 2025

Much interest from my side. I just tried out your branch and it's how I would imagine such a feature. What I am missing is:

  • Jump to note containing a task with <enter>
  • Change which todo states are listed from inside picker. Looks like there is code that should do that on C-n, but for me C-n just jumps to the next line in the picker
  • Change which todo states are listed as argument like :ObsidianTasks open
  • Toggle state directly from picker

I use the todo plugin in obsidian, although I like the idea of having a floating todo list like this even better. It's available everywhere and not tied to a specific document and it is not adding a big new concept of a dynamically updating markdown document.

@sotte
Copy link

sotte commented Apr 21, 2025

@andrewinci do you want to push this feature along? This would be highly appreciated.

@andrewinci
Copy link
Author

Hey @sotte, I'll try to pick this up this weekend

@andrewinci andrewinci force-pushed the main branch 2 times, most recently from 41c2571 to cd0ebbd Compare April 26, 2025 16:11
@sstark
Copy link

sstark commented Apr 27, 2025

@andrewinci I just gave this a test run. With snacks.picker it works as advertised.

With telescope as picker it causes problems:

   Error  08:24:54 msg_show.lua_error Error executing vim.schedule lua callback: ...share/nvim/lazy/telescope.nvim/lua/telescope/pickers.lua:1468: Cursor position outside buffer
stack traceback:
	[C]: in function 'nvim_win_set_cursor'
	...share/nvim/lazy/telescope.nvim/lua/telescope/pickers.lua:1468: in function ''
	vim/_editor.lua: in function <vim/_editor.lua:0>

This seems to happen for telescope when newlines are encountered in the input. I could get rid of it by replacing newlines:

diff --git a/lua/obsidian/commands/tasks.lua b/lua/obsidian/commands/tasks.lua
index 8be0995..e839581 100644
--- a/lua/obsidian/commands/tasks.lua
+++ b/lua/obsidian/commands/tasks.lua
@@ -51,8 +51,9 @@ local function showTasks(client, data)
   for _, task in ipairs(tasks) do
     local tStatus = checkboxes[task.status]
     if tStatus and (not filter or tStatus.name == filter) then
+      descr_trimmed = string.gsub(task.description, "\n"," ")
       table.insert(toShow, {
-        display = string.format(" %s", task.description),
+        display = string.format(" %s", descr_trimmed),
         filename = task.path,
         lnum = task.line,
         icon = tStatus.char,

Not sure if that is the appropriate place or even method for this, but at least that would work for a first approach perhaps.

See also: nvim-telescope/telescope.nvim#3163

@andrewinci
Copy link
Author

@sstark, thank you for the suggestion, I've just updated the client to strip out \n from the description as suggested.

@andrewinci andrewinci changed the title Add tasks helper feat: add tasks command Apr 27, 2025
@andrewinci andrewinci marked this pull request as ready for review April 27, 2025 19:23
@andrewinci andrewinci requested a review from sotte April 27, 2025 19:23
@sstark
Copy link

sstark commented Apr 27, 2025

@andrewinci Cool! Thank you for doing that. Some comments from my side:

  • With Telescope I get a preview window, with snacks.picker I don't. Is that a configuration on user side that needs to be done?
  • :Obsidian tasks <tab> works nicely with blink
  • When selecting a task with snacks.pick configured, nothing opens. With Telescope it does.
  • Obsidian tasks is mentioned in README.md, but not in obsidian.txt, so can not be found offline
  • Toggling task filter in Telescope with <C-n> does not seem to work

@andrewinci andrewinci force-pushed the main branch 6 times, most recently from ade5ddb to 95cc004 Compare April 28, 2025 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants