Skip to content

Commit e09a8c6

Browse files
authored
feat: expose diagnostic.code in from_json & from_regex (#244)
* feat: expose diagnostic.code in from_json & from_regex * fix: tests
1 parent c1d2b66 commit e09a8c6

3 files changed

Lines changed: 16 additions & 13 deletions

File tree

lua/guard/lint.lua

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ end
141141
---@param message string?
142142
---@param severity number?
143143
---@param source string?
144-
function M.diag_fmt(buf, lnum_start, col_start, message, severity, source, lnum_end, col_end)
144+
---@param code string?
145+
function M.diag_fmt(buf, lnum_start, col_start, message, severity, source, lnum_end, col_end, code)
145146
return {
146147
bufnr = buf,
147148
col = col_start,
@@ -152,6 +153,7 @@ function M.diag_fmt(buf, lnum_start, col_start, message, severity, source, lnum_
152153
namespace = ns,
153154
severity = severity or vim.diagnostic.severity.HINT,
154155
source = source or 'Guard',
156+
code = code,
155157
}
156158
end
157159

@@ -188,10 +190,6 @@ local json_opts = {
188190
lines = nil,
189191
}
190192

191-
local function formulate_msg(msg, code)
192-
return (msg or '') .. (code and ('[%s]'):format(code) or '')
193-
end
194-
195193
local function attr_value(mes, attribute)
196194
return type(attribute) == 'function' and attribute(mes) or mes[attribute]
197195
end
@@ -239,11 +237,12 @@ function M.from_json(opts)
239237
buf,
240238
json_get_offset(mes, attr.lnum, off),
241239
json_get_offset(mes, attr.col, off),
242-
formulate_msg(message, code),
240+
message,
243241
opts.severities[attr_value(mes, attr.severity)],
244242
opts.source,
245243
json_get_offset(mes, attr.lnum_end or attr.lnum, off),
246-
json_get_offset(mes, attr.col_end or attr.col, off)
244+
json_get_offset(mes, attr.col_end or attr.col, off),
245+
code
247246
)
248247
)
249248
end, offences or {})
@@ -282,11 +281,12 @@ function M.from_regex(opts)
282281
buf,
283282
normalize(mes.lnum, off),
284283
normalize(mes.col, off),
285-
formulate_msg(mes.message, mes.code),
284+
mes.message,
286285
opts.severities[mes.severity],
287286
opts.source,
288287
normalize(mes.lnum_end or mes.lnum, off),
289-
normalize(mes.col_end or mes.lnum, off)
288+
normalize(mes.col_end or mes.lnum, off),
289+
mes.code
290290
)
291291
)
292292
end, offences)

spec/lint_spec.lua

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ describe('lint module', function()
112112
end_col = 1,
113113
lnum = 1,
114114
end_lnum = 1,
115-
message = 'Very important error message[error code 114514]',
115+
message = 'Very important error message',
116+
code = 'error code 114514',
116117
namespace = ns,
117118
severity = 2,
118119
},
@@ -136,7 +137,8 @@ describe('lint module', function()
136137
end_col = 1,
137138
lnum = 1,
138139
end_lnum = 1,
139-
message = 'Very important error message[error code 114514]',
140+
message = 'Very important error message',
141+
code = 'error code 114514',
140142
namespace = ns,
141143
severity = 2,
142144
},
@@ -146,7 +148,8 @@ describe('lint module', function()
146148
end_col = 0,
147149
end_lnum = 0,
148150
lnum = 0,
149-
message = 'Very important error message[warning]',
151+
message = 'Very important error message',
152+
code = 'warning',
150153
namespace = ns,
151154
severity = 2,
152155
source = 'mock_linter_json',

spec/settings_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ describe('settings', function()
150150
same(true, util.getopt('auto_lint'))
151151
vim.cmd('silent! write!')
152152
vim.wait(500)
153-
same('Very important error message[error code 114514]', vd.get()[1].message)
153+
same('Very important error message', vd.get()[1].message)
154154

155155
vim.g.guard_config = { auto_lint = false }
156156
same(false, util.getopt('auto_lint'))

0 commit comments

Comments
 (0)