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