Highlight TODO but only in comments

The wiki solution didn’t cut it for me, so I went with this small snippet.

hook -group todohighlight global BufCreate .* %{
	add-highlighter buffer/ dynregex "%opt{comment_line}[ \t]*\b(TODO|FIXME|MAYBE)\b" 1:default+bu@comment
}

This is still flawed however, as TODOs in multi-line comments won’t be highlighted.

FWIW dynregex has fallen from grace.
I think it’s possible to implement the same beavior with a WinSetOption comment_line=.* hook. That hook will be more efficient. In theory Kakoune could optimize the dynregex pattern the same way but that’s surely not worth the code complexity.

2 Likes

Thanks for that information !
Modify the snippet to work without dynregex :

hook -group todohighlight global BufCreate %opt{regular_buffers} %{
    evaluate-commands %sh{
    	printf '%s\n' "add-highlighter buffer/ regex '${kak_opt_comment_line}[ \t]*\b(TODO|FIXME|MAYBE)\b' 1:default+bu@comment"
    }
}

I’m still using BufCreate since WinSetOption triggers the hook multiple times if I have the same buffer opened in multiple windows.
I scope the highlighter to window/, but is there a good reason to repeat do so ?

Another update, the shell call is non-necessary here :

hook -group todohighlight global BufCreate %opt{regular_buffers} %{
    evaluate-commands "add-highlighter buffer/ regex '%opt{comment_line}[ \t]*\b(TODO|FIXME|MAYBE)\b' 1:default+bu@comment"
}
1 Like

For simplicity I would recommend to put admonition keywords in the rc scripts directly and make a PR. In most languages admonitions are a thing, e.g. in Crystal: Documenting code - Crystal

The current version of crystal.kak for comments looks like this:

add-highlighter shared/crystal/comment region '#(?!\{)' '$' fill comment

and you can patch like this:

add-highlighter shared/crystal/comment region '#(?!\{)' '$' group
add-highlighter shared/crystal/comment/ fill comment
add-highlighter shared/crystal/comment/ regex '\B`[#.]?\w+[?!]?`\B|\B(?<!\*)\*\w+[?!]?\*(?!\*)\B|\b(?:BUG|DEPRECATED|EXPERIMENTAL|FIXME|NOTE|OPTIMIZE|TODO|WARNING)\b|\B:(?:ditto|nodoc|inherit):$' 0:meta
1 Like

Yep, it would be interesting for Crystal, and languages that have admonitions as first-class citizen of their grammar, to put the highlight directly in its RC file.
However, some devs might not want TODO, MIGHT, etc… to be highlighted, even if the language they are working with uses admonitions.

Wouldn’t adding those rules directly in the language’s RC force them to disable the feature ?
IMO it’s best to have this as a to-enable feature rather than a to-disable one, and, the snippet I provided accomplish, badly, just that.

No need for the evaluate-commands

hook -group todohighlight global BufCreate %opt{regular_buffers} %{
    add-highlighter buffer/todo-highlight regex "%opt{comment_line}[ \t]*\b(TODO|FIXME|MAYBE)\b" 1:default+bu@comment
}