Skip to content

Commit 8ba9988

Browse files
committed
Refactor UI components and layout APIs
1 parent 744e0f3 commit 8ba9988

File tree

11 files changed

+313
-297
lines changed

11 files changed

+313
-297
lines changed

lua/clojure-test/api/exceptions.lua

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
local ui_exceptions = require("clojure-test.ui.exceptions")
1+
local components = require("clojure-test.ui.components")
22
local config = require("clojure-test.config")
33
local utils = require("clojure-test.utils")
44
local nio = require("nio")
55

6+
local NuiPopup = require("nui.popup")
7+
68
local M = {}
79

810
function M.go_to_exception(target_window, exception)
@@ -47,6 +49,34 @@ function M.go_to_exception(target_window, exception)
4749
end
4850
end
4951

52+
local function open_exception_popup()
53+
local popup = NuiPopup({
54+
border = {
55+
style = "rounded",
56+
text = {
57+
top = " Exception ",
58+
},
59+
},
60+
position = "50%",
61+
relative = "editor",
62+
enter = true,
63+
size = {
64+
width = 120,
65+
height = 30,
66+
},
67+
})
68+
69+
for _, chord in ipairs(utils.into_table(config.keys.ui.quit)) do
70+
popup:map("n", chord, function()
71+
popup:unmount()
72+
end, { noremap = true })
73+
end
74+
75+
popup:mount()
76+
77+
return popup
78+
end
79+
5080
function M.render_exception(sym)
5181
local exceptions = config.backend:analyze_exception(sym)
5282
if not exceptions or exceptions == vim.NIL then
@@ -55,8 +85,8 @@ function M.render_exception(sym)
5585

5686
local last_active_window = vim.api.nvim_get_current_win()
5787

58-
local popup = ui_exceptions.open_exception_popup()
59-
ui_exceptions.render_exceptions_to_buf(popup.bufnr, exceptions)
88+
local popup = open_exception_popup()
89+
components.exception.render_exceptions_to_buf(popup.bufnr, exceptions)
6090

6191
local event = require("nui.utils.autocmd").event
6292
popup:on({ event.WinLeave }, function()

lua/clojure-test/api/run.lua

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local exceptions_api = require("clojure-test.api.exceptions")
2-
local interface_api = require("clojure-test.ui")
2+
local layouts = require("clojure-test.ui.layouts")
33
local config = require("clojure-test.config")
44
local nio = require("nio")
55

@@ -43,25 +43,24 @@ local function handle_go_to_event(target_window, event)
4343
end)
4444
end
4545

46-
local M = {}
47-
48-
local active_ui = nil
46+
local M = {
47+
active_ui = nil,
48+
}
4949

5050
function M.open_reports(reports)
5151
local last_active_window = vim.api.nvim_get_current_win()
5252

53-
local ui = active_ui
53+
local ui = M.active_ui
5454
if not ui then
55-
ui = interface_api.create(function(event)
55+
ui = layouts.create_layout(function(event)
5656
if event.type == "go-to" then
5757
return handle_go_to_event(last_active_window, event)
5858
end
5959
end)
60-
active_ui = ui
60+
M.active_ui = ui
6161
end
6262

6363
ui:mount()
64-
6564
ui:render_reports(reports)
6665
end
6766

@@ -75,19 +74,19 @@ function M.run_tests(tests)
7574
unmounted = false,
7675
}
7776

