Skip to content

Commit

Permalink
Find first valid path when gd on exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvincent committed Jun 17, 2024
1 parent 1f9c23d commit fd1adbf
Showing 1 changed file with 49 additions and 30 deletions.
79 changes: 49 additions & 30 deletions lua/clojure-test/api/run.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,72 @@ local eval = require("clojure-test.api.eval")
local ui = require("clojure-test.ui")
local nio = require("nio")

local function go_to_test(layout, test)
local meta = eval.eval(eval.API.resolve_metadata_for_symbol, "'" .. test)
if not meta then
return
end

layout:unmount()
vim.cmd("edit " .. meta.file)
vim.schedule(function()
vim.api.nvim_win_set_cursor(0, { meta.line or 0, meta.column or 0 })
end)
end

local function go_to_exception(layout, exception)
local stack = exception["stack-trace"]
if not stack or stack == vim.NIL then
return
end

-- This will iterate over all the frames in a stack trace until a frame points to
-- a line/file/symbol that is within the project classpath and cwd.
--
-- This is a bit hacky as it involves many sequential evals, but it's quick and
-- dirty and it works.
--
-- Future implementation should probably do all this work in clojure land over a
-- single eval
for _, frame in ipairs(stack) do
local symbol = frame.names[1]
local line = frame.line
if symbol then
local meta = eval.eval(eval.API.resolve_metadata_for_symbol, "'" .. symbol)
if meta and meta ~= vim.NIL then
layout:unmount()
vim.cmd("edit " .. meta.file)
vim.schedule(function()
vim.api.nvim_win_set_cursor(0, { line or meta.line or 0, meta.column or 0 })
end)
return
end
end
end
end

-- This function is called when <Cr> is pressed while on a node in the report
-- tree.
--
-- This function implements a kind of 'go-to-definition' for the various types
-- of nodes
local function handle_on_enter(layout, node)
nio.run(function()
local symbol
local line
local col

if node.test then
symbol = node.test
return go_to_test(layout, node.test)
end

local exception
if node.assertion then
if node.assertion.exception then
exception = node.assertion.exception[#node.assertion.exception]
else
symbol = node.test
return go_to_exception(layout, node.assertion.exception[#node.assertion.exception])
end
end

if node.exception then
exception = node.exception
end

if exception and exception["stack-trace"] ~= vim.NIL then
symbol = exception["stack-trace"][1].names[1]
line = exception["stack-trace"][1].line
return go_to_test(layout, node.test)
end

if not symbol then
return
end

local meta = eval.eval(eval.API.resolve_metadata_for_symbol, "'" .. symbol)
if not meta then
return
if node.exception then
return go_to_exception(layout, node.exception)
end

layout:unmount()
vim.cmd("edit " .. meta.file)
vim.schedule(function()
vim.api.nvim_win_set_cursor(0, { line or meta.line or 0, col or meta.column or 0 })
end)
end)
end

Expand Down

0 comments on commit fd1adbf

Please sign in to comment.