Skip to content

Commit fd1adbf

Browse files
committed
Find first valid path when gd on exceptions
1 parent 1f9c23d commit fd1adbf

File tree

1 file changed

+49
-30
lines changed

1 file changed

+49
-30
lines changed

lua/clojure-test/api/run.lua

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,72 @@ local eval = require("clojure-test.api.eval")
44
local ui = require("clojure-test.ui")
55
local nio = require("nio")
66

7+
local function go_to_test(layout, test)
8+
local meta = eval.eval(eval.API.resolve_metadata_for_symbol, "'" .. test)
9+
if not meta then
10+
return
11+
end
12+
13+
layout:unmount()
14+
vim.cmd("edit " .. meta.file)
15+
vim.schedule(function()
16+
vim.api.nvim_win_set_cursor(0, { meta.line or 0, meta.column or 0 })
17+
end)
18+
end
19+
20+
local function go_to_exception(layout, exception)
21+
local stack = exception["stack-trace"]
22+
if not stack or stack == vim.NIL then
23+
return
24+
end
25+
26+
-- This will iterate over all the frames in a stack trace until a frame points to
27+
-- a line/file/symbol that is within the project classpath and cwd.
28+
--
29+
-- This is a bit hacky as it involves many sequential evals, but it's quick and
30+
-- dirty and it works.
31+
--
32+
-- Future implementation should probably do all this work in clojure land over a
33+
-- single eval
34+
for _, frame in ipairs(stack) do
35+
local symbol = frame.names[1]
36+
local line = frame.line
37+
if symbol then
38+
local meta = eval.eval(eval.API.resolve_metadata_for_symbol, "'" .. symbol)
39+
if meta and meta ~= vim.NIL then
40+
layout:unmount()
41+
vim.cmd("edit " .. meta.file)
42+
vim.schedule(function()
43+
vim.api.nvim_win_set_cursor(0, { line or meta.line or 0, meta.column or 0 })
44+
end)
45+
return
46+
end
47+
end
48+
end
49+
end
50+
751
-- This function is called when <Cr> is pressed while on a node in the report
852
-- tree.
953
--
1054
-- This function implements a kind of 'go-to-definition' for the various types
1155
-- of nodes
1256
local function handle_on_enter(layout, node)
1357
nio.run(function()
14-
local symbol
15-
local line
16-
local col
17-
1858
if node.test then
19-
symbol = node.test
59+
return go_to_test(layout, node.test)
2060
end
2161

22-
local exception
2362
if node.assertion then
2463
if node.assertion.exception then
25-
exception = node.assertion.exception[#node.assertion.exception]
26-
else
27-
symbol = node.test
64+
return go_to_exception(layout, node.assertion.exception[#node.assertion.exception])
2865
end
29-
end
30-
31-
if node.exception then
32-
exception = node.exception
33-
end
3466

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

40-
if not symbol then
41-
return
42-
end
43-
44-
local meta = eval.eval(eval.API.resolve_metadata_for_symbol, "'" .. symbol)
45-
if not meta then
46-
return
70+
if node.exception then
71+
return go_to_exception(layout, node.exception)
4772
end
48-
49-
layout:unmount()
50-
vim.cmd("edit " .. meta.file)
51-
vim.schedule(function()
52-
vim.api.nvim_win_set_cursor(0, { line or meta.line or 0, col or meta.column or 0 })
53-
end)
5473
end)
5574
end
5675

0 commit comments

Comments
 (0)