todo-comments.nvim
todo-comments.nvim copied to clipboard
feature: TODO with numbers
Did you check the docs?
- [X] I have read all the todo-comments.nvim docs
Is your feature request related to a problem? Please describe.
no
Describe the solution you'd like
I often use TODO labels with numbers, like TODO 1 : TODO 2 :. I'm not sure whether the keywords support regular expressions, but if so I can do that with something like TODO \d+ : to match all TODO with numbers.
Besides, it'll be nice if the jumpings to next or previous TODO are numbers-aware. For example, I have many TODO 2 and many TODO 3, and TODOs with the same number indicate some common goals, then I can jump to next TODO 2 with something like jump_next_same_number()
And of course you can populate these things to other labels.
Describe alternatives you've considered
no
Additional context
No response
For the TODO labels with numbers, you could try to do something like this in your config setup:
config = function()
require("todo-comments").setup {
highlight = {
pattern = [[.*<(KEYWORDS)\s*\d+\s*:]],
},
}
end
Does this help?
Thanks. I missed the highlight config. But can I ask what is the < in the pattern? Special charaters?
And I think numbers should be highlighted with keywords, too. I tried [[.*<(KEYWORDS\s*\d+)\s*:]] but it didn't match.
Actually, I'm not sure what is the <.
Make sure that the (KEYWORDS) is unchanged, the pattern that you provided has (KEYWORDS\s*\d+). The correct configuration should give you something like this:

After some grep in the source code, I found < is the special character for "beginning of word" in vim, since they add \v before the pattern configured.
In your screenshot, only TODO and spaces around it have an fg-bg-inverted highlight, but I'd like to include the numbers, too. The parentheses should be a submatch that will have an inverted bg, since the code:
https://github.com/folke/todo-comments.nvim/blob/8febc60a76feefd8203077ef78b6a262ea1a41f9/lua/todo-comments/highlight.lua#L34-L54
Possible config is:
highlight = {
pattern = [[.*<((KEYWORDS)\s*\d+)\s*:]],
keyword = 'bg',
},
This plugin will replace KEYWORDS into something like FIX|TODO|INFO, so we need an extra pair of parentheses around KEYWORDS, just like the above config.
However it will not work again, because present implementation uses keywords without numbers to index your config:
https://github.com/folke/todo-comments.nvim/blob/8febc60a76feefd8203077ef78b6a262ea1a41f9/lua/todo-comments/highlight.lua#L199
So even if we matched the right thing in highlight.pattern config, it will not be in keywords config.
So I think it's not something that can be accomplished by a pattern. Needs an extra feature.
Ooh, now I get it. I'm sorry for my misinterpretation of your problem...
I have a similar format insofaras there's data attached to the keyword before the : delimiter, like this:
// TODO([email protected]): The message
It would be very nice to be able to highlight the (...) part differently from the keyword and the text that follows. Additionally, it would be very nice to have this data show in a dedicated column in Telescope, but I think that's a different issue.
I would also like to have something like TODO(name or scope): highlighted with the part in parens.
I ended up poking apart the code and figured out why I couldn't get it working
Only the 'keyword' part of the pattern is highlighted. However, you can't extend the keyword part of the pattern, since it will then fail to look up highlighting information.
I finally got it working like so:
{
'folke/todo-comments.nvim',
dependencies = { 'nvim-lua/plenary.nvim' },
opts = {
signs = false,
highlight = {
pattern = [[.*<(%(KEYWORDS)%(\(.{-1,}\))?):]]
}
},
config = function(_, opts)
require('todo-comments').setup(opts)
-- todo-comments.config.keywords is the list of keyword aliases.
-- It is used to map from the matched pattern to one of the highlight group names.
-- Add a custom __index that fuzzy matches against the KEYWORD(...) pattern
local o = require('todo-comments.config')
setmetatable(o.keywords, { __index = function(t, k)
local kw = vim.fn.matchstr(k, [[^\w\+]])
if kw == '' then
return nil
else
return rawget(t, kw)
end
end})
end
},
I also needed to change https://github.com/folke/todo-comments.nvim/blob/bca0e00644c22a3eecedce703c0db080dd6bdc55/lua/todo-comments/highlight.lua#L50 to local start = str:find(kw, 0, true) to prevent the matched text from being interpreted as a lua pattern by string.find, since it treats the ( and ) characters specially.
With that, it finally works and highlights proper:
This is all pretty accursed as you can imagine. Maybe @folke has a suggestion for how this could be made into a proper feature based on this?
I've opened a pull request implementing this feature (#255). If you'd like to try it out, you can use the following lazy.nvim spec:
{
-- https://github.com/folke/todo-comments.nvim/pull/255
'LunarLambda/todo-comments.nvim',
branch = 'enhanced-matching',
dependencies = { 'nvim-lua/plenary.nvim' },
opts = {
highlight = {
pattern = {
-- NOTE(xyz):
[[.*<((KEYWORDS)%(\(.{-1,}\))?):]],
-- TODO 123:
[[.*<((KEYWORDS)%(\s+\d+)?):]],
}
}
},
}
Isn't your PR trying to do the same thing with #180 or am I missing something? It's just that #180 only allows for text to be considered as part of the highlight, whereas yours allows for numbers as well?
I hadn't seen #180. It's matching logic is more fuzzy, For example, it assumes the pattern ends with a single character that shouldn't be highlighted (e.g. :) and it looks like it might highlight extra whitespace too?
My PR maintains the fact only the first capture group of the pattern is highlighted, which allows precisely controlling leading and trailing characters.
Can your PR also match the trailing colon to be highlighted?
It does if you use the "wide" highlighting (default configuration), see the screenshot in https://github.com/folke/todo-comments.nvim/issues/185#issuecomment-1972101975
Maybe I didn't express myself correctly. I'm more interested if the colon can have a, for example black, fg inside the highlight itself just after the ending parenthesis just like the rest of the characters that are being highlighted.
move the : inside the capturing group of the pattern, and use the non-wide highlighting
(I'm outside right now so I can't give a config example)
It's ok. No need to provide an example. I was just interested if it could be done and your explanation is perfectly comprehensive. Looking forward to your PR getting merged :smile:
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.