leindex 1.6.0

LeIndex MCP and semantic code search engine for AI tools and large codebases
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
//! Reference integrity checking via legraphe

use crate::edit::ResolvedEditChange;
use crate::graph::ProgramDependenceGraph;
use crate::validation::Location;
use crate::validation::ValidationError;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::Arc;

/// Type of reference issue found
#[derive(Debug, Clone, PartialEq)]
pub enum ReferenceIssueType {
    /// Imported symbol doesn't exist
    BrokenImport {
        /// The symbol that couldn't be found
        symbol: String,
    },
    /// Used but not defined reference
    UndefinedReference {
        /// The undefined name
        name: String,
    },
    /// New cycle introduced in dependencies
    CyclicDependency {
        /// The cycle of dependencies
        cycle: Vec<String>,
    },
}

/// A reference issue found during validation
#[derive(Debug, Clone)]
pub struct ReferenceIssue {
    /// Type of reference issue
    pub issue_type: ReferenceIssueType,
    /// File path where the issue occurred
    pub file_path: PathBuf,
    /// Location in the file
    pub location: Location,
    /// Description of the issue
    pub description: String,
}

impl ReferenceIssue {
    /// Create a new reference issue
    pub fn new(
        issue_type: ReferenceIssueType,
        file_path: PathBuf,
        location: Location,
        description: String,
    ) -> Self {
        Self {
            issue_type,
            file_path,
            location,
            description,
        }
    }

    /// Create a broken import issue
    pub fn broken_import(symbol: String, file_path: PathBuf, location: Location) -> Self {
        Self {
            issue_type: ReferenceIssueType::BrokenImport {
                symbol: symbol.clone(),
            },
            file_path,
            location,
            description: format!("Import '{}' not found in project", symbol),
        }
    }

    /// Create an undefined reference issue
    pub fn undefined_reference(name: String, file_path: PathBuf, location: Location) -> Self {
        Self {
            issue_type: ReferenceIssueType::UndefinedReference { name: name.clone() },
            file_path,
            location,
            description: format!("Undefined reference '{}'", name),
        }
    }

    /// Create a cyclic dependency issue
    pub fn cyclic_dependency(cycle: Vec<String>, file_path: PathBuf) -> Self {
        Self {
            issue_type: ReferenceIssueType::CyclicDependency {
                cycle: cycle.clone(),
            },
            file_path,
            location: Location { line: 1, column: 1 },
            description: format!("Cyclic dependency detected: {}", cycle.join(" -> ")),
        }
    }
}

/// Reference checker using PDG
#[derive(Clone)]
pub struct ReferenceChecker {
    /// PDG for reference checking
    pdg: Arc<ProgramDependenceGraph>,
}

impl ReferenceChecker {
    /// Create a new reference checker
    pub fn new(pdg: Arc<ProgramDependenceGraph>) -> Self {
        Self { pdg }
    }

    /// Check references for edit changes
    ///
    /// # Arguments
    /// * `changes` - Edit changes to check
    ///
    /// # Returns
    /// Vector of reference issues found
    pub fn check_references(
        &self,
        changes: &[ResolvedEditChange],
    ) -> Result<Vec<ReferenceIssue>, ValidationError> {
        let mut issues = Vec::new();

        for change in changes {
            // Extract imports from new content
            let imports = self.extract_imports(change);

            // Check each import against the PDG
            for import in imports {
                if !self.import_exists_in_pdg(&import) {
                    issues.push(ReferenceIssue::broken_import(
                        import,
                        change.file_path.clone(),
                        Location { line: 1, column: 1 },
                    ));
                }
            }

            // Check for undefined references
            let undefined = self.find_undefined_references(change);
            issues.extend(undefined);
        }

        // Check for new cycles
        issues.extend(self.check_for_cycles(changes)?);

        Ok(issues)
    }

