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 #[arg(short, long, env = "RUST_ANALYZER_CLI_PORT", default_value_t = 60094)]
14 pub port: u16,
15
16 #[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 Daemon {
28 #[arg(short, long, default_value = ".")]
30 workspace: PathBuf,
31 },
32 Status,
34 Refresh,
36 Symbol {
38 name: String,
40 #[arg(default_value = "any")]
42 kind: String,
43 #[arg(long)]
45 exact: bool,
46 },
47 Outline {
49 #[arg(short, long)]
51 file: Option<PathBuf>,
52 #[arg(long)]
54 file_list: Option<String>,
55 #[arg(short, long)]
57 output: Option<PathBuf>,
58 },
59 Definition {
61 #[arg(short, long)]
63 file: PathBuf,
64 #[arg(short, long)]
66 line: u32,
67 #[arg(short, long)]
69 col: u32,
70 },
71 Cursor {
73 #[arg(short, long)]
75 file: PathBuf,
76 #[arg(short, long)]
78 line: u32,
79 #[arg(short, long)]
81 col: u32,
82 #[arg(short, long, default_value = "incoming")]
84 mode: String,
85 #[arg(short, long, default_value_t = 1)]
87 depth: u32,
88 },
89 TypeHierarchy {
91 #[arg(short, long)]
93 file: PathBuf,
94 #[arg(short, long)]
96 line: u32,
97 #[arg(short, long)]
99 col: u32,
100 #[arg(short, long, default_value = "supertypes")]
102 mode: String,
103 #[arg(short, long, default_value_t = 1)]
105 depth: u32,
106 },
107 Check {
109 #[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