Skip to content

Commit

Permalink
feat: add multiline diagnostic support (#305)
Browse files Browse the repository at this point in the history
* multiline support

* Do not change single line diagnostics

* Just show icon in header line

* style: lua annotations

* feat: optional multiline

---------

Co-authored-by: Folke Lemaitre <folke.lemaitre@gmail.com>
  • Loading branch information
MariaSolOs and folke committed Jul 22, 2023
1 parent 20d1b30 commit 7a6abd7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Trouble comes with the following defaults:
previous = "k", -- previous item
next = "j" -- next item
},
multiline = false, -- render multi-line messages
indent_lines = true, -- add an indent guide below the fold icons
auto_open = false, -- automatically open the list when you have diagnostics
auto_close = false, -- automatically close the list when you have no diagnostics
Expand Down
1 change: 1 addition & 0 deletions lua/trouble/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ local defaults = {
previous = "k", -- preview item
next = "j", -- next item
},
multiline = false, -- render multi-line messages
indent_lines = true, -- add an indent guide below the fold icons
auto_open = false, -- automatically open the list when you have diagnostics
auto_close = false, -- automatically close the list when you have no diagnostics
Expand Down
15 changes: 13 additions & 2 deletions lua/trouble/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ function renderer.render_diagnostics(view, text, items)

text:render(indent, "Indent")
text:render(sign .. " ", sign_hl, { exact = true })
text:render(diag.text, "Text" .. diag.type, " ")
-- text:render(diag.type, diag.type, " ")

local lines = config.options.multiline and vim.split(diag.full_text, "\n") or { diag.text }

text:render(lines[1], "Text" .. diag.type, " ")

if diag.source then
text:render(diag.source, "Source")
Expand All @@ -155,6 +157,15 @@ function renderer.render_diagnostics(view, text, items)
text:render(" ")

text:render("[" .. diag.lnum .. ", " .. diag.col .. "]", "Location")

for l = 2, #lines do
local str = lines[l]
text:nl()
view.items[text.lineNr + 1] = diag
text:render(indent .. " ", "Indent")
text:render(str, "Text" .. diag.type, " ")
end

text:nl()
end
end
Expand Down
18 changes: 9 additions & 9 deletions lua/trouble/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function M.fix_mode(opts)
end
end

---@return number
function M.count(tab)
local count = 0
for _ in pairs(tab) do
Expand Down Expand Up @@ -143,8 +144,8 @@ function M.process_item(item, bufnr)
if finish.character == nil or finish.line == nil then
M.error("Found an item for Trouble without finish range " .. vim.inspect(finish))
end
local row = start.line
local col = start.character
local row = start.line ---@type number
local col = start.character ---@type number

if not item.message and filename then
-- check if the filename is a uri
Expand All @@ -167,22 +168,21 @@ function M.process_item(item, bufnr)
---@class Item
---@field is_file boolean
---@field fixed boolean
local ret
ret = {
local ret = {
bufnr = bufnr,
filename = filename,
lnum = row + 1,
col = col + 1,
start = start,
finish = finish,
sign = item.sign,
sign_hl = item.sign_hl,
sign = item.sign, ---@type string?
sign_hl = item.sign_hl, ---@type string?
-- remove line break to avoid display issues
text = vim.trim(item.message:gsub("[\n]", "")):sub(0, vim.o.columns),
text = vim.trim(item.message:gsub("[\n]+", "")):sub(0, vim.o.columns),
full_text = vim.trim(item.message),
type = M.severity[item.severity] or M.severity[0],
code = item.code or (item.user_data and item.user_data.lsp and item.user_data.lsp.code),
source = item.source,
code = item.code or (item.user_data and item.user_data.lsp and item.user_data.lsp.code), ---@type string?
source = item.source, ---@type string?
severity = item.severity or 0,
}
return ret
Expand Down

0 comments on commit 7a6abd7

Please sign in to comment.