    /// Extract imports from edit change content
    fn extract_imports(&self, change: &ResolvedEditChange) -> Vec<String> {
        let mut imports = Vec::new();
        let lang = change.infer_language();

        match lang {
            "python" => {
                for line in change.new_content.lines() {
                    let line = line.trim();
                    if line.starts_with("import ") || line.starts_with("from ") {
                        // Extract the import path
                        if let Some(rest) = line.strip_prefix("import ") {
                            let import_path = rest.split(" as ").next().unwrap_or(rest).trim();
                            imports.push(import_path.to_string());
                        } else if let Some(rest) = line.strip_prefix("from ") {
                            let import_path = rest.split(" import ").next().unwrap_or(rest).trim();
                            imports.push(import_path.to_string());
                        }
                    }
                }
            }
            "javascript" | "typescript" => {
                for line in change.new_content.lines() {
                    let line = line.trim();
                    if line.contains("import ") && line.contains("from ") {
                        // Extract from '...' or from "..."
                        if let Some(start) = line.find("from ") {
                            let rest = &line[start + 5..];
                            let quote = rest.chars().next();
                            if let Some('"') | Some('\'') = quote {
                                if let Some(end) = rest[1..].find(quote.unwrap()) {
                                    imports.push(rest[1..end + 1].to_string());
                                }
                            }
                        }
                    } else if line.contains("require(") {
                        // Extract require('...') or require("...")
                        if let Some(start) = line.find("require(") {
                            let rest = &line[start + 8..]; // Skip "require("
                            if let Some(end) = rest.find(')') {
                                let inner = &rest[..end];
                                let inner = inner.trim();
                                if inner.starts_with('"') || inner.starts_with('\'') {
                                    imports.push(inner[1..inner.len() - 1].to_string());
                                }
                            }
                        }
                    }
                }
            }
            "rust" => {
                for line in change.new_content.lines() {
                    let line = line.trim();
                    if line.starts_with("use ") {
                        let import_path = line
                            .trim_start_matches("use ")
                            .trim_end_matches(';')
                            .trim()
                            .to_string();
                        imports.push(import_path);
                    } else if line.starts_with("mod ") {
                        let mod_name = line
                            .trim_start_matches("mod ")
                            .trim_end_matches(';')
                            .trim()
                            .to_string();
                        imports.push(mod_name);
                    }
                }
            }
            "go" => {
                for line in change.new_content.lines() {
                    let line = line.trim();
                    if line.starts_with("\"") && line.contains("\"") {
                        let import_path = line.trim_matches('"');
                        imports.push(import_path.to_string());
                    }
                }
            }
            _ => {
                // For other languages, use basic regex-like patterns
                // This is a simplified approach
            }
        }

        imports
    }

    /// Check if an import exists in the PDG
    fn import_exists_in_pdg(&self, import: &str) -> bool {
        // Check if the import exists as a module or symbol in the PDG
        let import_lower = import.to_lowercase();

        // Check if any node in the PDG matches the import
        for node_id in self.pdg.node_indices() {
            if let Some(node) = self.pdg.get_node(node_id) {
                let node_name_lower = node.name.to_lowercase();
                if node_name_lower.contains(&import_lower)
                    || import_lower.contains(&node_name_lower)
                {
                    return true;
                }
            }
        }

        // Also check file paths
        for node_id in self.pdg.node_indices() {
            if let Some(node) = self.pdg.get_node(node_id) {
                let file_path_lower = node.file_path.to_lowercase();
                if file_path_lower.contains(&import_lower) {
                    return true;
                }
            }
        }

        false
    }

    /// Find undefined references in the edit change
    fn find_undefined_references(&self, change: &ResolvedEditChange) -> Vec<ReferenceIssue> {
        let mut issues = Vec::new();
        let lang = change.infer_language();

        match lang {
            "python" => {
                // Find function calls that might be undefined
                for (line_num, line) in change.new_content.lines().enumerate() {
                    // Simple heuristic: look for function calls
                    let calls = self.extract_python_function_calls(line);
                    for call in calls {
                        if !self.symbol_exists_in_pdg(&call) {
                            issues.push(ReferenceIssue::undefined_reference(
                                call,
                                change.file_path.clone(),
                                Location {
                                    line: line_num + 1,
                                    column: 1,
                                },
                            ));
                        }
                    }
                }
            }
            _ => {
                // For other languages, we'd need more sophisticated analysis
            }
        }

        issues
    }

