todo-comments.nvim icon indicating copy to clipboard operation
todo-comments.nvim copied to clipboard

feature: TODO with numbers

Open Martinits opened this issue 2 years ago • 16 comments

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

Martinits avatar Mar 09 '23 06:03 Martinits

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?

MatheusKael avatar Apr 13 '23 00:04 MatheusKael

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.

Martinits avatar Apr 13 '23 07:04 Martinits

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: image

MatheusKael avatar Apr 13 '23 20:04 MatheusKael

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.

Martinits avatar Apr 14 '23 06:04 Martinits

Ooh, now I get it. I'm sorry for my misinterpretation of your problem...

MatheusKael avatar Apr 14 '23 21:04 MatheusKael

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.

Manuzor avatar May 02 '23 11:05 Manuzor

I would also like to have something like TODO(name or scope): highlighted with the part in parens.

LunarLambda avatar Feb 28 '24 09:02 LunarLambda

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: image

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?

LunarLambda avatar Feb 29 '24 22:02 LunarLambda

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+)?):]],
      }
    }
  },
}

LunarLambda avatar Mar 11 '24 15:03 LunarLambda

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?

dpetka2001 avatar Mar 11 '24 15:03 dpetka2001

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.

LunarLambda avatar Mar 11 '24 16:03 LunarLambda

Can your PR also match the trailing colon to be highlighted?

dpetka2001 avatar Mar 11 '24 16:03 dpetka2001

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

LunarLambda avatar Mar 11 '24 16:03 LunarLambda

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.

dpetka2001 avatar Mar 11 '24 16:03 dpetka2001

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)

LunarLambda avatar Mar 11 '24 16:03 LunarLambda

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:

dpetka2001 avatar Mar 11 '24 16:03 dpetka2001

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.

github-actions[bot] avatar Jul 06 '24 01:07 github-actions[bot]