78-
local ui = active_ui
77+
local ui = M.active_ui
7978
if not ui then
80-
ui = interface_api.create(function(event)
79+
ui = layouts.create_layout(function(event)
8180
if event.type == "go-to" then
8281
return handle_go_to_event(state.last_active_window, event)
8382
end
8483
if event.type == "unmount" then
8584
state.unmounted = true
86-
active_ui = nil
85+
M.active_ui = nil
8786
return
8887
end
8988
end)
90-
active_ui = ui
89+
M.active_ui = ui
9190
end
9291

9392
ui:mount()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
local M = {}
2+
3+
function M.write_clojure_to_buf(buf, contents)
4+
vim.api.nvim_set_option_value("filetype", "clojure", {
5+
buf = buf,
6+
})
7+
8+
local lines = {}
9+
if contents then
10+
lines = vim.split(contents, "\n")
11+
end
12+
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
13+
end
14+
15+
return M

lua/clojure-test/ui/exceptions.lua renamed to lua/clojure-test/ui/components/exception.lua

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
local config = require("clojure-test.config")
21
local utils = require("clojure-test.utils")
32

4-
local NuiPopup = require("nui.popup")
53
local NuiLine = require("nui.line")
64
local NuiText = require("nui.text")
75

@@ -67,32 +65,4 @@ function M.render_exceptions_to_buf(buf, exception_chain)
6765
end
6866
end
6967

70-
function M.open_exception_popup()
71-
local popup = NuiPopup({
72-
border = {
73-
style = "rounded",
74-
text = {
75-
top = " Exception ",
76-
},
77-
},
78-
position = "50%",
79-
relative = "editor",
80-
enter = true,
81-
size = {
82-
width = 120,
83-
height = 30,
84-
},
85-
})
86-
87-
for _, chord in ipairs(utils.into_table(config.keys.ui.quit)) do
88-
popup:map("n", chord, function()
89-
popup:unmount()
90-
end, { noremap = true })
91-
end
92-
93-
popup:mount()
94-
95-
return popup
96-
end
97-
9868
return M
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
exception = require("clojure-test.ui.components.exception"),
3+
tree = require("clojure-test.ui.components.tree"),
4+
clojure = require("clojure-test.ui.components.clojure"),
5+
}

lua/clojure-test/ui/report-tree.lua renamed to lua/clojure-test/ui/components/tree.lua

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,10 @@ end
150150

151151
local M = {}
152152

153-
function M.create(window, on_event)
153+
function M.create(buf, on_event)
154154
local tree = NuiTree({
155-
winid = window.winid,
156-
bufnr = vim.api.nvim_win_get_buf(window.winid),
157-
ns_id = "testns",
155+
bufnr = buf,
156+
ns_id = "clojure-test-report-tree",
158157
nodes = {},
159158
prepare_node = function(node)
160159
local line = NuiLine()
@@ -183,10 +182,14 @@ function M.create(window, on_event)
183182
tree = tree,
184183
}
185184

186-
local map_options = { noremap = true, nowait = true }
185+
local map_options = {
186+
noremap = true,
187+
nowait = true,
188+
buffer = buf,
189+
}
187190

188191
for _, chord in ipairs(utils.into_table(config.keys.ui.collapse_node)) do
189-
window:map("n", chord, function()
192+
vim.keymap.set("n", chord, function()
190193
local node = tree:get_node()
191194
if not node then
192195
return
@@ -206,7 +209,7 @@ function M.create(window, on_event)
206209
end
207210

208211
for _, chord in ipairs(utils.into_table(config.keys.ui.expand_node)) do
209-
window:map("n", chord, function()
212+
vim.keymap.set("n", chord, function()
210213
local node = tree:get_node()
211214
if node and node:expand() then
212215
tree:render()
@@ -215,7 +218,7 @@ function M.create(window, on_event)
215218
end
216219

217220
for _, chord in ipairs(utils.into_table(config.keys.ui.go_to)) do
218-
window:map("n", chord, function()
221+
vim.keymap.set("n", chord, function()
219222
local node = tree:get_node()
220223
if not node then
221224
return
@@ -228,18 +231,21 @@ function M.create(window, on_event)
228231
end, map_options)
229232
end
230233

231-
local event = require("nui.utils.autocmd").event
232-
window:on({ event.CursorMoved }, function()
233-
local node = tree:get_node()
234-
if not node then
235-
return
236-
end
234+
vim.api.nvim_create_autocmd("CursorMoved", {
235+
desc = "Track cursor in report-tree",
236+
buffer = buf,
237+
callback = function()
238+
local node = tree:get_node()
239+
if not node then
240+
return
241+
end
237242

238-
on_event({
239-
type = "hover",
240-
node = node,
241-
})
242-
end, {})
243+
on_event({
244+
type = "hover",
245+
node = node,
246+
})
247+
end,
248+
})
243249

244250
function ReportTree:render_reports(reports)
245251
tree:set_nodes(reports_to_nodes(reports, tree:get_nodes()))

lua/clojure-test/ui/layout.lua

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)