rust-analyzer-cli 0.4.1

A library and CLI tool built on top of rust-analyzer for codebase navigation
Documentation
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use anyhow::{Result, anyhow};
use clap::Parser;
use reqwest::Client;
use rust_analyzer_cli::{
    cli::{Cli, Commands},
    daemon,
    lsp::client::{extract_body_snippet, format_body_with_line_numbers},
    lsp::types::*,
};

use std::path::PathBuf;

#[tokio::main]
async fn main() -> Result<()> {
    tracing_subscriber::fmt::init();

    let cli = Cli::parse();
    let port = cli.port;
    let json_output = cli.json;
    let daemon_url = format!("http://127.0.0.1:{}", port);

    match cli.command {
        Commands::Daemon { workspace } => {
            daemon::server::start_daemon_server(workspace, port).await?;
        }
        Commands::Status => {
            let client = Client::new();
            let res = client
                .get(format!("{}/status", daemon_url))
                .send()
                .await
                .map_err(|_| daemon_not_running_error(port))?;
            let res = check_response(res).await?;

            let status: DaemonStatusResponse = res.json().await?;
            if json_output {
                println!("{}", serde_json::to_string_pretty(&status)?);
            } else {
                println!("rust-analyzer-cli Daemon Status:");
                println!(
                    "  Status: {}",
                    if status.ready {
                        "READY"
                    } else {
                        "INITIALIZING"
                    }
                );
                println!("  Workspace: {}", status.workspace_root);
                println!("  rust-analyzer PID: {}", status.process_id);
            }
        }
        Commands::Refresh => {
            let client = Client::new();
            let res = client
                .post(format!("{}/refresh", daemon_url))
                .send()
                .await
                .map_err(|_| daemon_not_running_error(port))?;
            let res = check_response(res).await?;

            let body: serde_json::Value = res.json().await?;
            if json_output {
                println!("{}", serde_json::to_string_pretty(&body)?);
            } else {
                println!("rust-analyzer session refreshed successfully.");
            }
        }
        Commands::Symbol {
            name,
            kind,
            exact,
            body,
            max_lines,
        } => {
            let client = Client::new();
            let req = SymbolQueryRequest { name, kind, exact };
            let res = client
                .post(format!("{}/api/symbol", daemon_url))
                .json(&req)
                .send()
                .await
                .map_err(|_| daemon_not_running_error(port))?;
            let res = check_response(res).await?;

            let mut symbols: Vec<SymbolItem> = res.json().await?;
            if body {
                for s in &mut symbols {
                    let (snippet, _, _, _) = extract_body_snippet(
                        PathBuf::from(&s.file).as_path(),
                        s.line,
                        s.line,
                        max_lines,
                    );
                    if !snippet.is_empty() {
                        s.body = Some(snippet);
                    }
                }
            }

            if json_output {
                println!("{}", serde_json::to_string_pretty(&symbols)?);
            } else if symbols.is_empty() {
                println!("No matching symbols found.");
            } else {
                println!("Found {} symbol(s):", symbols.len());
                for s in symbols {
                    let container = s
                        .container_name
                        .map(|c| format!(" ({})", c))
                        .unwrap_or_default();
                    println!(
                        "  [{}] {}{} -> {}:{}:{}",
                        s.kind, s.name, container, s.file, s.line, s.col
                    );
                    if let Some(ref b) = s.body {
                        println!("{}", format_body_with_line_numbers(b, s.line));
                    }
                }
            }
        }
        Commands::Outline {
            file,
            file_list,
            output,
            body,
            max_lines,
        } => {
            let client = Client::new();
            let mut target_files = Vec::new();

            if let Some(f) = file {
                target_files.push(f);
            } else if let Some(fl) = file_list {
                for item in fl.split(',') {
                    let trimmed = item.trim();
                    if !trimmed.is_empty() {
                        target_files.push(PathBuf::from(trimmed));
                    }
                }
            } else {
                return Err(anyhow!(
                    "Please specify --file <path> or --file-list <paths>"
                ));
            }

            let mut all_outlines = Vec::new();
            for f in &target_files {
                let req = OutlineQueryRequest { file: f.clone() };
                let res = client
                    .post(format!("{}/api/outline", daemon_url))
                    .json(&req)
                    .send()
                    .await
                    .map_err(|_| daemon_not_running_error(port))?;
                let res = check_response(res).await?;

                let mut items: Vec<OutlineItem> = res.json().await?;
                if body {
                    for item in &mut items {
                        let (snippet, _, _, _) = extract_body_snippet(
                            PathBuf::from(f).as_path(),
                            item.line,
                            item.line,
                            max_lines,
                        );
                        if !snippet.is_empty() {
                            item.body = Some(snippet);
                        }
                    }
                }
                all_outlines.push((f.to_string_lossy().to_string(), items));
            }

            let output_str = if json_output {
                serde_json::to_string_pretty(&all_outlines)?
            } else {
                let mut buf = String::new();
                for (path_str, items) in all_outlines {
                    buf.push_str(&format!("File: {}\n", path_str));
                    format_outline_items(&items, 1, &mut buf, body);
                    buf.push('\n');
                }
                buf
            };

            if let Some(out_path) = output {
                std::fs::write(&out_path, &output_str)?;
                println!("Outline successfully written to {}", out_path.display());
            } else {
                print!("{}", output_str);
            }
        }
        Commands::Definition {
            file,
            line,
            col,
            body,
            max_lines,
            no_line_numbers,
        } => {
            let client = Client::new();
            let req = DefinitionQueryRequest { file, line, col };
            let res = client
                .post(format!("{}/api/definition", daemon_url))
                .json(&req)
                .send()
                .await
                .map_err(|_| daemon_not_running_error(port))?;
            let res = check_response(res).await?;

            let mut items: Vec<DefinitionItem> = res.json().await?;
            if body {
                for item in &mut items {
                    let (snippet, _, _, _) = extract_body_snippet(
                        PathBuf::from(&item.file).as_path(),
                        item.line,
                        item.end_line,
                        max_lines,
                    );
                    if !snippet.is_empty() {
                        item.body = Some(snippet.clone());
                        item.snippet = Some(snippet);
                    }
                }
            }

            if json_output {
                println!("{}", serde_json::to_string_pretty(&items)?);
            } else if items.is_empty() {
                println!("No definition found at location.");
            } else {
                println!("Definition location(s):");
                for d in items {
                    println!(
                        "  {}:{}:{} (end {}:{})",
                        d.file, d.line, d.col, d.end_line, d.end_col
                    );
                    if let Some(ref b) = d.body {
                        if no_line_numbers {
                            println!("{}", b);
                        } else {
                            println!("{}", format_body_with_line_numbers(b, d.line));
                        }
                    }
                }
            }
        }
        Commands::Body {
            file,
            line,
            col,
            max_lines,
            no_line_numbers,
        } => {
            let client = Client::new();
            let req = BodyQueryRequest {
                file: file.clone(),
                line,
                col,
                max_lines,
            };
            let res = client
                .post(format!("{}/api/body", daemon_url))
                .json(&req)
                .send()
                .await
                .map_err(|_| daemon_not_running_error(port))?;
            let res = check_response(res).await?;

            let body_item: BodyItem = res.json().await?;

            if json_output {
                println!("{}", serde_json::to_string_pretty(&body_item)?);
            } else {
                println!(
                    "Body: {} ({}:{}-{}:{})",
                    body_item.file,
                    body_item.line,
                    body_item.col,
                    body_item.end_line,
                    body_item.end_col
                );
                if no_line_numbers {
                    println!("{}", body_item.body);
                } else {
                    println!(
                        "{}",
                        format_body_with_line_numbers(&body_item.body, body_item.line)
                    );
                }
                if body_item.is_truncated {
                    println!(
                        "... (truncated at {} lines, total {} lines)",
                        body_item.body.lines().count(),
                        body_item.total_lines
                    );
                }
            }
        }

        Commands::Cursor {
            file,
            line,
            col,
            mode,
            depth,
        } => {
            let client = Client::new();
            let req = CursorQueryRequest {
                file,
                line,
                col,
                mode,
                depth,
            };
            let res = client
                .post(format!("{}/api/cursor", daemon_url))
                .json(&req)
                .send()
                .await
                .map_err(|_| daemon_not_running_error(port))?;
            let res = check_response(res).await?;

            let items: Vec<CursorItem> = res.json().await?;
            if json_output {
                println!("{}", serde_json::to_string_pretty(&items)?);
            } else if items.is_empty() {
                println!("No cursor references or calls found.");
            } else {
                println!("Cursor query results ({} items):", items.len());
                for c in items {
                    let extra = c
                        .caller_or_callee
                        .map(|x| format!(" [{}]", x))
                        .unwrap_or_default();
                    println!(
                        "  [{}] {}{} -> {}:{}:{}",
                        c.kind, c.name, extra, c.file, c.line, c.col
                    );
                }
            }
        }
        Commands::TypeHierarchy {
            file,
            line,
            col,
            mode,
            depth,
        } => {
            let client = Client::new();
            let req = TypeHierarchyQueryRequest {
                file,
                line,
                col,
                mode,
                depth,
            };
            let res = client
                .post(format!("{}/api/type-hierarchy", daemon_url))
                .json(&req)
                .send()
                .await
                .map_err(|_| daemon_not_running_error(port))?;
            let res = check_response(res).await?;

            let items: Vec<TypeHierarchyItemResult> = res.json().await?;
            if json_output {
                println!("{}", serde_json::to_string_pretty(&items)?);
            } else if items.is_empty() {
                println!("No type hierarchy items found.");
            } else {
                println!("Type hierarchy results:");
                for t in items {
                    println!(
                        "  [{}] {} -> {}:{}:{}",
                        t.kind, t.name, t.file, t.line, t.col
                    );
                }
            }
        }

        Commands::Check { target } => {
            let client = Client::new();
            let req = CheckQueryRequest { target };
            let res = client
                .post(format!("{}/api/check", daemon_url))
                .json(&req)
                .send()
                .await
                .map_err(|_| daemon_not_running_error(port))?;
            let res = check_response(res).await?;

            let check_res: CheckResponse = res.json().await?;

            if json_output {
                println!("{}", serde_json::to_string_pretty(&check_res)?);
            } else {
                println!(
                    "Cargo Check Result: {}",
                    if check_res.success {
                        "PASSED"
                    } else {
                        "FAILED"
                    }
                );
                for diag in check_res.diagnostics {
                    let loc = match (diag.file, diag.line, diag.col) {
                        (Some(f), Some(l), Some(c)) => format!(" at {}:{}:{}", f, l, c),
                        (Some(f), _, _) => format!(" at {}", f),
                        _ => String::new(),
                    };
                    println!("  [{}] {}{}", diag.level.to_uppercase(), diag.message, loc);
                }
            }
        }
        Commands::InitSkill { workspace, dir } => {
            let skill_rel = dir.unwrap_or_else(|| {
                PathBuf::from(".agents/skills/rust-codebase-navigation/SKILL.md")
            });
            let target_path = workspace.join(skill_rel);
            if let Some(parent) = target_path.parent() {
                std::fs::create_dir_all(parent)?;
            }
            let skill_content = include_str!("../.agents/skills/rust-codebase-navigation/SKILL.md");
            std::fs::write(&target_path, skill_content)?;
            if json_output {
                println!(
                    "{}",
                    serde_json::json!({
                        "success": true,
                        "path": target_path.to_string_lossy()
                    })
                );
            } else {
                println!("Successfully installed skill to: {}", target_path.display());
            }
        }
    }

    Ok(())
}

