folke / todo-comments.nvim

✅ Highlight, list and search todo comments in your projects
Apache License 2.0
3.06k stars 88 forks source link

feature: api for fetching todo counts #197

Open crides opened 1 year ago

crides commented 1 year ago

Did you check the docs?

Is your feature request related to a problem? Please describe.

Not possible yet to have the todo counts on the statusline

Describe the solution you'd like

some api functions are provided to fetch the type, icon, and count of todos in a buffer

Describe alternatives you've considered

can't really think of other good alternatives now; relying on loclist/telescope intermediate commands seems pretty janky

Additional context

No response

dasupradyumna commented 1 year ago

Although an API for this explicit purpose is missing, you can still write your own statusline component generating function using require('todo-comments.search').search(), as mentioned in #172. As @folke has mentioned in that same thread, you need to properly throttle this function so it does not slow down the UI.

I have written a primitive implementation for the same purpose, but never got around to throttling it for performance since it is working well enough for me (for now). You can adapt it for your case if you wish, I will update it for throttling when I find the time. You can find the function statusline_todo_list() here (on line 92 at time of writing).

crides commented 1 year ago

Thanks. I'll look into this when I got more time

juiceDeLemon commented 1 year ago

Although an API for this explicit purpose is missing, you can still write your own statusline component generating function using require('todo-comments.search').search(), as mentioned in #172. As @folke has mentioned in that same thread, you need to properly throttle this function so it does not slow down the UI.

I have written a primitive implementation for the same purpose, but never got around to throttling it for performance since it is working well enough for me (for now). You can adapt it for your case if you wish, I will update it for throttling when I find the time. You can find the function statusline_todo_list() here (on line 92 at time of writing).

Hi. I'm interested in that function and the link does not work. Would you mind if you can share the function?

dasupradyumna commented 1 year ago

Sorry about that, here you go! :)

local todo = {
  ---@type table<string,string> map of TODO keywords and their icons
  render = {
    FIX = '',
    HACK = '',
    NOTE = '',
    PERF = '',
    TEST = '✎',
    TODO = '',
    WARN = '',
  },
  ---@type table<string, integer> map of TODO keywords and their counts
  stats = {},
}
---returns a highlight-supported string of TODO comment statistics
-- PERF: expensive function, proper throttling required
-- [ Check https://github.com/folke/todo-comments.nvim/issues/172#issuecomment-1382691260 ]
function status.statusline_todo_list()
  if not require('todo-comments.config').loaded then return '' end

  -- count number of keyword occurences
  require('todo-comments.search').search(function(entries)
    todo.stats = {}
    for _, entry in ipairs(entries) do
      todo.stats[entry.tag] = (todo.stats[entry.tag] or 0) + 1
    end
  end, { disable_not_found_warnings = true })

  local out = {}
  for keyword, count in vim.spairs(todo.stats) do
    table.insert(out, ('%%#TodoFg%s#%s %d '):format(keyword, todo.render[keyword], count))
  end
  if not vim.tbl_isempty(out) then
    table.insert(out, 1, '%#TabLineSel#◤◢%* ')
    table.insert(out, '%#TabLineSel#◤◢%*')
  end
  return table.concat(out)
end

There are a bunch of config-specific things here and there, but you should be able to adapt it to your tastes.

juiceDeLemon commented 1 year ago

thanks!

github-actions[bot] commented 1 month ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

Lazerbeak12345 commented 1 month ago

I would like this feature.

github-actions[bot] commented 1 day ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.