-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
120 additions
and
42 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
<img src="assets/logo.png" alt="Vial Logo" width="0" height="120" align="right"/> | ||
|
||
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. | ||
|
||
- `<leader>tt`: Run the test at the current cursor position. | ||
- `<leader>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", "<leader>tt", "<cmd>lua require'vial'.run_test()<cr>", { noremap = true, silent = true }) | ||
vim.api.nvim_set_keymap("n", "<leader>tl", "<cmd>lua require'vial'.run_last_test()<cr>", { noremap = true, silent = true }) | ||
``` | ||
- `<leader>tt`: Run the current test | ||
- `<leader>tl`: Run the last test | ||
- `<leader>tc`: Clear the last test | ||
- `<leader>ta`: Toggle automatic test running | ||
- `<leader>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 `<leader>tt` to run the test. | ||
3. To re-run the last executed test, press `<leader>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) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.