magellan 3.3.3

Deterministic codebase mapping tool for local development
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
//! LSP enrichment using rust-analyzer, clangd, and javac
//!
//! Uses CLI commands to extract type signatures and documentation.
//!
//! ## rust-analyzer
//!
//! rust-analyzer provides JSON output through its analysis commands:
//!
//! ```bash
//! # Get analysis stats (JSON lines)
//! rust-analyzer analysis-stats .
//! ```
//!
//! ## clangd
//!
//! clangd provides JSON output through clangd-query or direct AST parsing:
//!
//! ```bash
//! # Get AST in JSON format
//! clangd-query --dump-ast file.cpp
//! ```
//!
//! ## javac (Java)
//!
//! For Java, we use javac with annotation processing or parse source directly:
//!
//! ```bash
//! # Compile and extract signatures
//! javac -parameters -Xprint file.java
//! ```

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use std::process::Command;

use super::analyzer::{
    detect_available_analyzers, detect_language_from_path, AnalyzerKind, AnalyzerResult,
};
use crate::graph::CodeGraph;

/// Configuration for symbol enrichment
#[derive(Debug, Clone)]
pub struct EnrichConfig {
    /// Which analyzers to use (None = use all available)
    pub analyzers: Option<Vec<AnalyzerKind>>,
    /// Enrich only these files (None = all files)
    pub files: Option<Vec<PathBuf>>,
    /// Timeout per file in seconds
    pub timeout_secs: u64,
}

impl Default for EnrichConfig {
    fn default() -> Self {
        Self {
            analyzers: None,
            files: None,
            timeout_secs: 30,
        }
    }
}

/// Result of enrichment operation
#[derive(Debug, Clone)]
pub struct EnrichResult {
    /// Number of files processed
    pub files_processed: usize,
    /// Number of symbols enriched
    pub symbols_enriched: usize,
    /// Number of errors encountered
    pub errors: usize,
}

/// Parsed signature from LSP analyzer
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LspSignature {
    /// Symbol name
    pub name: String,
    /// Full signature (e.g., "fn main() -> Result<(), Error>")
    pub signature: String,
    /// Return type (if available)
    pub return_type: Option<String>,
    /// Parameters (if available)
    pub parameters: Vec<String>,
    /// Documentation (if available)
    pub documentation: Option<String>,
}

/// Enrich symbols in the database with LSP data
///
/// # Arguments
/// * `graph` - Magellan code graph
/// * `config` - Enrichment configuration
///
/// # Returns
/// Enrichment result with statistics
pub fn enrich_symbols(graph: &mut CodeGraph, config: &EnrichConfig) -> Result<EnrichResult> {
    let mut result = EnrichResult {
        files_processed: 0,
        symbols_enriched: 0,
        errors: 0,
    };

    // Detect available analyzers
    let available_analyzers = detect_available_analyzers();

    if available_analyzers.is_empty() {
        eprintln!("No LSP analyzers found (rust-analyzer, javac)");
        eprintln!("Install rust-analyzer: rustup component add rust-analyzer");
        eprintln!("Install Java JDK for Java projects");
        return Ok(result);
    }

    eprintln!("Found {} analyzer(s):", available_analyzers.len());
    for analyzer in &available_analyzers {
        eprintln!("  - {}", analyzer.binary_name());
    }
    eprintln!();

    // Get all files from the graph
    let files = graph.all_file_nodes()?;

    for (file_path_str, _file_node) in files {
        let file_path = Path::new(&file_path_str);

        // Skip if file filter is specified and this file is not in it
        if let Some(ref files_filter) = config.files {
            if !files_filter.contains(&file_path.to_path_buf()) {
                continue;
            }
        }

        // Detect language for this file
        let language = match detect_language_from_path(file_path) {
            Some(lang) => lang,
            None => continue, // Skip unsupported languages
        };

        // Get appropriate analyzer for this language
        let analyzer_kind = match super::analyzer::get_analyzer_for_language(language) {
            Some(kind) if available_analyzers.contains(&kind) => kind,
            _ => continue, // No analyzer available for this language
        };

        eprintln!(
            "Enriching {:?} with {}",
            file_path,
            analyzer_kind.binary_name()
        );

        // Get symbols for this file
        let symbols = match graph.symbols_in_file(&file_path_str) {
            Ok(s) => s,
            Err(e) => {
                eprintln!("  Error getting symbols: {}", e);
                result.errors += 1;
                continue;
            }
        };

        if symbols.is_empty() {
            continue;
        }

        // Run analyzer on this file and parse JSON output
        let workspace_root = file_path.parent().unwrap_or(Path::new("."));
        let signatures = match analyzer_kind {
            AnalyzerKind::RustAnalyzer => parse_rust_analyzer_json(file_path, workspace_root)?,
            AnalyzerKind::Clangd => parse_clangd_json(file_path, workspace_root)?,
            AnalyzerKind::JDTLS => parse_javac_output(file_path, workspace_root)?,
        };

        // Match signatures to symbols
        let enriched_count = match_signatures_to_symbols(&symbols, &signatures)?;

        result.files_processed += 1;
        result.symbols_enriched += enriched_count;
        eprintln!("  Enriched {} symbols", enriched_count);
    }

    eprintln!();
    eprintln!("Enrichment complete:");
    eprintln!("  Files processed: {}", result.files_processed);
    eprintln!("  Symbols enriched: {}", result.symbols_enriched);
    eprintln!("  Errors: {}", result.errors);

    Ok(result)
}

