Last active
February 13, 2024 09:59
-
-
Save iboard/f5f928cbdaef8c21211ed0ae317886c4 to your computer and use it in GitHub Desktop.
Lua script for LazyVim to run current line, current file, and the entire suite with mix test in a neovim split view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Run mix test in a split view in neovim | |
-- Andi Altendorfer, 2024-02-12 | |
-- | |
-- Install: | |
-- Add the following line in your init.lua file | |
-- | |
-- require("kitchen.kitchen") | |
-- | |
-- And place this file as lua/kitchen/kitchen.lua | |
-- | |
-- Usage: | |
-- | |
-- Place the cursor within an Elixir test and press | |
-- | |
-- <leader><C-L> .... Run the test of the current line. | |
-- <leader><C-F> .... Run the current test file | |
-- <leader><C-S> .... Run all tests | |
-- | |
-- You can get this script from | |
-- https://gist.github.com/iboard/f5f928cbdaef8c21211ed0ae317886c4 | |
-- ---------------------------------------------------------------------- | |
M = { } | |
local function run_mix_test(command) | |
print("Kitchen: Running mix test with \n" .. command .. "\n") | |
vim.cmd('sp | terminal zsh -c \'' .. command .. '\'') | |
end | |
-- CURRENT LINE | |
vim.api.nvim_create_user_command('MixTestCurrentLine', function() | |
local file_name = vim.fn.expand('%') | |
local line_number = vim.api.nvim_win_get_cursor(0)[1] | |
local command = 'mix test ' .. file_name .. ':' .. tostring(line_number) | |
run_mix_test(command) | |
end, { nargs = '*' }) | |
-- CURRENT FILE | |
vim.api.nvim_create_user_command('MixTestCurrentFile', function() | |
local file_name = vim.fn.expand('%') | |
local command = 'mix test ' .. file_name .. ' --max-failures 1 --seed 0' | |
run_mix_test(command) | |
end, { nargs = '*' }) | |
-- ENTIRE SUITE | |
vim.api.nvim_create_user_command('MixTestSuite', function() | |
local command = 'mix test --max-failures 1 --seed 0' | |
run_mix_test(command) | |
end, { nargs = '*' }) | |
-- INSTALL HOTKEYS | |
local Util = require("lazyvim.util") | |
local map = Util.safe_keymap_set | |
map("n", "<leader><C-L>", ":MixTestCurrentLine<CR>", { desc = "Run test in current line" }) | |
map("n", "<leader><C-F>", ":MixTestCurrentFile<CR>", { desc = "Run current file" }) | |
map("n", "<leader><C-S>", ":MixTestSuite<CR>", { desc = "Run all tests" }) | |
return M | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment