rust-analyzer-cli 0.2.0

A CLI tool built on top of rust-analyzer
use clap::{Parser, Subcommand};
use std::path::PathBuf;

#[derive(Parser, Debug)]
#[command(
    name = "rust-analyzer-cli",
    author,
    version,
    about = "Compiler-accurate Rust codebase navigation powered by rust-analyzer LSP"
)]
pub struct Cli {
    /// Port for the background daemon HTTP server
    #[arg(short, long, env = "RUST_ANALYZER_CLI_PORT", default_value_t = 60094)]
    pub port: u16,

    /// Output results in JSON format
    #[arg(short, long, global = true)]
    pub json: bool,

    #[command(subcommand)]
    pub command: Commands,
}

#[derive(Subcommand, Debug)]
pub enum Commands {
    /// Start the background LSP daemon and HTTP server
    Daemon {
        /// Workspace root directory
        #[arg(short, long, default_value = ".")]
        workspace: PathBuf,
    },
    /// Query daemon and rust-analyzer status
    Status,
    /// Refresh / restart the rust-analyzer session
    Refresh,
    /// Search workspace symbols
    Symbol {
        /// Symbol name to search for
        name: String,
        /// Optional symbol filter: any, struct, enum, fn, trait, type, const, var
        #[arg(default_value = "any")]
        kind: String,
        /// Match exact symbol name
        #[arg(long)]
        exact: bool,
    },
    /// Extract file document symbols outline
    Outline {
        /// Target file path
        #[arg(short, long)]
        file: Option<PathBuf>,
        /// Comma-separated list of target file paths
        #[arg(long)]
        file_list: Option<String>,
        /// Write outline output to a file
        #[arg(short, long)]
        output: Option<PathBuf>,
    },
    /// Go to definition at file line and column
    Definition {
        /// File path
        #[arg(short, long)]
        file: PathBuf,
        /// Line number (1-based)
        #[arg(short, long)]
        line: u32,
        /// Column number (1-based)
        #[arg(short, long)]
        col: u32,
    },
    /// Find references or call hierarchy at file line and column
    Cursor {
        /// File path
        #[arg(short, long)]
        file: PathBuf,
        /// Line number (1-based)
        #[arg(short, long)]
        line: u32,
        /// Column number (1-based)
        #[arg(short, long)]
        col: u32,
        /// Mode: incoming, outgoing, or references
        #[arg(short, long, default_value = "incoming")]
        mode: String,
        /// Traversal depth (default: 1)
        #[arg(short, long, default_value_t = 1)]
        depth: u32,
    },
    /// Query type hierarchy (supertypes / subtypes)
    TypeHierarchy {
        /// File path
        #[arg(short, long)]
        file: PathBuf,
        /// Line number (1-based)
        #[arg(short, long)]
        line: u32,
        /// Column number (1-based)
        #[arg(short, long)]
        col: u32,
        /// Mode: supertypes or subtypes
        #[arg(short, long, default_value = "supertypes")]
        mode: String,
        /// Traversal depth (default: 1)
        #[arg(short, long, default_value_t = 1)]
        depth: u32,
    },
    /// Run cargo check compilation verification
    Check {
        /// Optional cargo build target
        #[arg(short, long)]
        target: Option<String>,
    },
}