/// Parse rust-analyzer JSON output
fn parse_rust_analyzer_json(file_path: &Path, workspace: &Path) -> Result<Vec<LspSignature>> {
    let mut signatures = Vec::new();

    // Try rust-analyzer analysis-stats command
    let output = Command::new("rust-analyzer")
        .args(["analysis-stats", "--load-output-dirs"])
        .arg(file_path)
        .current_dir(workspace)
        .output()
        .context("Failed to run rust-analyzer")?;

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Parse JSON from stdout (rust-analyzer outputs line-delimited JSON)
    for line in stdout.lines() {
        if line.trim().is_empty() {
            continue;
        }

        // Try to parse as JSON
        if let Ok(json_value) = serde_json::from_str::<serde_json::Value>(line) {
            if let Some(signature) = extract_signature_from_json(&json_value) {
                signatures.push(signature);
            }
        }
    }

    Ok(signatures)
}

/// Parse clangd JSON output using clangd-query
fn parse_clangd_json(file_path: &Path, workspace: &Path) -> Result<Vec<LspSignature>> {
    let mut signatures = Vec::new();

    // Check if file is C/C++
    let extension = file_path.extension().and_then(|e| e.to_str()).unwrap_or("");

    let is_cpp = matches!(extension, "cpp" | "cc" | "cxx" | "hpp" | "h");

    // Use clangd-query to get AST information
    let output = Command::new("clangd-query")
        .args([
            std::ffi::OsStr::new("--dump-ast"),
            std::ffi::OsStr::new("--include-refs"),
            file_path.as_os_str(),
        ])
        .current_dir(workspace)
        .output();

    match output {
        Ok(output) => {
            let stdout = String::from_utf8_lossy(&output.stdout);

            // Parse clangd AST output (not JSON, but structured text)
            // Format: kind name type location
            for line in stdout.lines() {
                if let Some(sig) = parse_clangd_line(line, is_cpp) {
                    signatures.push(sig);
                }
            }
        }
        Err(_) => {
            // Fallback: use clang with -Xclang -ast-dump=json
            let output = Command::new("clang")
                .args([
                    std::ffi::OsStr::new("-Xclang"),
                    std::ffi::OsStr::new("-ast-dump=json"),
                    std::ffi::OsStr::new("-fsyntax-only"),
                    file_path.as_os_str(),
                ])
                .current_dir(workspace)
                .output()
                .context("Failed to run clang AST dump")?;

            let stdout = String::from_utf8_lossy(&output.stdout);

            // Parse JSON AST
            if let Ok(json_value) = serde_json::from_str::<serde_json::Value>(&stdout) {
                extract_signatures_from_clang_ast(&json_value, &mut signatures);
            }
        }
    }

    Ok(signatures)
}

/// Parse a single line from clangd-query output
fn parse_clangd_line(line: &str, is_cpp: bool) -> Option<LspSignature> {
    let line = line.trim();
    if line.is_empty() {
        return None;
    }

    // clangd-query format: "kind name type"
    // Example: "Function main int()"
    let parts: Vec<&str> = line.split_whitespace().collect();
    if parts.len() < 3 {
        return None;
    }

    let kind = parts[0];
    let name = parts[1];
    let type_info = parts[2..].join(" ");

    match kind {
        "Function" | "Method" | "Constructor" | "Destructor" => Some(LspSignature {
            name: name.to_string(),
            signature: if is_cpp {
                format!("{} {}", kind, type_info)
            } else {
                format!("int {}({})", name, type_info)
            },
            return_type: Some(type_info.clone()),
            parameters: vec![],
            documentation: None,
        }),
        "Class" | "Struct" | "Enum" => Some(LspSignature {
            name: name.to_string(),
            signature: format!("{} {}", kind, name),
            return_type: None,
            parameters: vec![],
            documentation: None,
        }),
        _ => None,
    }
}