async fn check_response(res: reqwest::Response) -> Result<reqwest::Response> {
    if !res.status().is_success() {
        let status = res.status();
        let err_text = res
            .text()
            .await
            .unwrap_or_else(|_| "Unknown error".to_string());
        return Err(anyhow!("Daemon error ({}): {}", status, err_text));
    }
    Ok(res)
}

fn daemon_not_running_error(port: u16) -> anyhow::Error {
    anyhow!(
        "rust-analyzer-cli daemon is not running on http://127.0.0.1:{}.\nStart the daemon first with:\n  rust-analyzer-cli daemon --workspace .",
        port
    )
}

fn format_outline_items(
    items: &[OutlineItem],
    indent_level: usize,
    buf: &mut String,
    show_body: bool,
) {
    let indent = "  ".repeat(indent_level);
    for item in items {
        let detail = item
            .detail
            .as_ref()
            .map(|d| format!(" ({})", d))
            .unwrap_or_default();
        buf.push_str(&format!(
            "{}[{}] {}{} at line {}:{}\n",
            indent, item.kind, item.name, detail, item.line, item.col
        ));
        if show_body && let Some(ref b) = item.body {
            buf.push_str(&format_body_with_line_numbers(b, item.line));
            buf.push('\n');
        }
        format_outline_items(&item.children, indent_level + 1, buf, show_body);
    }
}