cargo-nvim 0.1.6

A Neovim plugin for Rust Cargo commands
Documentation
# ๐Ÿš€ cargo.nvim

[![Rust CI](https://github.com/nwiizo/cargo.nvim/actions/workflows/rust.yml/badge.svg)](https://github.com/nwiizo/cargo.nvim/actions/workflows/rust.yml)
[![Lua CI](https://github.com/nwiizo/cargo.nvim/actions/workflows/lua.yml/badge.svg)](https://github.com/nwiizo/cargo.nvim/actions/workflows/lua.yml)

---

๐Ÿ“ฆ A Neovim plugin that provides seamless integration with Rust's Cargo commands. Execute Cargo commands directly from Neovim with a floating window interface.

![cargo.nvim demo](.github/cargo.nvim.gif)

## โœจ Features

- ๐Ÿ”ง Execute Cargo commands directly from Neovim
- ๐ŸชŸ Real-time output in floating windows
- ๐ŸŽจ Syntax highlighting for Cargo output
- โšก Asynchronous command execution
- ๐Ÿ”„ Auto-closing windows on command completion
- โŒจ๏ธ Easy keyboard shortcuts for window management
- ๐Ÿ“Ÿ Terminal mode for interactive applications

## ๐ŸŽฏ Scope

cargo.nvim is a faithful front-end to the `cargo` CLI inside Neovim: it runs Cargo
subcommands and shows their streamed output in a readable window, including
interactive programs (via terminal mode). It is intentionally a *command runner and
report viewer*, not a language-intelligence layer.

It complements, rather than replaces, the LSP-based Rust tooling you may already use:

- **rust-analyzer / [rustaceanvim]https://github.com/mrcjkb/rustaceanvim** โ€” inline diagnostics, jump-to-error, hover, macro expansion, code actions, runnables/testables.
- **[crates.nvim]https://github.com/saecki/crates.nvim** โ€” editing dependencies inside `Cargo.toml`.
- **[neotest]https://github.com/nvim-neotest/neotest** โ€” structured test running and navigation.

If you run those, cargo.nvim is most useful for the Cargo subcommands they don't
surface (e.g. `audit`, `outdated`, `tree`, `vendor`, `publish`, `doc`, `bench`) and
for running interactive programs. Overlapping commands such as `check`, `clippy`,
`test`, and `run` remain available for backward compatibility, but the LSP/neotest
equivalents generally offer a richer experience.

## ๐Ÿ“‘ Table of Contents

- [๐Ÿš€ cargo.nvim]#-cargonvim
  - [โœจ Features]#-features
  - [๐ŸŽฏ Scope]#-scope
  - [๐Ÿ“‘ Table of Contents]#-table-of-contents
  - [๐Ÿ“ฅ Installation]#-installation
    - [Using lazy.nvim]#using-lazynvim
    - [Using packer.nvim]#using-packernvim
  - [๐Ÿ“‹ Requirements]#-requirements
    - [Troubleshooting]#troubleshooting
  - [๐Ÿ› ๏ธ Available Commands]#๏ธ-available-commands
  - [โš™๏ธ Configuration]#๏ธ-configuration
  - [โŒจ๏ธ Key Mappings]#๏ธ-key-mappings
  - [๐Ÿ”„ Interactive Mode]#-interactive-mode
  - [๐Ÿ“Ÿ Terminal Mode]#-terminal-mode
  - [๐Ÿ‘ฅ Contributing]#-contributing
  - [๐Ÿ“œ License]#-license
  - [๐Ÿ’ Acknowledgements]#-acknowledgements
  - [๐ŸŽ‰ Related Projects]#-related-projects

## ๐Ÿ“ฅ Installation

### Using [lazy.nvim]https://github.com/folke/lazy.nvim

```lua
{
  "nwiizo/cargo.nvim",
  build = "cargo build --release",
  config = function()
    require("cargo").setup({
      float_window = true,
      window_width = 0.8,
      window_height = 0.8,
      border = "rounded",
      auto_close = true,
      close_timeout = 5000,
    })
  end,
  ft = { "rust" },
  cmd = {
    "CargoBench",
    "CargoBuild", 
    "CargoClean",
    "CargoDoc",
    "CargoNew",
    "CargoRun",
    "CargoRunTerm",
    "CargoTest",
    "CargoUpdate",
    "CargoCheck",
    "CargoClippy",
    "CargoAdd",
    "CargoRemove",
    "CargoFmt",
    "CargoFix"
  }
}
```

### Using [packer.nvim]https://github.com/wbthomason/packer.nvim

```lua
use {
  "nwiizo/cargo.nvim",
  run = "cargo build --release",
  config = function()
    require("cargo").setup({
      float_window = true,
      window_width = 0.8,
      window_height = 0.8,
      border = "rounded",
      auto_close = true,
      close_timeout = 5000,
    })
  end,
}
```

## ๐Ÿ“‹ Requirements

- ๐Ÿ’ป Neovim >= 0.9.0
- ๐Ÿฆ€ Rust and Cargo installed on your system
- ๐Ÿ“š **LuaJIT development libraries (REQUIRED for building)**:
  - **Ubuntu/Debian:** `sudo apt install libluajit-5.1-dev`
  - **macOS:** `brew install luajit`
  - **Arch Linux:** `sudo pacman -S luajit`
  - **Fedora:** `sudo dnf install luajit-devel`

> **Important:** You must install the LuaJIT development package BEFORE installing this plugin. The plugin includes a native Rust library that links against LuaJIT.

### Troubleshooting

If you see "Cargo library not found" error after installation:

1. **Check if LuaJIT dev package is installed** (see above)
2. **Rebuild the library manually:**
   ```bash
   cd ~/.local/share/nvim/lazy/cargo.nvim  # or your plugin directory
   cargo build --release
   ```
3. **Verify the library was built:**
   ```bash
   ls -la target/release/libcargo_nvim.*
   # Should show libcargo_nvim.so (Linux) or libcargo_nvim.dylib (macOS)
   ```

If `cargo build --release` fails with "unable to find library -lluajit-5.1", you need to install the LuaJIT development package for your system.

## ๐Ÿ› ๏ธ Available Commands

### Core Commands
- ๐Ÿ“Š `:CargoBench` - Run benchmarks
- ๐Ÿ—๏ธ `:CargoBuild` - Build the project
- ๐Ÿงน `:CargoClean` - Remove generated artifacts 
- ๐Ÿ“š `:CargoDoc` - Generate project documentation
- โœจ `:CargoNew` - Create a new Cargo project
- โ–ถ๏ธ  `:CargoRun` - Run the project in a floating window
- ๐Ÿ“Ÿ `:CargoRunTerm` - Run the project in terminal mode (better for interactive applications)
- ๐Ÿงช `:CargoTest` - Run tests
- ๐Ÿ”„ `:CargoUpdate` - Update dependencies

### Additional Commands
- ๐Ÿ” `:CargoCheck` - Check the project for errors
- ๐Ÿ“‹ `:CargoClippy` - Run the Clippy linter
- โž• `:CargoAdd` - Add dependency
- โž– `:CargoRemove` - Remove dependency
- ๐ŸŽจ `:CargoFmt` - Format code with rustfmt
- ๐Ÿ”ง `:CargoFix` - Auto-fix warnings
- ๐Ÿ“ฆ `:CargoPublish` - Publish package
- ๐Ÿ“ฅ `:CargoInstall` - Install binary
- ๐Ÿ“ค `:CargoUninstall` - Uninstall binary
- ๐Ÿ”Ž `:CargoSearch` - Search packages
- ๐ŸŒฒ `:CargoTree` - Show dependency tree
- ๐Ÿ“ฆ `:CargoVendor` - Vendor dependencies
- ๐Ÿ›ก๏ธ `:CargoAudit` - Audit dependencies
- ๐Ÿ“Š `:CargoOutdated` - Check outdated dependencies
- ๐Ÿค– `:CargoAutodd` - Automatically manage dependencies

## โš™๏ธ Configuration

You can customize cargo.nvim by passing options to the setup function:

```lua
require("cargo").setup({
  -- Window settings
  float_window = true,          -- Use floating window
  window_width = 0.8,           -- Window width (80% of editor width)
  window_height = 0.8,          -- Window height (80% of editor height)
  border = "rounded",           -- Border style ("none", "single", "double", "rounded")
  wrap_output = true,           -- Enable text wrapping in output window
  show_line_numbers = true,     -- Show line numbers in output window
  show_cursor_line = true,      -- Highlight current line in output window
  
  -- Auto-close settings
  auto_close = true,            -- Auto close window on success
  close_timeout = 5000,         -- Close window after 5000ms
  
  -- Timeout settings
  run_timeout = 300,            -- Timeout for cargo run in seconds
  interactive_timeout = 30,     -- Inactivity timeout for interactive mode
  
  -- Advanced behavior options
  force_interactive_run = true, -- Always treat cargo run as interactive mode
  max_inactivity_warnings = 3,  -- Maximum number of inactivity warnings before termination
  detect_proconio = true,       -- Enable detection of proconio usage
  force_smart_detection = true, -- Always use smart detection for interactive programs
  
  -- Key mappings (customizable)
  keymaps = {
    close = "q",
    scroll_up = "<C-u>",
    scroll_down = "<C-d>",
    scroll_top = "gg",
    scroll_bottom = "G",
    interrupt = "<C-c>",
    send_input = "i",    -- Send a line to the program's stdin
    toggle_wrap = "w",
    copy_output = "y",
    clear_output = "c",
  },
})
```

### Custom Commands

The `commands` table is extensible: add an entry to register a new `:Cargo*`
command from your configuration alone โ€” no rebuild required. Each entry may set
`cmd` (the actual Cargo subcommand, defaults to the key) and `args` (arguments
prepended before any you type):

```lua
require("cargo").setup({
  commands = {
    -- :CargoTestRelease -> `cargo test --release`
    testRelease = { cmd = "test", args = { "--release" }, desc = "Run tests in release mode" },
    -- :CargoNextest -> `cargo nextest run`
    nextest = { cmd = "nextest", args = { "run" }, desc = "Run tests with nextest" },
  },
})
```

The key becomes the command name (`testRelease` โ†’ `:CargoTestRelease`). When
`nargs` is omitted it defaults to `"*"`, so extra arguments are still accepted.

## โŒจ๏ธ Key Mappings

In the floating window:
- `q` or `<Esc>` - Close the window
- `<C-c>` - Interrupt the running command
- `i` - Send a line to the program's stdin
- `<C-u>` - Scroll up
- `<C-d>` - Scroll down
- `gg` - Scroll to top
- `G` - Scroll to bottom
- `w` - Toggle text wrapping
- `y` - Copy all output to clipboard
- `c` - Clear output

## ๐Ÿ”„ Interactive Mode

Commands run asynchronously: output is streamed into the floating window as it is
produced, and you stay in control of the editor while a command runs.

For programs that read from stdin:
- Press `i` in the output window to be prompted for a line of input, which is
  sent to the running program's stdin.
- Press `<C-c>` to interrupt (terminate) the running command at any time.

For terminal-style programs that need a full TTY (e.g. `proconio`, TUIs), prefer
[Terminal Mode](#-terminal-mode) below.

## ๐Ÿ“Ÿ Terminal Mode

For highly interactive applications (e.g., using proconio or TUI applications):
- Use `:CargoRunTerm` to run your application in a terminal emulator inside a floating window
- Supports full terminal capabilities for interactive Rust applications
- Useful for:
  - Competitive programming with libraries like proconio
  - Text-based UI applications
  - Programs requiring advanced terminal input/output
- Provides a better experience than the standard `:CargoRun` for interactive applications

### Terminal Mode Key Mappings
- `q` or `<Esc>` - Close the window (after program completion)
- `<C-\><C-n>` - Switch to normal mode (while running)
- `<C-c>` - Send interrupt signal
- `<C-d>` - Send EOF signal

## ๐Ÿ‘ฅ Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

1. ๐Ÿด Fork the repository
2. ๐ŸŒฟ Create a feature branch
3. โœ๏ธ Commit your changes
4. ๐Ÿš€ Push to the branch
5. ๐Ÿ“ซ Open a Pull Request

## ๐Ÿ“œ License

MIT License - see the [LICENSE](LICENSE) file for details.

## ๐Ÿ’ Acknowledgements

This plugin is inspired by various Neovim plugins and the Rust community.

## ๐ŸŽ‰ Related Projects

- [cargo-autodd]https://github.com/nwiizo/cargo-autodd - A tool for automatic dependency management