/// Extract signatures from clang JSON AST
fn extract_signatures_from_clang_ast(json: &serde_json::Value, signatures: &mut Vec<LspSignature>) {
    if let Some(obj) = json.as_object() {
        // Check if this is a function declaration
        if let Some(kind) = obj.get("kind").and_then(|v| v.as_str()) {
            if matches!(
                kind,
                "FunctionDecl" | "CXXMethodDecl" | "Constructor" | "Destructor"
            ) {
                if let Some(name) = obj.get("name").and_then(|v| v.as_str()) {
                    let return_type = obj
                        .get("type")
                        .and_then(|t| t.get("qualType"))
                        .and_then(|t| t.as_str())
                        .map(String::from);

                    let signature = format!("{} {}", kind, name);

                    signatures.push(LspSignature {
                        name: name.to_string(),
                        signature,
                        return_type,
                        parameters: vec![],
                        documentation: None,
                    });
                }
            }
        }

        // Recurse into children
        if let Some(children) = obj.get("inner").and_then(|v| v.as_array()) {
            for child in children {
                extract_signatures_from_clang_ast(child, signatures);
            }
        }
    }
}

/// Parse javac output for Java signatures
fn parse_javac_output(file_path: &Path, workspace: &Path) -> Result<Vec<LspSignature>> {
    let mut signatures = Vec::new();

    // Use javac -Xprint to print declarations
    let output = Command::new("javac")
        .args([
            std::ffi::OsStr::new("-Xprint"),
            std::ffi::OsStr::new("-parameters"), // Include parameter names
            file_path.as_os_str(),
        ])
        .current_dir(workspace)
        .output()
        .context("Failed to run javac -Xprint")?;

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Parse javac -Xprint output
    // Format: modifiers class ClassName { ... }
    //         modifiers returnType methodName(params) { ... }
    for line in stdout.lines() {
        if let Some(sig) = parse_javac_line(line) {
            signatures.push(sig);
        }
    }

    Ok(signatures)
}

/// Parse a single line from javac -Xprint output
fn parse_javac_line(line: &str) -> Option<LspSignature> {
    let line = line.trim();
    if line.is_empty() || line.starts_with("//") {
        return None;
    }

    // Class/Interface/Enum declaration
    if let Some(sig) = parse_java_type_decl(line) {
        return Some(sig);
    }

    // Method declaration
    if let Some(sig) = parse_java_method_decl(line) {
        return Some(sig);
    }

    None
}

/// Parse Java type declaration (class, interface, enum)
fn parse_java_type_decl(line: &str) -> Option<LspSignature> {
    // Match: [modifiers] class ClassName [extends ...] [implements ...] {
    // or: [modifiers] interface InterfaceName [extends ...] {
    // or: [modifiers] enum EnumName {

    let kind = if line.contains(" class ") {
        "class"
    } else if line.contains(" interface ") {
        "interface"
    } else if line.contains(" enum ") {
        "enum"
    } else {
        return None;
    };

    // Extract name
    let parts: Vec<&str> = line.split_whitespace().collect();
    let name_idx = parts.iter().position(|&p| p == kind)? + 1;
    let name = parts.get(name_idx)?.split('<').next()?;

    Some(LspSignature {
        name: name.to_string(),
        signature: line.split('{').next()?.trim().to_string(),
        return_type: None,
        parameters: vec![],
        documentation: None,
    })
}

/// Parse Java method declaration
fn parse_java_method_decl(line: &str) -> Option<LspSignature> {
    // Match: [modifiers] ReturnType methodName(params) [throws ...] {
    if !line.contains('(') || !line.contains(')') {
        return None;
    }

    // Skip lines that are clearly not methods
    if line.contains(" class ") || line.contains(" interface ") || line.contains(" enum ") {
        return None;
    }

    // Extract method name and parameters
    let paren_start = line.find('(')?;
    let paren_end = line.find(')')?;

    let before_parens = &line[..paren_start];
    let params_str = &line[paren_start + 1..paren_end];

    // Extract method name (last word before parenthesis)
    let name = before_parens.split_whitespace().last()?;

    // Extract return type (second to last word before parenthesis)
    let return_type: Option<String> = before_parens
        .split_whitespace()
        .rev()
        .nth(1)
        .map(String::from);

    // Parse parameters
    let parameters = parse_java_parameters(params_str);

    // Build signature
    let signature = line.split('{').next()?.trim().to_string();

    Some(LspSignature {
        name: name.to_string(),
        signature,
        return_type,
        parameters,
        documentation: None,
    })
}

