Skip to main content

rust_analyzer_cli/
cli.rs

1use clap::{Parser, Subcommand};
2use std::path::PathBuf;
3
4#[derive(Parser, Debug)]
5#[command(
6    name = "rust-analyzer-cli",
7    author,
8    version,
9    about = "Compiler-accurate Rust codebase navigation powered by rust-analyzer LSP"
10)]
11pub struct Cli {
12    /// Port for the background daemon HTTP server
13    #[arg(short, long, env = "RUST_ANALYZER_CLI_PORT", default_value_t = 60094)]
14    pub port: u16,
15
16    /// Output results in JSON format
17    #[arg(short, long, global = true)]
18    pub json: bool,
19
20    #[command(subcommand)]
21    pub command: Commands,
22}
23
24#[derive(Subcommand, Debug)]
25pub enum Commands {
26    /// Start the background LSP daemon and HTTP server
27    Daemon {
28        /// Workspace root directory
29        #[arg(short, long, default_value = ".")]
30        workspace: PathBuf,
31    },
32    /// Query daemon and rust-analyzer status
33    Status,
34    /// Refresh / restart the rust-analyzer session
35    Refresh,
36    /// Search workspace symbols
37    Symbol {
38        /// Symbol name to search for
39        name: String,
40        /// Optional symbol filter: any, struct, enum, fn, trait, type, const, var
41        #[arg(default_value = "any")]
42        kind: String,
43        /// Match exact symbol name
44        #[arg(long)]
45        exact: bool,
46    },
47    /// Extract file document symbols outline
48    Outline {
49        /// Target file path
50        #[arg(short, long)]
51        file: Option<PathBuf>,
52        /// Comma-separated list of target file paths
53        #[arg(long)]
54        file_list: Option<String>,
55        /// Write outline output to a file
56        #[arg(short, long)]
57        output: Option<PathBuf>,
58    },
59    /// Go to definition at file line and column
60    Definition {
61        /// File path
62        #[arg(short, long)]
63        file: PathBuf,
64        /// Line number (1-based)
65        #[arg(short, long)]
66        line: u32,
67        /// Column number (1-based)
68        #[arg(short, long)]
69        col: u32,
70    },
71    /// Find references or call hierarchy at file line and column
72    Cursor {
73        /// File path
74        #[arg(short, long)]
75        file: PathBuf,
76        /// Line number (1-based)
77        #[arg(short, long)]
78        line: u32,
79        /// Column number (1-based)
80        #[arg(short, long)]
81        col: u32,
82        /// Mode: incoming, outgoing, or references
83        #[arg(short, long, default_value = "incoming")]
84        mode: String,
85        /// Traversal depth (default: 1)
86        #[arg(short, long, default_value_t = 1)]
87        depth: u32,
88    },
89    /// Query type hierarchy (supertypes / subtypes)
90    TypeHierarchy {
91        /// File path
92        #[arg(short, long)]
93        file: PathBuf,
94        /// Line number (1-based)
95        #[arg(short, long)]
96        line: u32,
97        /// Column number (1-based)
98        #[arg(short, long)]
99        col: u32,
100        /// Mode: supertypes or subtypes
101        #[arg(short, long, default_value = "supertypes")]
102        mode: String,
103        /// Traversal depth (default: 1)
104        #[arg(short, long, default_value_t = 1)]
105        depth: u32,
106    },
107    /// Run cargo check compilation verification
108    Check {
109        /// Optional cargo build target
110        #[arg(short, long)]
111        target: Option<String>,
112    },
113}
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118
119    #[test]
120    fn test_cli_symbol_parsing() {
121        let args = vec!["rust-analyzer-cli", "symbol", "LspClient", "struct", "--exact"];
122        let cli = Cli::try_parse_from(args).unwrap();
123        assert_eq!(cli.port, 60094);
124        assert!(!cli.json);
125        match cli.command {
126            Commands::Symbol { name, kind, exact } => {
127                assert_eq!(name, "LspClient");
128                assert_eq!(kind, "struct");
129                assert!(exact);
130            }
131            _ => panic!("Expected Symbol command"),
132        }
133    }
134
135    #[test]
136    fn test_cli_outline_parsing() {
137        let args = vec!["rust-analyzer-cli", "--json", "outline", "-f", "src/main.rs"];
138        let cli = Cli::try_parse_from(args).unwrap();
139        assert!(cli.json);
140        match cli.command {
141            Commands::Outline { file, file_list, output } => {
142                assert_eq!(file, Some(PathBuf::from("src/main.rs")));
143                assert!(file_list.is_none());
144                assert!(output.is_none());
145            }
146            _ => panic!("Expected Outline command"),
147        }
148    }
149
150    #[test]
151    fn test_cli_definition_parsing() {
152        let args = vec!["rust-analyzer-cli", "definition", "-f", "src/lib.rs", "-l", "10", "-c", "5"];
153        let cli = Cli::try_parse_from(args).unwrap();
154        match cli.command {
155            Commands::Definition { file, line, col } => {
156                assert_eq!(file, PathBuf::from("src/lib.rs"));
157                assert_eq!(line, 10);
158                assert_eq!(col, 5);
159            }
160            _ => panic!("Expected Definition command"),
161        }
162    }
163}
164