Skip to content

Commit 0a886b9

Browse files
authored
Add option to show diagnostic code for diagnostics (#171)
This matches the behavior of the built-in diagnostic display. It makes it much easier to disable specific diagnostic codes from linters and type checkers when they need to be overridden.
1 parent c7b1488 commit 0a886b9

6 files changed

Lines changed: 54 additions & 2 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ require("tiny-inline-diagnostic").setup({
161161
if_many = false, -- Only show source if multiple sources exist for the same diagnostic
162162
},
163163

164+
-- Display the diagnostic code of diagnostics (e.g., "F401", "no-dupe-args")
165+
show_code = true,
166+
164167
-- Use icons from vim.diagnostic.config instead of preset icons
165168
use_icons_from_diagnostic = false,
166169

doc/tiny-inline-diagnostic.nvim.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ CONFIGURATION*tiny-inline-diagnostic.nvim-tiny-inline-diagnostic.nvim-configurat
186186
enabled = false, -- Enable showing source names
187187
if_many = false, -- Only show source if multiple sources exist for the same diagnostic
188188
},
189+
190+
-- Display the diagnostic code of diagnostics (e.g., "F401", "no-dupe-args")
191+
show_code = true,
189192

190193
-- Use icons from vim.diagnostic.config instead of preset icons
191194
use_icons_from_diagnostic = false,

lua/tiny-inline-diagnostic/chunk.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@ function M.get_chunks(opts, diags_on_line, diag_index, diag_line, cursor_line, b
411411
show_source = true
412412
end
413413

414+
local show_code = opts.options.show_code
415+
414416
local diag_message = diag.message
415417
if diag.is_related then
416418
local location_info = ""
@@ -426,8 +428,13 @@ function M.get_chunks(opts, diags_on_line, diag_index, diag_line, cursor_line, b
426428
end
427429
end
428430
diag_message = diag_message .. location_info
429-
elseif show_source and diag.source then
430-
diag_message = diag_message .. " (" .. diag.source .. ")"
431+
else
432+
if show_code and diag.code then
433+
diag_message = diag_message .. " [" .. diag.code .. "]"
434+
end
435+
if show_source and diag.source then
436+
diag_message = diag_message .. " (" .. diag.source .. ")"
437+
end
431438
end
432439

433440
local chunks = { diag_message }

lua/tiny-inline-diagnostic/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ local default_config = {
2828
enabled = false,
2929
if_many = false,
3030
},
31+
show_code = true,
3132
show_related = {
3233
enabled = true,
3334
max_count = 3,

tests/test_chunk.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ T["get_chunks"]["returns chunk info for diagnostic"] = function()
288288
softwrap = 10,
289289
break_line = { enabled = false },
290290
show_source = { enabled = false },
291+
show_code = false,
291292
},
292293
})
293294
local diags = {
@@ -312,6 +313,7 @@ T["get_chunks"]["includes source when show_source is enabled"] = function()
312313
softwrap = 10,
313314
break_line = { enabled = false },
314315
show_source = { enabled = true },
316+
show_code = false,
315317
},
316318
})
317319
local diags = {
@@ -326,6 +328,35 @@ T["get_chunks"]["includes source when show_source is enabled"] = function()
326328
local result = chunk.get_chunks(opts, diags, 1, 0, 0, buf)
327329
local full_message = table.concat(result.chunks, "")
328330
MiniTest.expect.equality(full_message:find("test_lsp") ~= nil, true)
331+
MiniTest.expect.equality(full_message:find("E308") ~= nil, false)
332+
end)
333+
end
334+
335+
T["get_chunks"]["includes code when show_code is enabled"] = function()
336+
H.with_buf({ "test line" }, function(buf)
337+
local opts = H.make_opts({
338+
options = {
339+
overflow = { mode = "none" },
340+
multilines = { enabled = false },
341+
softwrap = 10,
342+
break_line = { enabled = false },
343+
show_source = { enabled = true },
344+
show_code = true,
345+
},
346+
})
347+
local diags = {
348+
{
349+
message = "test error",
350+
severity = vim.diagnostic.severity.ERROR,
351+
lnum = 0,
352+
source = "test_lsp",
353+
code = "E308",
354+
},
355+
}
356+
357+
local result = chunk.get_chunks(opts, diags, 1, 0, 0, buf)
358+
local full_message = table.concat(result.chunks, "")
359+
MiniTest.expect.equality(full_message:find("%[E308%] %(test_lsp%)") ~= nil, true)
329360
end)
330361
end
331362

@@ -339,6 +370,7 @@ T["get_chunks"]["sets need_to_be_under for long lines"] = function()
339370
softwrap = 10,
340371
break_line = { enabled = false },
341372
show_source = { enabled = false },
373+
show_code = false,
342374
},
343375
})
344376
local diags = {
@@ -360,6 +392,7 @@ T["get_chunks"]["does not set need_to_be_under for short lines"] = function()
360392
softwrap = 10,
361393
break_line = { enabled = false },
362394
show_source = { enabled = false },
395+
show_code = false,
363396
},
364397
})
365398
local diags = {
@@ -381,6 +414,7 @@ T["get_chunks"]["uses display width not byte length"] = function()
381414
softwrap = 5,
382415
break_line = { enabled = false },
383416
show_source = { enabled = false },
417+
show_code = false,
384418
},
385419
})
386420
local diags = {
@@ -402,6 +436,7 @@ T["get_chunks"]["uses virtcol when cursor on diagnostic line"] = function()
402436
softwrap = 10,
403437
break_line = { enabled = false },
404438
show_source = { enabled = false },
439+
show_code = false,
405440
},
406441
})
407442
local diags = {
@@ -423,6 +458,7 @@ T["get_chunks"]["accounts for window width in wrapping decision"] = function()
423458
softwrap = 10,
424459
break_line = { enabled = false },
425460
show_source = { enabled = false },
461+
show_code = false,
426462
},
427463
})
428464
local diags = {
@@ -444,6 +480,7 @@ T["get_chunks"]["wraps when line exceeds window width minus softwrap"] = functio
444480
softwrap = 5,
445481
break_line = { enabled = false },
446482
show_source = { enabled = false },
483+
show_code = false,
447484
},
448485
})
449486
local diags = {

tests/test_diagnostic.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ local function create_test_opts()
1212
use_icons_from_diagnostic = false,
1313
set_arrow_to_diag_color = false,
1414
show_source = { enabled = false },
15+
show_code = false,
1516
overflow = { mode = "none" },
1617
break_line = { enabled = false },
1718
multilines = { enabled = false },

0 commit comments

Comments
 (0)