Skip to content

Commit

Permalink
feat: added support for vim.diagnostics and Neovim 0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Dec 10, 2021
1 parent 78eb5eb commit 735dcd5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
16 changes: 13 additions & 3 deletions lua/trouble/providers/diagnostic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ local M = {}

---@param options TroubleOptions
---@return Item[]
function M.diagnostics(_win, buf, cb, options)
function M.diagnostics(_, buf, cb, options)
if options.mode == "workspace_diagnostics" then
buf = nil
end

local buffer_diags = buf and { [buf] = vim.lsp.diagnostic.get(buf) } or vim.lsp.diagnostic.get_all()
local items = {}

if vim.diagnostic then
local diags = vim.diagnostic.get(buf)
for _, item in ipairs(diags) do
table.insert(items, util.process_item(item))
end
else
---@diagnostic disable-next-line: deprecated
local diags = buf and { [buf] = vim.lsp.diagnostic.get(buf) } or vim.lsp.diagnostic.get_all()
items = util.locations_to_items(diags, 1)
end

local items = util.locations_to_items(buffer_diags, 1)
cb(items)
end

Expand Down
4 changes: 2 additions & 2 deletions lua/trouble/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function renderer.render(view, opts)
end, config.options)
end

---@param view View
---@param view TroubleView
---@param text Text
---@param items Item[]
---@param filename string
Expand Down Expand Up @@ -111,7 +111,7 @@ function renderer.render_file(view, text, filename, items)
end
end

---@param view View
---@param view TroubleView
---@param text Text
---@param items Item[]
function renderer.render_diagnostics(view, text, items)
Expand Down
21 changes: 20 additions & 1 deletion lua/trouble/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,30 @@ end
-- see https://github.com/nvim-telescope/telescope.nvim/blob/0d6cd47990781ea760dd3db578015c140c7b9fa7/lua/telescope/utils.lua#L85

function M.process_item(item, bufnr)
bufnr = bufnr or item.bufnr
local filename = vim.api.nvim_buf_get_name(bufnr)
local uri = vim.uri_from_bufnr(bufnr)
local range = item.range or item.targetSelectionRange
local range = item.range
or item.targetSelectionRange
or {
["start"] = {
character = item.col,
line = item.lnum,
},
["end"] = {
character = item.end_col,
line = item.end_lnum,
},
}
local start = range["start"]
local finish = range["end"]

if start.character == nil or start.line == nil then
M.error("Found an item for Trouble without start range " .. vim.inspect(start))
end
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

Expand Down
1 change: 1 addition & 0 deletions lua/trouble/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local highlight = vim.api.nvim_buf_add_highlight
---@class TroubleView
---@field buf number
---@field win number
---@field group boolean
---@field items Item[]
---@field folded table<string, boolean>
---@field parent number
Expand Down

0 comments on commit 735dcd5

Please sign in to comment.