-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathfloating-report-viewer.lua
More file actions
95 lines (77 loc) · 2.32 KB
/
floating-report-viewer.lua
File metadata and controls
95 lines (77 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
local class = require('java-core.utils.class')
local StringBuilder = require('java-test.utils.string-builder')
local TestStatus = require('java-test.results.result-status')
local ReportViewer = require('java-test.ui.report-viewer')
---@class java-test.FloatingReportViewer
---@field window number|nil
---@field buffer number|nil
---@overload fun(): java-test.FloatingReportViewer
local FloatingReportViewer = class(ReportViewer)
---Shows the test results in a floating window
---@param test_results java-test.TestResults[]
function FloatingReportViewer:show(test_results)
---@param results java-test.TestResults[]
local function build_result(results, indentation, prefix)
local ts = StringBuilder()
for _, result in ipairs(results) do
local tc = StringBuilder()
tc.append(prefix .. indentation)
if result.is_suite then
tc.append(' ' .. result.test_name).lbreak()
else
local status = result.result.status or TestStatus.Passed
tc.append(status.icon .. result.test_name).lbreak()
if result.result.status == TestStatus.Failed then
tc.append(indentation).append(result.result.trace, indentation)
end
end
if result.children then
tc.append(build_result(result.children, indentation .. '\t', ''))
end
ts.append(tc.build())
end
return ts.build()
end
local res = build_result(test_results, '', '')
FloatingReportViewer.show_in_window(vim.split(res, '\n'))
end
function FloatingReportViewer.show_in_window(content)
vim.api.nvim_create_autocmd('BufWinEnter', {
once = true,
callback = function()
for _, status in pairs(TestStatus) do
vim.fn.matchadd(status.highlight, status.icon)
end
end,
})
local Popup = require('nui.popup')
local event = require('nui.utils.autocmd').event
local popup = Popup({
enter = true,
focusable = true,
border = {
style = 'rounded',
},
position = '50%',
relative = 'editor',
size = {
width = '80%',
height = '60%',
},
win_options = {
foldmethod = 'indent',
foldlevel = 1,
},
})
-- mount/open the component
popup:mount()
-- unmount component when cursor leaves buffer
popup:on(event.BufLeave, function()
popup:unmount()
end)
-- set content
vim.api.nvim_buf_set_lines(popup.bufnr, 0, 1, false, content)
vim.bo[popup.bufnr].modifiable = false
vim.bo[popup.bufnr].readonly = true
end
return FloatingReportViewer