Skip to content

Commit

Permalink
feat: Use code descriptions for opening URIs with extra information (#…
Browse files Browse the repository at this point in the history
…309)

* feat(diagnostics): open code HREFs

* style: remove redundant if-branch

* docs: document action in readme
  • Loading branch information
MariaSolOs committed Jul 25, 2023
1 parent 46de8a9 commit d2b0f1d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Trouble comes with the following defaults:
toggle_preview = "P", -- toggle auto_preview
hover = "K", -- opens a small popup with the full multiline message
preview = "p", -- preview the diagnostic location
open_code_href = "c", -- if present, open a URI with more information about the diagnostic error
close_folds = {"zM", "zm"}, -- close all folds
open_folds = {"zR", "zr"}, -- open all folds
toggle_fold = {"zA", "za"}, -- toggle fold of current file
Expand Down
1 change: 1 addition & 0 deletions lua/trouble/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ local defaults = {
toggle_preview = "P", -- toggle auto_preview
hover = "K", -- opens a small popup with the full multiline message
preview = "p", -- preview the diagnostic location
open_code_href = "c", -- if present, open a URI with more information about the diagnostic error
close_folds = { "zM", "zm" }, -- close all folds
open_folds = { "zR", "zr" }, -- open all folds
toggle_fold = { "zA", "za" }, -- toggle fold of current file
Expand Down
3 changes: 3 additions & 0 deletions lua/trouble/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ function Trouble.action(action)
if action == "preview" then
view:preview()
end
if action == "open_code_href" then
view:open_code_href()
end

if Trouble[action] then
Trouble[action]()
Expand Down
9 changes: 7 additions & 2 deletions lua/trouble/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,9 @@ end

-- based on the Telescope diagnostics code
-- 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 start = {
Expand Down Expand Up @@ -183,6 +181,13 @@ function M.process_item(item, bufnr)
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), ---@type string?
code_href = (item.codeDescription and item.codeDescription.href)
or (
item.user_data
and item.user_data.lsp
and item.user_data.lsp.codeDescription
and item.user_data.lsp.codeDescription.href
), ---@type string?
source = item.source, ---@type string?
severity = item.severity or 0,
}
Expand Down
35 changes: 35 additions & 0 deletions lua/trouble/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -521,4 +521,39 @@ end

View.preview = util.throttle(50, View._preview)

function View:open_code_href()
if not vim.api.nvim_win_is_valid(self.parent) then
return
end

local item = self:current_item()
if not item then
return
end
util.debug("open code href")

if item.is_file ~= true and item.code_href then
local cmd
if vim.fn.has("win32") == 1 then
cmd = "explorer"
elseif vim.fn.executable("xdg-open") == 1 then
cmd = "xdg-open"
elseif vim.fn.executable("wslview") == 1 then
cmd = "wslview"
else
cmd = "open"
end

local ret = vim.fn.jobstart({ cmd, item.code_href }, { detach = true })
if ret <= 0 then
local msg = {
"Failed to open code href",
ret,
vim.inspect(cmd),
}
vim.notify(table.concat(msg, "\n"), vim.log.levels.ERROR)
end
end
end

return View

0 comments on commit d2b0f1d

Please sign in to comment.