    /// Extract Python function calls from a line
    fn extract_python_function_calls(&self, line: &str) -> Vec<String> {
        let mut calls = Vec::new();

        // Simple regex-like extraction for function_name( patterns
        let chars: Vec<char> = line.chars().collect();
        let mut i = 0;

        while i < chars.len() {
            if chars[i].is_alphabetic() || chars[i] == '_' {
                let start = i;
                while i < chars.len() && (chars[i].is_alphanumeric() || chars[i] == '_') {
                    i += 1;
                }
                let name: String = chars[start..i].iter().collect();

                // Check if followed by '('
                if i < chars.len() && chars[i] == '(' {
                    // Skip built-ins
                    if !self.is_python_builtin(&name) {
                        calls.push(name);
                    }
                }
            } else {
                i += 1;
            }
        }

        calls
    }

    /// Check if a name is a Python built-in
    fn is_python_builtin(&self, name: &str) -> bool {
        const BUILTINS: &[&str] = &[
            "print",
            "len",
            "str",
            "int",
            "float",
            "list",
            "dict",
            "set",
            "tuple",
            "range",
            "enumerate",
            "zip",
            "map",
            "filter",
            "sorted",
            "reversed",
            "sum",
            "min",
            "max",
            "abs",
            "all",
            "any",
            "bool",
            "type",
            "isinstance",
            "open",
            "input",
            "exit",
            "quit",
            "help",
            "dir",
            "vars",
            "id",
            "super",
            "self",
            "cls",
            "None",
            "True",
            "False",
            "await",
            "async",
            "if",
            "else",
            "elif",
            "for",
            "while",
            "def",
            "class",
            "return",
            "yield",
            "import",
            "from",
            "as",
            "with",
            "try",
            "except",
            "finally",
            "raise",
            "assert",
            "pass",
            "break",
            "continue",
            "and",
            "or",
            "not",
            "in",
            "is",
            "lambda",
            "global",
            "nonlocal",
            "del",
        ];
        BUILTINS.contains(&name)
    }

    /// Check if a symbol exists in the PDG
    fn symbol_exists_in_pdg(&self, symbol: &str) -> bool {
        self.pdg.find_by_symbol(symbol).is_some()
    }

    /// Check for cycles introduced by the changes
    fn check_for_cycles(
        &self,
        changes: &[ResolvedEditChange],
    ) -> Result<Vec<ReferenceIssue>, ValidationError> {
        let mut issues = Vec::new();

        // Build a dependency graph from the changes
        let mut deps: HashMap<String, Vec<String>> = HashMap::new();
        let mut visited: HashSet<String> = HashSet::new();
        let mut rec_stack: HashSet<String> = HashSet::new();

        for change in changes {
            let file_name = change.file_path.to_string_lossy().to_string();
            let imports = self.extract_imports(change);

            deps.insert(file_name.clone(), imports);
        }

        // Check for cycles using DFS
        for node in deps.keys() {
            if !visited.contains(node) {
                if let Some(cycle) = self.detect_cycle(node, &deps, &mut visited, &mut rec_stack) {
                    issues.push(ReferenceIssue::cyclic_dependency(
                        cycle,
                        PathBuf::from(node),
                    ));
                }
            }
        }

        Ok(issues)
    }