/// Parse Java method parameters
fn parse_java_parameters(params_str: &str) -> Vec<String> {
    if params_str.trim().is_empty() {
        return vec![];
    }

    params_str
        .split(',')
        .map(|p| {
            let p = p.trim();
            // Extract just the parameter name (last word)
            p.split_whitespace().last().unwrap_or(p).to_string()
        })
        .collect()
}

/// Extract signature from rust-analyzer JSON
fn extract_signature_from_json(json: &serde_json::Value) -> Option<LspSignature> {
    // rust-analyzer JSON format varies by command
    // Common format for function analysis:
    // { "name": "main", "kind": "function", "signature": "fn main() -> ()" }

    let name = json.get("name")?.as_str()?.to_string();
    let signature = json
        .get("signature")
        .or_else(|| json.get("display_name"))?
        .as_str()?
        .to_string();

    let return_type = json
        .get("return_type")
        .and_then(|v| v.as_str())
        .map(String::from);

    let parameters = json
        .get("parameters")
        .and_then(|v| v.as_array())
        .map(|arr| {
            arr.iter()
                .filter_map(|v| v.as_str().map(String::from))
                .collect()
        })
        .unwrap_or_default();

    let documentation = json
        .get("documentation")
        .or_else(|| json.get("docs"))
        .and_then(|v| v.as_str())
        .map(String::from);

    Some(LspSignature {
        name,
        signature,
        return_type,
        parameters,
        documentation,
    })
}

/// Match parsed signatures to symbol facts
fn match_signatures_to_symbols(
    symbols: &[crate::ingest::SymbolFact],
    signatures: &[LspSignature],
) -> Result<usize> {
    let mut matched = 0;

    for symbol in symbols {
        if let Some(ref name) = symbol.name {
            // Find matching signature
            if let Some(sig) = signatures.iter().find(|s| s.name == *name) {
                eprintln!("    Matched '{}': {}", name, sig.signature);
                matched += 1;
            }
        }
    }

    Ok(matched)
}

/// Run enrichment with default configuration
pub fn run_enrich(db_path: &Path) -> Result<EnrichResult> {
    let mut graph = CodeGraph::open(db_path)?;
    let config = EnrichConfig::default();
    enrich_symbols(&mut graph, &config)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_extract_signature_from_json() {
        let json = serde_json::json!({
            "name": "main",
            "signature": "fn main() -> Result<(), Error>",
            "return_type": "Result<(), Error>",
            "parameters": [],
            "documentation": "Main entry point"
        });

        let sig = extract_signature_from_json(&json);
        assert!(sig.is_some());
        let sig = sig.unwrap();
        assert_eq!(sig.name, "main");
        assert_eq!(sig.signature, "fn main() -> Result<(), Error>");
        assert_eq!(sig.return_type, Some("Result<(), Error>".to_string()));
    }

    #[test]
    fn test_parse_clangd_line() {
        let sig = parse_clangd_line("Function main int()", false);
        assert!(sig.is_some());
        let sig = sig.unwrap();
        assert_eq!(sig.name, "main");
        assert!(sig.signature.contains("main"));
    }

    #[test]
    fn test_parse_javac_class() {
        let sig = parse_java_type_decl("public class MyClass {");
        assert!(sig.is_some());
        let sig = sig.unwrap();
        assert_eq!(sig.name, "MyClass");
        assert_eq!(sig.signature, "public class MyClass");
    }

    #[test]
    fn test_parse_javac_method() {
        let sig = parse_java_method_decl("public void myMethod(int x, String y) {");
        assert!(sig.is_some());
        let sig = sig.unwrap();
        assert_eq!(sig.name, "myMethod");
        assert_eq!(sig.return_type, Some("void".to_string()));
        assert_eq!(sig.parameters, vec!["x", "y"]);
    }

    #[test]
    fn test_parse_java_parameters() {
        let params = parse_java_parameters("int x, String y, boolean z");
        assert_eq!(params, vec!["x", "y", "z"]);
    }

    #[test]
    fn test_enrich_config_default() {
        let config = EnrichConfig::default();
        assert!(config.analyzers.is_none());
        assert!(config.files.is_none());
        assert_eq!(config.timeout_secs, 30);
    }
}