forked from nvimdev/guard.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlint.lua
More file actions
298 lines (258 loc) · 6.88 KB
/
lint.lua
File metadata and controls
298 lines (258 loc) · 6.88 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
local async = require('guard._async')
local api = vim.api
local util = require('guard.util')
local vd = vim.diagnostic
local ft = require('guard.filetype')
local M = {}
local ns = api.nvim_create_namespace('Guard')
local custom_ns = {}
---Execute command with stdin for linting
---@async
---@param cmd string[]
---@param cwd string
---@param config {env: table?, timeout: integer?}
---@param input string|string[]
---@return string output
---@return {code: integer, stderr: string, cmd: string}? error
local function exec_linter(cmd, cwd, config, input)
local result = async.await(1, function(callback)
local handle = vim.system(cmd, {
stdin = true,
cwd = cwd,
env = config.env,
timeout = config.timeout,
}, callback)
if type(input) == 'table' then
input = table.concat(input, '\n')
end
handle:write(input)
handle:write(nil)
end)
if result.code ~= 0 and #result.stderr > 0 then
return '', {
code = result.code,
stderr = result.stderr,
cmd = cmd[1],
}
end
return result.stdout, nil
end
---@param buf number?
function M.do_lint(buf)
buf = buf or api.nvim_get_current_buf()
local linters = util.eval(
vim.tbl_map(
util.toolcopy,
(vim.tbl_get(ft, vim.bo[buf].filetype, 'linter') or vim.tbl_get(ft, '*', 'linter'))
)
)
linters = vim.tbl_filter(function(config)
return util.should_run(config, buf)
end, linters)
async.run(function()
vd.reset(ns, buf)
for _, linter in ipairs(linters) do
M.do_lint_single(buf, linter)
end
end)
end
---@param buf number
---@param config LintConfig
function M.do_lint_single(buf, config)
local lint = util.eval1(config)
local custom = config.events ~= nil
local fname, cwd = util.buf_get_info(buf)
if not util.should_run(lint, buf) then
return
end
local prev_lines = api.nvim_buf_get_lines(buf, 0, -1, false)
if custom and not custom_ns[config] then
custom_ns[config] = api.nvim_create_namespace(tostring(config))
end
local cns = custom and custom_ns[config] or ns
if custom then
vd.reset(cns, buf)
end
local results = {}
local data = ''
if lint.cmd then
async.run(function()
local out, err = exec_linter(util.get_cmd(lint, fname, buf), cwd, lint, prev_lines)
if err then
vim.notify(
'[Guard]: ' .. ('%s exited with code %d\n%s'):format(err.cmd, err.code, err.stderr),
vim.log.levels.WARN
)
data = ''
else
data = out
end
if #data > 0 then
results = lint.parse(data, buf, fname, cwd)
end
vim.schedule(function()
if api.nvim_buf_is_valid(buf) and #results ~= 0 then
if not custom then
vim.list_extend(results, vd.get(buf))
end
vd.set(cns, buf, results)
end
end)
end)
else
data = lint.fn(prev_lines, fname, cwd)
if #data > 0 then
results = lint.parse(data, buf, fname, cwd)
end
vim.schedule(function()
if api.nvim_buf_is_valid(buf) and #results ~= 0 then
if not custom then
vim.list_extend(results, vd.get(buf))
end
vd.set(cns, buf, results)
end
end)
end
end
---@param buf number
---@param lnum_start number?
---@param lnum_end number?
---@param col_start number?
---@param col_end number?
---@param message string?
---@param severity number?
---@param source string?
---@param code string?
function M.diag_fmt(buf, lnum_start, col_start, message, severity, source, lnum_end, col_end, code)
return {
bufnr = buf,
col = col_start,
end_col = col_end or col_start,
lnum = lnum_start,
end_lnum = lnum_end or lnum_start,
message = message or '',
namespace = ns,
severity = severity or vim.diagnostic.severity.HINT,
source = source or 'Guard',
code = code,
}
end
local severities = {
error = 1,
warning = 2,
info = 3,
style = 4,
}
M.severities = severities
local from_opts = {
offset = 1,
source = nil,
severities = severities,
}
local regex_opts = {
regex = nil,
groups = { 'lnum', 'col', 'severity', 'code', 'message' },
}
local json_opts = {
get_diagnostics = function(...)
return vim.json.decode(...)
end,
attributes = {
lnum = 'line',
col = 'column',
message = 'message',
code = 'code',
severity = 'severity',
},
lines = nil,
}
local function attr_value(mes, attribute)
return type(attribute) == 'function' and attribute(mes) or mes[attribute]
end
---@param nr any?
---@param off number
local function normalize(nr, off)
if not nr or nr == '' then
return 0
else
return tonumber(nr) - off
end
end
local function json_get_offset(mes, attr, off)
return normalize(attr_value(mes, attr), off)
end
function M.from_json(opts)
opts = vim.tbl_deep_extend('force', from_opts, opts or {})
opts = vim.tbl_deep_extend('force', json_opts, opts)
return function(result, buf)
local diags, offences = {}, {}
if opts.lines then
vim.tbl_map(function(line)
local offence = opts.get_diagnostics(line)
if offence then
table.insert(offences, offence)
end
end, vim.split(result, '\r?\n', { trimempty = true }))
else
offences = opts.get_diagnostics(result)
end
local attr = opts.attributes
local off = opts.offset
vim.tbl_map(function(mes)
local message = attr_value(mes, attr.message)
local code = attr_value(mes, attr.code)
table.insert(
diags,
M.diag_fmt(
buf,
json_get_offset(mes, attr.lnum, off),
json_get_offset(mes, attr.col, off),
message,
opts.severities[attr_value(mes, attr.severity)],
opts.source,
json_get_offset(mes, attr.lnum_end or attr.lnum, off),
json_get_offset(mes, attr.col_end or attr.col, off),
code
)
)
end, offences or {})
return diags
end
end
function M.from_regex(opts)
opts = vim.tbl_deep_extend('force', from_opts, opts or {})
opts = vim.tbl_deep_extend('force', regex_opts, opts)
return function(result, buf)
local diags, offences = {}, {}
local lines = vim.split(result, '\r?\n', { trimempty = true })
for _, line in ipairs(lines) do
local offence = {}
local matches = { line:match(opts.regex) }
if #matches == #opts.groups then
for i = 1, #opts.groups do
offence[opts.groups[i]] = matches[i]
end
table.insert(offences, offence)
end
end
local off = opts.offset
vim.tbl_map(function(mes)
table.insert(
diags,
M.diag_fmt(
buf,
normalize(mes.lnum, off),
normalize(mes.col, off),
mes.message,
opts.severities[mes.severity],
opts.source,
normalize(mes.lnum_end or mes.lnum, off),
normalize(mes.col_end or mes.lnum, off),
mes.code
)
)
end, offences)
return diags
end
end
return M