]> git.rmz.io Git - dotfiles.git/blob - nvim/lua/plugins/test.lua
lazyvim: remove calls to lazyvim.util.has
[dotfiles.git] / nvim / lua / plugins / test.lua
1 return {
2 {
3 "nvim-neotest/neotest",
4 opts = {
5 status = { virtual_text = true },
6 output = { open_on_run = true },
7 quickfix = {
8 open = function()
9 require("trouble").open({ mode = "quickfix", focus = false })
10 end,
11 },
12 },
13 config = function(_, opts)
14 local neotest_ns = vim.api.nvim_create_namespace("neotest")
15 vim.diagnostic.config({
16 virtual_text = {
17 format = function(diagnostic)
18 -- Replace newline and tab characters with space for more compact diagnostics
19 local message = diagnostic.message:gsub("\n", " "):gsub("\t", " "):gsub("%s+", " "):gsub("^%s+", "")
20 return message
21 end,
22 },
23 }, neotest_ns)
24
25 opts.consumers = opts.consumers or {}
26 -- Refresh and auto close trouble after running tests
27 ---@type neotest.Consumer
28 opts.consumers.trouble = function(client)
29 client.listeners.results = function(adapter_id, results, partial)
30 if partial then
31 return
32 end
33 local tree = assert(client:get_position(nil, { adapter = adapter_id }))
34
35 local failed = 0
36 for pos_id, result in pairs(results) do
37 if result.status == "failed" and tree:get_key(pos_id) then
38 failed = failed + 1
39 end
40 end
41 vim.schedule(function()
42 local trouble = require("trouble")
43 if trouble.is_open() then
44 trouble.refresh()
45 if failed == 0 then
46 trouble.close()
47 end
48 end
49 end)
50 end
51 return {}
52 end
53
54 if opts.adapters then
55 local adapters = {}
56 for name, config in pairs(opts.adapters or {}) do
57 if type(name) == "number" then
58 if type(config) == "string" then
59 config = require(config)
60 end
61 adapters[#adapters + 1] = config
62 elseif config ~= false then
63 local adapter = require(name)
64 if type(config) == "table" and not vim.tbl_isempty(config) then
65 local meta = getmetatable(adapter)
66 if adapter.setup then
67 adapter.setup(config)
68 elseif meta and meta.__call then
69 adapter(config)
70 else
71 error("Adapter " .. name .. " does not support setup")
72 end
73 end
74 adapters[#adapters + 1] = adapter
75 end
76 end
77 opts.adapters = adapters
78 end
79
80 require("neotest").setup(opts)
81 end,
82 -- stylua: ignore
83 keys = {
84 { "<leader>tt", function() require("neotest").run.run(vim.fn.expand("%")) end, desc = "Run File" },
85 { "<leader>tT", function() require("neotest").run.run(vim.loop.cwd()) end, desc = "Run All Test Files" },
86 { "<leader>tr", function() require("neotest").run.run() end, desc = "Run Nearest" },
87 { "<leader>ts", function() require("neotest").summary.toggle() end, desc = "Toggle Summary" },
88 { "<leader>to", function() require("neotest").output.open({ enter = true, auto_close = true }) end, desc = "Show Output" },
89 { "<leader>tO", function() require("neotest").output_panel.toggle() end, desc = "Toggle Output Panel" },
90 { "<leader>tS", function() require("neotest").run.stop() end, desc = "Stop" },
91 },
92 },
93 {
94 "mfussenegger/nvim-dap",
95 -- stylua: ignore
96 keys = {
97 { "<leader>td", function() require("neotest").run.run({strategy = "dap"}) end, desc = "Debug Nearest" },
98 },
99 },
100 }