    /// Detect cycle using DFS
    fn detect_cycle(
        &self,
        node: &str,
        deps: &HashMap<String, Vec<String>>,
        visited: &mut HashSet<String>,
        rec_stack: &mut HashSet<String>,
    ) -> Option<Vec<String>> {
        visited.insert(node.to_string());
        rec_stack.insert(node.to_string());

        if let Some(neighbors) = deps.get(node) {
            for neighbor in neighbors {
                if !visited.contains(neighbor) {
                    if let Some(cycle) = self.detect_cycle(neighbor, deps, visited, rec_stack) {
                        let mut result = vec![node.to_string()];
                        result.extend(cycle);
                        return Some(result);
                    }
                } else if rec_stack.contains(neighbor) {
                    // Found a cycle
                    return Some(vec![neighbor.to_string(), node.to_string()]);
                }
            }
        }

        rec_stack.remove(node);
        None
    }
}

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

    #[test]
    fn test_reference_issue_broken_import() {
        let issue = ReferenceIssue::broken_import(
            "missing_module".to_string(),
            PathBuf::from("test.py"),
            Location { line: 1, column: 1 },
        );
        assert!(matches!(
            issue.issue_type,
            ReferenceIssueType::BrokenImport { .. }
        ));
        assert_eq!(issue.file_path, PathBuf::from("test.py"));
    }

    #[test]
    fn test_reference_issue_undefined_reference() {
        let issue = ReferenceIssue::undefined_reference(
            "undefined_func".to_string(),
            PathBuf::from("test.py"),
            Location {
                line: 5,
                column: 10,
            },
        );
        assert!(matches!(
            issue.issue_type,
            ReferenceIssueType::UndefinedReference { .. }
        ));
    }

    #[test]
    fn test_reference_issue_cyclic_dependency() {
        let cycle = vec!["a".to_string(), "b".to_string(), "a".to_string()];
        let issue = ReferenceIssue::cyclic_dependency(cycle, PathBuf::from("test.py"));
        assert!(matches!(
            issue.issue_type,
            ReferenceIssueType::CyclicDependency { .. }
        ));
        assert!(issue.description.contains("a -> b -> a"));
    }

    #[test]
    fn test_reference_checker_new() {
        let pdg = Arc::new(ProgramDependenceGraph::new());
        let _checker = ReferenceChecker::new(pdg);
        // Just verify it was created
        assert!(true);
    }

    #[test]
    fn test_extract_imports_python() {
        let pdg = Arc::new(ProgramDependenceGraph::new());
        let checker = ReferenceChecker::new(pdg);

        let change = ResolvedEditChange::new(
            PathBuf::from("test.py"),
            String::new(),
            "import os\nimport sys\nfrom collections import defaultdict\n".to_string(),
        );

        let imports = checker.extract_imports(&change);
        assert_eq!(imports, vec!["os", "sys", "collections"]);
    }

    #[test]
    fn test_extract_imports_javascript() {
        let pdg = Arc::new(ProgramDependenceGraph::new());
        let checker = ReferenceChecker::new(pdg);

        let change = ResolvedEditChange::new(
            PathBuf::from("test.js"),
            String::new(),
            "import { foo } from 'bar';\nconst baz = require('qux');\n".to_string(),
        );

        let imports = checker.extract_imports(&change);
        assert_eq!(imports.len(), 2);
    }

    #[test]
    fn test_extract_imports_rust() {
        let pdg = Arc::new(ProgramDependenceGraph::new());
        let checker = ReferenceChecker::new(pdg);

        let change = ResolvedEditChange::new(
            PathBuf::from("test.rs"),
            String::new(),
            "use std::collections::HashMap;\nmod my_module;\n".to_string(),
        );

        let imports = checker.extract_imports(&change);
        assert_eq!(imports, vec!["std::collections::HashMap", "my_module"]);
    }

    #[test]
    fn test_is_python_builtin() {
        let pdg = Arc::new(ProgramDependenceGraph::new());
        let checker = ReferenceChecker::new(pdg);

        assert!(checker.is_python_builtin("print"));
        assert!(checker.is_python_builtin("len"));
        assert!(checker.is_python_builtin("if"));
        assert!(!checker.is_python_builtin("my_function"));
    }

    #[test]
    fn test_extract_python_function_calls() {
        let pdg = Arc::new(ProgramDependenceGraph::new());
        let checker = ReferenceChecker::new(pdg);

        let calls = checker.extract_python_function_calls("x = foo() + bar()");
        assert_eq!(calls, vec!["foo", "bar"]);

        let calls = checker.extract_python_function_calls("print('hello')");
        assert!(calls.is_empty()); // print is a builtin

        let calls = checker.extract_python_function_calls("my_func()");
        assert_eq!(calls, vec!["my_func"]);
    }

    #[test]
    fn test_reference_issue_type_equality() {
        assert_eq!(
            ReferenceIssueType::BrokenImport {
                symbol: "foo".to_string()
            },
            ReferenceIssueType::BrokenImport {
                symbol: "foo".to_string()
            }
        );
        assert_ne!(
            ReferenceIssueType::BrokenImport {
                symbol: "foo".to_string()
            },
            ReferenceIssueType::UndefinedReference {
                name: "foo".to_string()
            }
        );
    }
}