diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..95882de --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright © 2024 + + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the “Software”), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md index 28db71f..4054c80 100644 --- a/README.md +++ b/README.md @@ -1,74 +1,130 @@ -# Vial.nvim +# vial.nvim -`vial.nvim` is a Neovim plugin designed to run Rust tests using a configurable `vial` executable. This plugin allows you to run the current test under the cursor or the last run test with simple key mappings. +Vial Logo + +vial.nvim is a Neovim plugin that triggers commands on a separate terminal, primarily designed for running unit tests. + +It provides an efficient workflow for developers by automatically running tests on file save and offering various test-related commands. ## Features -- Run the Rust test at the current cursor position. -- Re-run the last executed test. -- Configurable path for the `vial` executable. +- Automatically runs tests on file save +- Runs the current test if a test is detected under cursor or repeats last test execution +- Customizable for different file types and test commands +- Toggleable automatic test running +- Easy-to-use keybindings for various test-related actions ## Installation ### Using [lazy.nvim](https://github.com/folke/lazy.nvim) -Add the following to your Neovim configuration: - ```lua -require('lazy').setup({ - -- Add other plugins here - { - 'fcoury/vial.nvim', - config = function() - require('vial').setup({ vial_path = "/path/to/vial" }) - end - } -}) +{ + 'fcoury/vial.nvim', + config = function() + require('vial').setup({ + -- your configuration here + }) + end +} ``` -Replace `/path/to/vial` with the actual path to your `vial` executable. +### Using [packer.nvim](https://github.com/wbthomason/packer.nvim) + +```lua +use { + 'fcoury/vial.nvim', + config = function() + require('vial').setup({ + -- your configuration here + }) + end +} +``` ## Configuration -### Default Configuration +### `setup` Function + +The `setup` function accepts the following options: -By default, the plugin looks for the `vial` executable at `target/release/vial`. If your `vial` executable is located elsewhere, you can configure the path during setup: +- `vial_path` (string): Path to the `vial` executable. +- `file_types` (table): Configuration for different file types. Each key is a file type, and the value is a table with the following keys: + - `command` (string): Command to run, where `%s` will be replaced by the test name. + - `extensions` (table): List of file extensions for the file type. + - `extract` (function): Function to extract the test name from the current buffer. + +Here's an example configuration for Rust files: ```lua -require('vial').setup({ - vial_path = "/custom/path/to/vial" -- Replace with your actual path +local function current_rust_test_name() + -- Get the current buffer and cursor position + local bufnr = vim.api.nvim_get_current_buf() + + ---@diagnostic disable-next-line: deprecated + local row, _ = unpack(vim.api.nvim_win_get_cursor(0)) + + local function_declaration_line = 0 + local function_name = "" + + -- Check lines above the cursor for the #[test] attribute and function definition + for i = row, 1, -1 do + local line = vim.api.nvim_buf_get_lines(bufnr, i - 1, i, false)[1] + + -- Check for function start and capture the function name + if line:match("^%s*fn%s") then + function_declaration_line = i + function_name = line:match("^%s*fn%s([%w_]+)%s*%(") + break + end + end + + for i = function_declaration_line, 1, -1 do + local line = vim.api.nvim_buf_get_lines(bufnr, i - 1, i, false)[1] + if line:match("^%s*#%[test%]") then + return function_name + end + end + + return nil +end + +require("vial").setup({ + vial_path = "/path/to/vial/executable", + file_types = { + rust = { + command = "cargo test %s -- --nocapture --color=always", + extensions = { ".rs" }, + extract = current_rust_test_name, + }, + }, }) ``` -### Key Mappings - -The plugin provides the following default key mappings: +Adjust the `vial_path` to point to your vial executable. -- `tt`: Run the test at the current cursor position. -- `tl`: Run the last executed test. +## Usage -You can add these mappings to your Neovim configuration file: +vial.nvim provides the following keybindings by default: -```lua -vim.api.nvim_set_keymap("n", "tt", "lua require'vial'.run_test()", { noremap = true, silent = true }) -vim.api.nvim_set_keymap("n", "tl", "lua require'vial'.run_last_test()", { noremap = true, silent = true }) -``` +- `tt`: Run the current test +- `tl`: Run the last test +- `tc`: Clear the last test +- `ta`: Toggle automatic test running +- `ts`: Show the last test name -## Usage +## Functions -1. Place the cursor on the line containing the test function you want to run. -2. Press `tt` to run the test. -3. To re-run the last executed test, press `tl`. +- `require('vial').run_test()`: Run the current test +- `require('vial').run_last_test()`: Run the last test +- `require('vial').clear_last_test()`: Clear the last test +- `require('vial').toggle_active()`: Toggle automatic test running +- `require('vial').show_last_test()`: Display the name of the last test ## Contributing -Contributions are welcome! Feel free to open issues or submit pull requests. +Contributions are welcome! Please feel free to submit a Pull Request. ## License -This plugin is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. - -## Acknowledgments - -- [Neovim](https://neovim.io/) -- [lazy.nvim](https://github.com/folke/lazy.nvim) +[MIT License](LICENSE) diff --git a/assets/logo.png b/assets/logo.png new file mode 100644 index 0000000..f90f37d Binary files /dev/null and b/assets/logo.png differ diff --git a/assets/vial-logo.png b/assets/vial-logo.png new file mode 100644 index 0000000..8474cee Binary files /dev/null and b/assets/vial-logo.png differ