Skip to content

Commit 55bbf64

Browse files
fix: diag_fmt fallback logic, revert early formatter check (#208)
1 parent f732ae5 commit 55bbf64

File tree

3 files changed

+50
-38
lines changed

3 files changed

+50
-38
lines changed

lua/guard/events.lua

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ local function debounced_lint(opt)
1818
end
1919
---@diagnostic disable-next-line: undefined-field
2020
debounce_timer = assert(uv.new_timer()) --[[uv_timer_t]]
21-
debounce_timer:start(util.getopt('lint_interval'), 0, function()
21+
---@type integer
22+
local interval = assert(tonumber(util.getopt('lint_interval')))
23+
debounce_timer:start(interval, 0, function()
2224
debounce_timer:stop()
2325
debounce_timer:close()
2426
debounce_timer = nil
@@ -89,22 +91,14 @@ end
8991

9092
---@param buf number
9193
---@return boolean
94+
--- We don't check ignore patterns here because users might expect
95+
--- other formatters in the same group to run even if another
96+
--- might ignore this file
9297
function M.check_fmt_should_attach(buf)
93-
local fmts = require('guard.filetype')[vim.bo[buf].filetype].formatter
94-
local bufname = api.nvim_buf_get_name(buf)
95-
-- check if it's not attached already and has an underlying file
98+
-- check if it's not attached already
9699
return #M.get_format_autocmds(buf) == 0
100+
-- and has an underlying file
97101
and vim.bo[buf].buftype ~= 'nofile'
98-
and iter(fmts):any(function(item)
99-
if type(item) == 'table' then
100-
for _, pat in ipairs(util.as_table(item.ignore_patterns) or {}) do
101-
if bufname:find(pat) then
102-
return false
103-
end
104-
end
105-
end
106-
return true
107-
end)
108102
end
109103

110104
---@param buf number

lua/guard/lint.lua

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ end
101101
function M.diag_fmt(buf, lnum_start, col_start, message, severity, source, lnum_end, col_end)
102102
return {
103103
bufnr = buf,
104-
col = col_start or 0,
104+
col = col_start,
105105
end_col = col_end or col_start,
106-
lnum = lnum_start or 0,
106+
lnum = lnum_start,
107107
end_lnum = lnum_end or lnum_start,
108108
message = message or '',
109109
namespace = ns,
@@ -153,6 +153,16 @@ local function attr_value(mes, attribute)
153153
return type(attribute) == 'function' and attribute(mes) or mes[attribute]
154154
end
155155

156+
---@param nr any?
157+
---@param off number
158+
local function normalize(nr, off)
159+
return tonumber(nr or off) - off
160+
end
161+
162+
local function json_get_offset(mes, attr, off)
163+
return normalize(attr_value(mes, attr), off)
164+
end
165+
156166
function M.from_json(opts)
157167
opts = vim.tbl_deep_extend('force', from_opts, opts or {})
158168
opts = vim.tbl_deep_extend('force', json_opts, opts)
@@ -165,26 +175,30 @@ function M.from_json(opts)
165175
vim.tbl_map(function(line)
166176
local offence = opts.get_diagnostics(line)
167177
if offence then
168-
offences[#offences + 1] = offence
178+
table.insert(offences, offence)
169179
end
170180
end, vim.split(result, '\r?\n', { trimempty = true }))
171181
else
172182
offences = opts.get_diagnostics(result)
173183
end
174184

185+
local attr = opts.attributes
186+
local off = opts.offset
175187
vim.tbl_map(function(mes)
176-
local attr = opts.attributes
177188
local message = attr_value(mes, attr.message)
178189
local code = attr_value(mes, attr.code)
179-
diags[#diags + 1] = M.diag_fmt(
180-
buf,
181-
tonumber(attr_value(mes, attr.lnum)) - opts.offset,
182-
tonumber(attr_value(mes, attr.col)) - opts.offset,
183-
formulate_msg(message, code),
184-
opts.severities[attr_value(mes, attr.severity)],
185-
opts.source,
186-
tonumber(attr_value(mes, attr.lnum_end or attr.lnum)) - opts.offset,
187-
tonumber(attr_value(mes, attr.col_end or attr.lnum)) - opts.offset
190+
table.insert(
191+
diags,
192+
M.diag_fmt(
193+
buf,
194+
json_get_offset(mes, attr.lnum, off),
195+
json_get_offset(mes, attr.col, off),
196+
formulate_msg(message, code),
197+
opts.severities[attr_value(mes, attr.severity)],
198+
opts.source,
199+
json_get_offset(mes, attr.lnum_end or attr.lnum, off),
200+
json_get_offset(mes, attr.col_end or attr.col, off)
201+
)
188202
)
189203
end, offences or {})
190204

@@ -212,20 +226,24 @@ function M.from_regex(opts)
212226
offence[opts.groups[i]] = matches[i]
213227
end
214228

215-
offences[#offences + 1] = offence
229+
table.insert(offences, offence)
216230
end
217231
end
218232

233+
local off = opts.offset
219234
vim.tbl_map(function(mes)
220-
diags[#diags + 1] = M.diag_fmt(
221-
buf,
222-
tonumber(mes.lnum) - opts.offset,
223-
tonumber(mes.col) - opts.offset,
224-
formulate_msg(mes.message, mes.code),
225-
opts.severities[mes.severity],
226-
opts.source,
227-
tonumber(mes.lnum_end or mes.lnum) - opts.offset,
228-
tonumber(mes.col_end or mes.col) - opts.offset
235+
table.insert(
236+
diags,
237+
M.diag_fmt(
238+
buf,
239+
normalize(mes.lnum, off),
240+
normalize(mes.col, off),
241+
formulate_msg(mes.message, mes.code),
242+
opts.severities[mes.severity],
243+
opts.source,
244+
normalize(mes.lnum_end or mes.lnum, off),
245+
normalize(mes.col_end or mes.lnum, off)
246+
)
229247
)
230248
end, offences)
231249

lua/guard/util.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function M.as_table(t)
2020
return vim.islist(t) and t or { t }
2121
end
2222

23-
---@source runtime/lua/vim/lsp/buf.lua
23+
---@see runtime/lua/vim/lsp/buf.lua
2424
---@param bufnr integer
2525
---@param mode "v"|"V"
2626
---@return table {start={row,col}, end={row,col}} using (1, 0) indexing

0 commit comments

Comments
 (0)