@@ -2,26 +2,33 @@ local M = {}
2
2
local notify = require (" rulebook.utils" ).notify
3
3
---- ----------------------------------------------------------------------------
4
4
5
- --- checks whether rule has id and source, as prescribed in nvim diagnostic structure
6
5
--- @param diag vim.Diagnostic
7
- --- @return boolean whether rule is valid
6
+ --- @return boolean
8
7
--- @nodiscard
9
- local function validDiagObj (diag )
8
+ local function sourceUsesCodes (diag )
10
9
local config = require (" rulebook.config" ).config
11
10
local sourcesWithNoCodes = vim .iter (config .ignoreComments )
12
11
:filter (function (_ , conf ) return conf .doesNotUseCodes end )
13
12
:map (function (linter , _ ) return linter end )
14
13
: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
16
23
17
24
local issuePlea =
18
25
" \n If 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
23
27
notify (" Diagnostic is missing a source." .. issuePlea , " warn" )
24
28
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
25
32
end
26
33
return true
27
34
end
56
63
57
64
--- @param diag vim.Diagnostic
58
65
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
59
71
if not validDiagObj (diag ) then return end
60
- local config = require (" rulebook.config" ).config
61
72
62
73
local indent = vim .api .nvim_get_current_line ():match (" ^%s*" )
63
- local ignoreComment = config . ignoreComments [ diag . source ] .comment
74
+ local ignoreComment = configForSource .comment
64
75
if type (ignoreComment ) == " function" then ignoreComment = ignoreComment (diag ) end
65
- local ignoreLocation = config . ignoreComments [ diag . source ] .location
76
+ local ignoreLocation = configForSource .location
66
77
67
78
-- insert the comment
68
79
if ignoreLocation == " prevLine" then
98
109
--- @param operation " lookupRule" | " ignoreRule" | " yankDiagnosticCode"
99
110
function M .selectRule (operation )
100
111
local config = require (" rulebook.config" ).config
101
- local operationIsIgnore = operation == " ignoreRule"
102
112
local lnum = vim .api .nvim_win_get_cursor (0 )[1 ] - 1
103
113
local startLine = lnum
104
114
local lastLine = vim .api .nvim_buf_line_count (0 )
@@ -107,24 +117,12 @@ function M.selectRule(operation)
107
117
-- loop through lines until we find a line with diagnostics
108
118
while true do
109
119
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
121
121
lnum = lnum + 1
122
122
123
123
-- GUARD
124
124
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 )
128
126
notify (msg , " warn" )
129
127
return
130
128
end
@@ -133,7 +131,7 @@ function M.selectRule(operation)
133
131
-- remove duplicate rules
134
132
local uniqueRule = {}
135
133
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
137
135
end
138
136
diagsAtLine = vim .tbl_values (uniqueRule )
139
137
@@ -145,25 +143,34 @@ function M.selectRule(operation)
145
143
146
144
-- select from multiple diagnostics
147
145
local title
148
- if operationIsIgnore then title = " Ignore Rule:" end
146
+ if operation == " ignoreRule " then title = " Ignore Rule:" end
149
147
if operation == " lookupRule" then title = " Lookup Rule:" end
150
148
if operation == " yankDiagnosticCode" then title = " Yank Diagnostic Code:" end
151
149
152
150
vim .ui .select (diagsAtLine , {
153
151
prompt = title ,
154
152
kind = " rule_selection" ,
155
153
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
160
167
return display
161
168
end ,
162
169
}, function (diag )
163
170
if not diag then return end -- user aborted
164
171
165
172
-- move cursor to location where we add the comment
166
- if operationIsIgnore and startLine ~= lnum then
173
+ if operation == " ignoreRule " and startLine ~= lnum then
167
174
vim .api .nvim_win_set_cursor (0 , { lnum + 1 , 0 })
168
175
vim .cmd (" normal! ^" )
169
176
end
0 commit comments