Skip to content

Commit e7e50f8

Browse files
committed
feat(highlight): allow complex keyword patterns (#185)
Match only the (KEYWORDS) portion of patterns for looking up highlight information. Allows highlighting more complicated patterns, like `NOTE(LunarLambda):` or `TODO 2:`.
1 parent 833d8dd commit e7e50f8

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

lua/todo-comments/config.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,12 @@ function M._setup()
123123
return M.options.search.pattern:gsub("KEYWORDS", tags(keywords))
124124
end
125125

126+
M.kw_regex = tags()
126127
M.hl_regex = {}
127128
local patterns = M.options.highlight.pattern
128129
patterns = type(patterns) == "table" and patterns or { patterns }
129130
for _, p in pairs(patterns) do
130-
p = p:gsub("KEYWORDS", tags())
131+
p = p:gsub("KEYWORDS", M.kw_regex)
131132
table.insert(M.hl_regex, p)
132133
end
133134
M.colors()

lua/todo-comments/highlight.lua

+7-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function M.match(str, patterns)
4747
local m = vim.fn.matchlist(str, [[\v\C]] .. pattern)
4848
if #m > 1 and m[2] then
4949
local kw = m[2]
50-
local start = str:find(kw)
50+
local start = str:find(kw, 0, true)
5151
return start, start + #kw, kw
5252
end
5353
end
@@ -192,6 +192,12 @@ function M.highlight(buf, first, last, _event)
192192
end
193193
end
194194

195+
local sub_kw = vim.fn.matchstr(kw, [[\v\C]] .. Config.kw_regex)
196+
197+
if sub_kw ~= "" then
198+
kw = sub_kw
199+
end
200+
195201
if kw then
196202
kw = Config.keywords[kw] or kw
197203
end

0 commit comments

Comments
 (0)