1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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>,
},
/// Initialize/install the rust-codebase-navigation SKILL.md in a workspace
InitSkill {
/// Target workspace directory (default: .)
#[arg(short, long, default_value = ".")]
workspace: PathBuf,
/// Custom relative skill path (default: .agents/skills/rust-codebase-navigation/SKILL.md)
#[arg(short, long)]
dir: Option<PathBuf>,
},
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cli_symbol_parsing() {
let args = vec!["rust-analyzer-cli", "symbol", "LspClient", "struct", "--exact"];
let cli = Cli::try_parse_from(args).unwrap();
assert_eq!(cli.port, 60094);
assert!(!cli.json);
match cli.command {
Commands::Symbol { name, kind, exact } => {
assert_eq!(name, "LspClient");
assert_eq!(kind, "struct");
assert!(exact);
}
_ => panic!("Expected Symbol command"),
}
}
#[test]
fn test_cli_outline_parsing() {
let args = vec!["rust-analyzer-cli", "--json", "outline", "-f", "src/main.rs"];
let cli = Cli::try_parse_from(args).unwrap();
assert!(cli.json);
match cli.command {
Commands::Outline { file, file_list, output } => {
assert_eq!(file, Some(PathBuf::from("src/main.rs")));
assert!(file_list.is_none());
assert!(output.is_none());
}
_ => panic!("Expected Outline command"),
}
}
#[test]
fn test_cli_definition_parsing() {
let args = vec!["rust-analyzer-cli", "definition", "-f", "src/lib.rs", "-l", "10", "-c", "5"];
let cli = Cli::try_parse_from(args).unwrap();
match cli.command {
Commands::Definition { file, line, col } => {
assert_eq!(file, PathBuf::from("src/lib.rs"));
assert_eq!(line, 10);
assert_eq!(col, 5);
}
_ => panic!("Expected Definition command"),
}
}
#[test]
fn test_cli_init_skill_parsing() {
let args = vec!["rust-analyzer-cli", "init-skill", "-w", "/my/proj", "-d", "custom/SKILL.md"];
let cli = Cli::try_parse_from(args).unwrap();
match cli.command {
Commands::InitSkill { workspace, dir } => {
assert_eq!(workspace, PathBuf::from("/my/proj"));
assert_eq!(dir, Some(PathBuf::from("custom/SKILL.md")));
}
_ => panic!("Expected InitSkill command"),
}
}
}