@@ -4,53 +4,72 @@ local eval = require("clojure-test.api.eval")
4
4
local ui = require (" clojure-test.ui" )
5
5
local nio = require (" nio" )
6
6
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
+
7
51
-- This function is called when <Cr> is pressed while on a node in the report
8
52
-- tree.
9
53
--
10
54
-- This function implements a kind of 'go-to-definition' for the various types
11
55
-- of nodes
12
56
local function handle_on_enter (layout , node )
13
57
nio .run (function ()
14
- local symbol
15
- local line
16
- local col
17
-
18
58
if node .test then
19
- symbol = node .test
59
+ return go_to_test ( layout , node .test )
20
60
end
21
61
22
- local exception
23
62
if node .assertion then
24
63
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 ])
28
65
end
29
- end
30
-
31
- if node .exception then
32
- exception = node .exception
33
- end
34
66
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 )
38
68
end
39
69
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 )
47
72
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 )
54
73
end )
55
74
end
56
75
0 commit comments