Skip to content

Commit

Permalink
WIP: Split view layout
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvincent committed Jul 5, 2024
1 parent beba3cc commit 2cfe0ef
Show file tree
Hide file tree
Showing 7 changed files with 388 additions and 156 deletions.
43 changes: 24 additions & 19 deletions lua/clojure-test/api/run.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local parser = require("clojure-test.api.report")
local config = require("clojure-test.config")
local eval = require("clojure-test.api.eval")
local ui = require("clojure-test.ui")
local interface_api = require("clojure-test.ui")
local nio = require("nio")

local function go_to_test(layout, test)
Expand All @@ -10,14 +10,14 @@ local function go_to_test(layout, test)
return
end

layout:unmount()
layout:navigate_back()
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 function go_to_exception(ui, exception)
local stack = exception["stack-trace"]
if not stack or stack == vim.NIL then
return
Expand All @@ -37,7 +37,7 @@ local function go_to_exception(layout, exception)
if symbol then
local meta = eval.eval(eval.API.resolve_metadata_for_symbol, "'" .. symbol)
if meta and meta ~= vim.NIL then
layout:unmount()
ui:navigate_back()
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 })
Expand All @@ -53,40 +53,47 @@ end
--
-- This function implements a kind of 'go-to-definition' for the various types
-- of nodes
local function handle_on_enter(layout, node)
local function handle_go_to_event(ui, event)
local node = event.node
nio.run(function()
if node.test then
return go_to_test(layout, node.test)
return go_to_test(ui, node.test)
end

if node.assertion then
if node.assertion.exception then
return go_to_exception(layout, node.assertion.exception[#node.assertion.exception])
return go_to_exception(ui, node.assertion.exception[#node.assertion.exception])
end

return go_to_test(layout, node.test)
return go_to_test(ui, node.test)
end

if node.exception then
return go_to_exception(layout, node.exception)
return go_to_exception(ui, node.exception)
end
end)
end

local M = {}

local active_ui = nil

function M.run_tests(tests)
if config.hooks.before_run then
config.hooks.before_run(tests)
end

local layout = ui.layout.create_test_layout()

layout:mount()
local ui = active_ui
if not ui then
ui = interface_api.create(function(event)
if event.type == "go-to" then
return handle_go_to_event(ui, event)
end
end)
active_ui = ui
end

local tree = ui.report_tree.create_tree(layout, function(node)
handle_on_enter(layout, node)
end)
ui:mount()

local reports = {}
for _, test in ipairs(tests) do
Expand All @@ -95,8 +102,7 @@ function M.run_tests(tests)

local queue = nio.control.queue()

tree:set_reports(reports)
tree:render()
ui:render_reports(reports)

local semaphore = nio.control.semaphore(1)
for _, test in ipairs(tests) do
Expand All @@ -120,8 +126,7 @@ function M.run_tests(tests)
end

reports[report.test] = parser.parse_test_report(report.test, report.data)
tree:set_reports(reports)
tree:render()
ui:render_reports(reports)
end
end

Expand Down
7 changes: 6 additions & 1 deletion lua/clojure-test/config.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
local backends = require("clojure-test.backends")

local M = {
layout = {
style = "split",
},

keys = {
global = {
run_all_tests = "<localleader>ta",
Expand All @@ -18,7 +22,8 @@ local M = {
cycle_focus_forwards = "<Tab>",
cycle_focus_backwards = "<S-Tab>",

quit = { "<Esc>", "q" },
quit = "q",
hide = "<Esc>",
},
},

Expand Down
116 changes: 112 additions & 4 deletions lua/clojure-test/ui/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,112 @@
return {
layout = require("clojure-test.ui.layout"),
report_tree = require("clojure-test.ui.report-tree"),
}
local report_tree_api = require("clojure-test.ui.report-tree")
local exceptions = require("clojure-test.ui.exceptions")
local layout_api = require("clojure-test.ui.layout")
local config = require("clojure-test.config")
local utils = require("clojure-test.utils")

local M = {}

local function write_clojure_to_buf(buf, contents)
vim.api.nvim_buf_set_option(buf, "filetype", "clojure")

local lines = {}
if contents then
lines = vim.split(contents, "\n")
end
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
end

local function handle_on_move(UI, event)
local node = event.node
local layout = UI.layout

if node.assertion then
if node.assertion.exception then
UI.layout:render_single()
exceptions.render_exception_to_buf(layout.windows.right.bufnr, node.assertion.exception)
return
end

layout:render_double()
write_clojure_to_buf(layout.windows.left.bufnr, node.assertion.expected)
write_clojure_to_buf(layout.windows.right.bufnr, node.assertion.actual)
return
end

if node.exception then
layout:render_single()
exceptions.render_exception_to_buf(layout.windows.right.bufnr, { node.exception })
return
end

layout:hide()
end

function M.create(on_event)
local UI = {
mounted = false,
layout = nil,
tree = nil,

last_active_window = vim.api.nvim_get_current_win(),
}

function UI:mount()
if UI.mounted then
return
end

UI.mounted = true

UI.layout = layout_api.create(function(event)
if event.type == "on-focus-lost" then
return UI.layout:hide()
end
end)
UI.layout:mount()

UI.tree = report_tree_api.create(UI.layout.windows.tree, function(event)
if event.type == "hover" then
return handle_on_move(UI, event)
end

on_event(event)
end)

for _, chord in ipairs(utils.into_table(config.keys.ui.quit)) do
UI.layout:map("n", chord, function()
UI:unmount()
end, { noremap = true })
end
end

function UI:unmount()
if not UI.mounted then
return
end

UI.mounted = false
UI.layout:unmount()
UI.layout = nil
UI.tree = nil

vim.api.nvim_set_current_win(UI.last_active_window)
end

function UI:navigate_back()
-- UI.layout:hide()
vim.api.nvim_set_current_win(UI.last_active_window)
end

function UI:render_reports(reports)
UI.tree:render_reports(reports)
end

function UI:render_exceptions(exception_chain) end

function UI:on() end

return UI
end

return M
Loading

0 comments on commit 2cfe0ef

Please sign in to comment.