rust-analyzer-cli 0.3.1

A library and CLI tool built on top of rust-analyzer for codebase navigation
Documentation
# rust-analyzer-cli

Compiler-accurate Rust codebase navigation powered by `rust-analyzer` LSP and an embedded HTTP daemon.

`rust-analyzer-cli` is dual-structured as both a **CLI tool** and a **Rust Library** (`rust_analyzer_cli`), enabling AI Agents, developer tools, and Rust programs to perform fast, compiler-accurate symbol search, document outline extraction, definition lookup, call hierarchy analysis, and compilation checks.

---

## Features

- 🚀 **Background Daemon & Persistent LSP Session**: Spawns `rust-analyzer` via JSON-RPC stdio and serves an embedded Axum HTTP REST server (default port `60094`).
- 🔍 **Workspace Symbol Search**: Search for structs, enums, functions, traits, and variables across the entire workspace.
- 📜 **Document Outline**: Extract hierarchical document symbol outlines for any `.rs` file.
- 🎯 **Go to Definition**: Jump directly to exact symbol definitions by file, line, and column.
- 📞 **Call Hierarchy & References**: Query incoming/outgoing call chains and symbol reference locations.
- 🧬 **Type Hierarchy**: Traverse supertypes and subtypes.
- 🛠 **Cargo Check Integration**: Run `cargo check` and parse structured compiler diagnostics.
- 🤖 **AI Agent Friendly**: Supports human-readable CLI outputs and `--json` structured formatting.

---

## Installation

### As a CLI Tool

```bash
cargo install rust-analyzer-cli
```

*Prerequisite*: Ensure `rust-analyzer` is installed and available in your system `PATH`.

### As a Rust Library

Add `rust_analyzer_cli` to your project's `Cargo.toml`:

```toml
[dependencies]
rust_analyzer_cli = "0.2.0"
```

---

## Usage Guide

### 1. Start the Background Daemon

Before running queries, start the daemon from your workspace root:

```bash
rust-analyzer-cli daemon --workspace .
```

To check daemon & LSP indexing status:

```bash
rust-analyzer-cli status [--json]
```

To refresh/restart the `rust-analyzer` session:

```bash
rust-analyzer-cli refresh [--json]
```

---

### 2. Core Commands

#### Search Workspace Symbols

```bash
rust-analyzer-cli symbol <name> [kind] [--exact] [--json]
# Example:
rust-analyzer-cli symbol LspClient struct --exact
```

#### Extract File Outline

```bash
rust-analyzer-cli outline --file <path> [-o <output_file>] [--json]
rust-analyzer-cli outline --file-list <path1,path2> [--json]
# Example:
rust-analyzer-cli outline --file src/main.rs
```

#### Go to Definition

```bash
rust-analyzer-cli definition --file <path> --line <line> --col <col> [--json]
# Example:
rust-analyzer-cli definition --file src/main.rs --line 13 --col 10
```

#### References & Call Hierarchy

```bash
rust-analyzer-cli cursor --file <path> --line <line> --col <col> [--mode incoming|outgoing|references] [--depth <n>] [--json]
# Example:
rust-analyzer-cli cursor --file src/main.rs --line 25 --col 8 --mode incoming
```

#### Type Hierarchy

```bash
rust-analyzer-cli type-hierarchy --file <path> --line <line> --col <col> [--mode supertypes|subtypes] [--depth <n>] [--json]
```

#### Cargo Check Verification

```bash
rust-analyzer-cli check [--target <target>] [--json]
```

---

## Using as a Rust Library

You can embed the HTTP server or interact with data types directly in your Rust applications:

```rust
use rust_analyzer_cli::daemon::server::start_daemon_server;
use rust_analyzer_cli::lsp::types::*;
use std::path::PathBuf;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let workspace = PathBuf::from(".");
    let port = 60094;

    // Start daemon server asynchronously
    tokio::spawn(async move {
        start_daemon_server(workspace, port).await.unwrap();
    });

    Ok(())
}
```

---

## AI Agent Integration

An agent skill definition is included at [.agents/skills/rust-codebase-navigation/SKILL.md](.agents/skills/rust-codebase-navigation/SKILL.md). AI coding agents (such as Google Antigravity or Agentic Assistants) can read this skill to navigate Rust codebases. You can also run `rust-analyzer-cli init-skill` to install it into any project workspace automatically.


---

## License

Dual-licensed under [MIT](LICENSE-MIT) or [Apache-2.0](LICENSE-APACHE).