Skip to content

Commit 44d954d

Browse files
committed
improv(ignoreComment): more helpful info if config/code/source missing
1 parent f08cee7 commit 44d954d

File tree

1 file changed

+40
-33
lines changed

1 file changed

+40
-33
lines changed

lua/rulebook/diagnostic-actions.lua

+40-33
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,33 @@ local M = {}
22
local notify = require("rulebook.utils").notify
33
--------------------------------------------------------------------------------
44

5-
---checks whether rule has id and source, as prescribed in nvim diagnostic structure
65
---@param diag vim.Diagnostic
7-
---@return boolean whether rule is valid
6+
---@return boolean
87
---@nodiscard
9-
local function validDiagObj(diag)
8+
local function sourceUsesCodes(diag)
109
local config = require("rulebook.config").config
1110
local sourcesWithNoCodes = vim.iter(config.ignoreComments)
1211
:filter(function(_, conf) return conf.doesNotUseCodes end)
1312
:map(function(linter, _) return linter end)
1413
:totable()
15-
if vim.tbl_contains(sourcesWithNoCodes, diag.source) then return true end
14+
return not vim.tbl_contains(sourcesWithNoCodes, diag.source)
15+
end
16+
17+
---checks whether rule has id and source, as prescribed in nvim diagnostic structure
18+
---@param diag vim.Diagnostic
19+
---@return boolean
20+
---@nodiscard
21+
local function validDiagObj(diag)
22+
if not sourceUsesCodes(diag) then return true end
1623

1724
local issuePlea =
1825
"\nIf you are using efm or nvim-lint, please check your linter config. Otherwise open an issue at the diagnostic source or the diagnostic provider."
19-
if not diag.code then
20-
notify("Diagnostic is missing a code (rule id)." .. issuePlea, "warn")
21-
return false
22-
elseif not diag.source then
26+
if not diag.source then
2327
notify("Diagnostic is missing a source." .. issuePlea, "warn")
2428
return false
29+
elseif not diag.code and sourceUsesCodes(diag) then
30+
notify("Diagnostic is missing a code (rule id)." .. issuePlea, "warn")
31+
return false
2532
end
2633
return true
2734
end
@@ -56,13 +63,17 @@ end
5663

5764
---@param diag vim.Diagnostic
5865
function M.ignoreRule(diag)
66+
local configForSource = require("rulebook.config").config.ignoreComments[diag.source]
67+
if not configForSource then
68+
notify(("No ignore comment configured for %q."):format(diag.source), "warn")
69+
return
70+
end
5971
if not validDiagObj(diag) then return end
60-
local config = require("rulebook.config").config
6172

6273
local indent = vim.api.nvim_get_current_line():match("^%s*")
63-
local ignoreComment = config.ignoreComments[diag.source].comment
74+
local ignoreComment = configForSource.comment
6475
if type(ignoreComment) == "function" then ignoreComment = ignoreComment(diag) end
65-
local ignoreLocation = config.ignoreComments[diag.source].location
76+
local ignoreLocation = configForSource.location
6677

6778
-- insert the comment
6879
if ignoreLocation == "prevLine" then
@@ -98,7 +109,6 @@ end
98109
---@param operation "lookupRule"|"ignoreRule"|"yankDiagnosticCode"
99110
function M.selectRule(operation)
100111
local config = require("rulebook.config").config
101-
local operationIsIgnore = operation == "ignoreRule"
102112
local lnum = vim.api.nvim_win_get_cursor(0)[1] - 1
103113
local startLine = lnum
104114
local lastLine = vim.api.nvim_buf_line_count(0)
@@ -107,24 +117,12 @@ function M.selectRule(operation)
107117
-- loop through lines until we find a line with diagnostics
108118
while true do
109119
diagsAtLine = vim.diagnostic.get(0, { lnum = lnum })
110-
if operationIsIgnore then
111-
-- INFO for rule search, there is no need to filter the diagnostics, since there
112-
-- is a fallback mechanic
113-
diagsAtLine = vim.tbl_filter(
114-
function(d) return config.ignoreComments[d.source] ~= nil end,
115-
diagsAtLine
116-
)
117-
end
118-
119-
-- no diagnostic -> search the next lines
120-
if #diagsAtLine > 0 then break end
120+
if #diagsAtLine > 0 then break end -- no diagnostic -> search the next lines
121121
lnum = lnum + 1
122122

123123
-- GUARD
124124
if lnum > lastLine or lnum > startLine + config.forwSearchLines then
125-
local msg = ("No supported diagnostics found in the next %s lines."):format(
126-
config.forwSearchLines
127-
)
125+
local msg = ("No diagnostics found in the next %s lines."):format(config.forwSearchLines)
128126
notify(msg, "warn")
129127
return
130128
end
@@ -133,7 +131,7 @@ function M.selectRule(operation)
133131
-- remove duplicate rules
134132
local uniqueRule = {}
135133
for _, diag in ipairs(diagsAtLine) do
136-
uniqueRule[diag.source .. (diag.code or diag.message)] = diag
134+
uniqueRule[(diag.source or "") .. (diag.code or diag.message)] = diag
137135
end
138136
diagsAtLine = vim.tbl_values(uniqueRule)
139137

@@ -145,25 +143,34 @@ function M.selectRule(operation)
145143

146144
-- select from multiple diagnostics
147145
local title
148-
if operationIsIgnore then title = "Ignore Rule:" end
146+
if operation == "ignoreRule" then title = "Ignore Rule:" end
149147
if operation == "lookupRule" then title = "Lookup Rule:" end
150148
if operation == "yankDiagnosticCode" then title = "Yank Diagnostic Code:" end
151149

152150
vim.ui.select(diagsAtLine, {
153151
prompt = title,
154152
kind = "rule_selection",
155153
format_item = function(diag)
156-
local source = diag.source and diag.source .. ": " or ""
157-
local desc = diag.code or diag.message
158-
local display = source .. desc
159-
if not (diag.code and diag.source) then display = "" .. display end
154+
local msg = vim.trim((diag.message or ""):sub(1, 50))
155+
if not diag.source then return ("[ No source] %s %s"):format(diag.code or "", msg) end
156+
157+
local display = diag.source .. ": " .. (diag.code or msg)
158+
if operation == "ignoreRule" then
159+
local configForSource = require("rulebook.config").config.ignoreComments[diag.source]
160+
if not configForSource then display = "[ No config] " .. display end
161+
if sourceUsesCodes(diag) and not diag.code then
162+
display = "[ No code] " .. display
163+
end
164+
elseif not diag.code then
165+
display = "[ No code] " .. display
166+
end
160167
return display
161168
end,
162169
}, function(diag)
163170
if not diag then return end -- user aborted
164171

165172
-- move cursor to location where we add the comment
166-
if operationIsIgnore and startLine ~= lnum then
173+
if operation == "ignoreRule" and startLine ~= lnum then
167174
vim.api.nvim_win_set_cursor(0, { lnum + 1, 0 })
168175
vim.cmd("normal! ^")
169176
end

0 commit comments

Comments
 (0)