-
Notifications
You must be signed in to change notification settings - Fork 94
/
config.lua
193 lines (173 loc) · 6.56 KB
/
config.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
local Util = require("todo-comments.util")
--- @class TodoConfig
local M = {}
M.keywords = {}
--- @type TodoOptions
M.options = {}
M.loaded = false
M.ns = vim.api.nvim_create_namespace("todo-comments")
--- @class TodoOptions
-- TODO: add support for markdown todos
local defaults = {
signs = true, -- show icons in the signs column
sign_priority = 8, -- sign priority
-- keywords recognized as todo comments
keywords = {
FIX = {
icon = " ", -- icon used for the sign, and in search results
color = "error", -- can be a hex color, or a named color (see below)
alt = { "FIXME", "BUG", "FIXIT", "ISSUE" }, -- a set of other keywords that all map to this FIX keywords
-- signs = false, -- configure signs for some keywords individually
},
TODO = { icon = " ", color = "info" },
HACK = { icon = " ", color = "warning" },
WARN = { icon = " ", color = "warning", alt = { "WARNING", "XXX" } },
PERF = { icon = " ", alt = { "OPTIM", "PERFORMANCE", "OPTIMIZE" } },
NOTE = { icon = " ", color = "hint", alt = { "INFO" } },
TEST = { icon = "⏲ ", color = "test", alt = { "TESTING", "PASSED", "FAILED" } },
},
gui_style = {
fg = "NONE", -- The gui style to use for the fg highlight group.
bg = "BOLD", -- The gui style to use for the bg highlight group.
},
merge_keywords = true, -- when true, custom keywords will be merged with the defaults
-- highlighting of the line containing the todo comment
-- * before: highlights before the keyword (typically comment characters)
-- * keyword: highlights of the keyword
-- * after: highlights after the keyword (todo text)
highlight = {
multiline = true, -- enable multine todo comments
multiline_pattern = "^.", -- lua pattern to match the next multiline from the start of the matched keyword
multiline_context = 10, -- extra lines that will be re-evaluated when changing a line
before = "", -- "fg" or "bg" or empty
keyword = "wide", -- "fg", "bg", "wide" or empty. (wide is the same as bg, but will also highlight surrounding characters)
after = "fg", -- "fg" or "bg" or empty
-- pattern can be a string, or a table of regexes that will be checked
pattern = [[.*<(KEYWORDS)\s*:]], -- pattern or table of patterns, used for highlightng (vim regex)
-- pattern = { [[.*<(KEYWORDS)\s*:]], [[.*\@(KEYWORDS)\s*]] }, -- pattern used for highlightng (vim regex)
comments_only = true, -- uses treesitter to match keywords in comments only
max_line_len = 400, -- ignore lines longer than this
exclude = {}, -- list of file types to exclude highlighting
throttle = 200,
},
-- list of named colors where we try to extract the guifg from the
-- list of hilight groups or use the hex color if hl not found as a fallback
colors = {
error = { "DiagnosticError", "ErrorMsg", "#DC2626" },
warning = { "DiagnosticWarn", "WarningMsg", "#FBBF24" },
info = { "DiagnosticInfo", "#2563EB" },
hint = { "DiagnosticHint", "#10B981" },
default = { "Identifier", "#7C3AED" },
test = { "Identifier", "#FF00FF" },
},
search = {
command = "rg",
args = {
"--color=never",
"--no-heading",
"--with-filename",
"--line-number",
"--column",
},
-- regex that will be used to match keywords.
-- don't replace the (KEYWORDS) placeholder
pattern = [[\b(KEYWORDS):]], -- ripgrep regex
-- pattern = [[\b(KEYWORDS)\b]], -- match without the extra colon. You'll likely get false positives
},
}
M._options = nil
function M.setup(options)
if vim.fn.has("nvim-0.8.0") == 0 then
error("todo-comments needs Neovim >= 0.8.0. Use the 'neovim-pre-0.8.0' branch for older versions")
end
M._options = options
if vim.api.nvim_get_vvar("vim_did_enter") == 0 then
vim.defer_fn(function()
M._setup()
end, 0)
else
M._setup()
end
end
function M._setup()
M.options = vim.tbl_deep_extend("force", {}, defaults, M.options or {}, M._options or {})
-- -- keywords should always be fully overriden
if M._options and M._options.keywords and M._options.merge_keywords == false then
M.options.keywords = M._options.keywords
end
for kw, opts in pairs(M.options.keywords) do
M.keywords[kw] = kw
for _, alt in pairs(opts.alt or {}) do
M.keywords[alt] = kw
end
end
local function tags(keywords)
local kws = keywords or vim.tbl_keys(M.keywords)
table.sort(kws, function(a, b)
return #b < #a
end)
return table.concat(kws, "|")
end
function M.search_regex(keywords)
return M.options.search.pattern:gsub("KEYWORDS", tags(keywords))
end
M.hl_regex = {}
local patterns = M.options.highlight.pattern
patterns = type(patterns) == "table" and patterns or { patterns }
for _, p in pairs(patterns) do
p = p:gsub("KEYWORDS", tags())
table.insert(M.hl_regex, p)
end
M.colors()
M.signs()
require("todo-comments.highlight").start()
M.loaded = true
end
function M.signs()
for kw, opts in pairs(M.options.keywords) do
vim.fn.sign_define("todo-sign-" .. kw, {
text = opts.icon,
texthl = "TodoSign" .. kw,
})
end
end
function M.colors()
local normal = Util.get_hl("Normal")
local fg_dark = Util.is_dark(normal.foreground or "#ffffff") and normal.foreground or normal.background
local fg_light = Util.is_dark(normal.foreground or "#ffffff") and normal.background or normal.foreground
fg_dark = fg_dark or "#000000"
fg_light = fg_light or "#ffffff"
local fg_gui = M.options.gui_style.fg
local bg_gui = M.options.gui_style.bg
local sign_hl = Util.get_hl("SignColumn")
local sign_bg = (sign_hl and sign_hl.background) and sign_hl.background or "NONE"
for kw, opts in pairs(M.options.keywords) do
local kw_color = opts.color or "default"
local hex
if kw_color:sub(1, 1) == "#" then
hex = kw_color
else
local colors = M.options.colors[kw_color]
colors = type(colors) == "string" and { colors } or colors
for _, color in pairs(colors) do
if color:sub(1, 1) == "#" then
hex = color
break
end
local c = Util.get_hl(color)
if c and c.foreground then
hex = c.foreground
break
end
end
end
if not hex then
error("Todo: no color for " .. kw)
end
local fg = Util.is_dark(hex) and fg_light or fg_dark
vim.cmd("hi def TodoBg" .. kw .. " guibg=" .. hex .. " guifg=" .. fg .. " gui=" .. bg_gui)
vim.cmd("hi def TodoFg" .. kw .. " guibg=NONE guifg=" .. hex .. " gui=" .. fg_gui)
vim.cmd("hi def TodoSign" .. kw .. " guibg=" .. sign_bg .. " guifg=" .. hex .. " gui=NONE")
end
end
return M