leindex 1.6.1

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
//! Semantic drift detection for signature changes and API breakage

use crate::edit::ResolvedEditChange;
use crate::graph::pdg::NodeType;
use crate::graph::ProgramDependenceGraph;
use crate::parse::traits::{CodeIntelligence, SignatureInfo};
use crate::validation::Location;
use crate::validation::ValidationError;
use std::collections::HashMap;
use std::sync::Arc;

/// Type of semantic drift detected
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DriftType {
    /// Function/method signature changed
    SignatureChanged,
    /// Visibility modifier changed
    VisibilityChanged,
    /// Type changed (parameter or return type)
    TypeChanged,
    /// Symbol was removed
    Removed,
    /// Symbol was added
    Added,
}

/// A semantic drift item
#[derive(Debug, Clone)]
pub struct DriftItem {
    /// Name of the affected symbol
    pub symbol_name: String,
    /// Type of drift
    pub drift_type: DriftType,
    /// Location in source
    pub location: Location,
    /// Impact description
    pub impact_description: String,
}

impl DriftItem {
    /// Create a new drift item
    pub fn new(
        symbol_name: String,
        drift_type: DriftType,
        location: Location,
        impact_description: String,
    ) -> Self {
        Self {
            symbol_name,
            drift_type,
            location,
            impact_description,
        }
    }

    /// Create a signature changed drift
    pub fn signature_changed(
        symbol_name: String,
        location: Location,
        old_sig: &str,
        new_sig: &str,
    ) -> Self {
        Self {
            symbol_name,
            drift_type: DriftType::SignatureChanged,
            location,
            impact_description: format!("Signature changed from '{}' to '{}'", old_sig, new_sig),
        }
    }

    /// Create a type changed drift
    pub fn type_changed(symbol_name: String, location: Location, type_desc: String) -> Self {
        Self {
            symbol_name,
            drift_type: DriftType::TypeChanged,
            location,
            impact_description: format!("Type changed: {}", type_desc),
        }
    }

    /// Create a visibility changed drift
    pub fn visibility_changed(
        symbol_name: String,
        location: Location,
        old_visibility: &str,
        new_visibility: &str,
    ) -> Self {
        Self {
            symbol_name,
            drift_type: DriftType::VisibilityChanged,
            location,
            impact_description: format!(
                "Visibility changed from '{}' to '{}'",
                old_visibility, new_visibility
            ),
        }
    }

    /// Create a removed drift
    pub fn removed(symbol_name: String, location: Location) -> Self {
        Self {
            impact_description: format!("Symbol '{}' was removed", symbol_name),
            symbol_name,
            drift_type: DriftType::Removed,
            location,
        }
    }

    /// Create an added drift
    pub fn added(symbol_name: String, location: Location) -> Self {
        Self {
            impact_description: format!("New symbol '{}' added", symbol_name),
            symbol_name,
            drift_type: DriftType::Added,
            location,
        }
    }

    /// Check if this is a breaking change
    pub fn is_breaking(&self) -> bool {
        matches!(
            self.drift_type,
            DriftType::SignatureChanged | DriftType::TypeChanged | DriftType::Removed
        )
    }
}

/// Report of semantic drift analysis
#[derive(Debug, Clone)]
pub struct DriftReport {
    /// Breaking changes detected
    pub breaking_changes: Vec<DriftItem>,
    /// All API changes (breaking and non-breaking)
    pub api_changes: Vec<DriftItem>,
}

impl DriftReport {
    /// Create a new empty drift report
    pub fn new() -> Self {
        Self {
            breaking_changes: Vec::new(),
            api_changes: Vec::new(),
        }
    }

    /// Add a drift item to the report
    pub fn add_drift(&mut self, drift: DriftItem) {
        if drift.is_breaking() {
            self.breaking_changes.push(drift.clone());
        }
        self.api_changes.push(drift);
    }

    /// Check if there are any breaking changes
    pub fn has_breaking_changes(&self) -> bool {
        !self.breaking_changes.is_empty()
    }

    /// Get the count of all changes
    pub fn total_changes(&self) -> usize {
        self.api_changes.len()
    }
}

impl Default for DriftReport {
    fn default() -> Self {
        Self::new()
    }
}

/// Semantic drift analyzer
#[derive(Clone)]
pub struct SemanticDriftAnalyzer {
    /// PDG for analyzing the codebase
    pdg: Arc<ProgramDependenceGraph>,
}

impl SemanticDriftAnalyzer {
    /// Create a new semantic drift analyzer
    pub fn new(pdg: Arc<ProgramDependenceGraph>) -> Self {
        Self { pdg }
    }

    /// Analyze semantic drift for edit changes
    ///
    /// # Arguments
    /// * `changes` - Edit changes to analyze
    ///
    /// # Returns
    /// Vector of drift items detected
    pub fn analyze_semantic_drift(
        &self,
        changes: &[ResolvedEditChange],
    ) -> Result<Vec<DriftItem>, ValidationError> {
        let mut drift_items = Vec::new();

        for change in changes {
            // Extract signatures from original content
            let original_sigs = self.extract_signatures(change, &change.original_content)?;

            // Extract signatures from new content
            let new_sigs = self.extract_signatures(change, &change.new_content)?;

            // Compare signatures to detect drift
            drift_items.extend(self.compare_signatures(change, &original_sigs, &new_sigs)?);
        }

        Ok(drift_items)
    }

    /// Extract signatures from content
    fn extract_signatures(
        &self,
        change: &ResolvedEditChange,
        content: &str,
    ) -> Result<Vec<SignatureInfo>, ValidationError> {
        if content.is_empty() {
            return Ok(Vec::new());
        }

        let lang = change.infer_language();
        let source = content.as_bytes();

        // Get the appropriate parser for this language
        match lang {
            "python" => {
                use crate::parse::python::PythonParser;
                let parser = PythonParser::new();
                parser
                    .get_signatures(source)
                    .map_err(|e| ValidationError::Parse(format!("Failed to parse Python: {}", e)))
            }
            "javascript" => {
                use crate::parse::javascript::JavaScriptParser;
                let parser = JavaScriptParser::new();
                parser.get_signatures(source).map_err(|e| {
                    ValidationError::Parse(format!("Failed to parse JavaScript: {}", e))
                })
            }
            "typescript" => {
                use crate::parse::javascript::TypeScriptParser;
                let parser = TypeScriptParser::new();
                parser.get_signatures(source).map_err(|e| {
                    ValidationError::Parse(format!("Failed to parse TypeScript: {}", e))
                })
            }
            "rust" => {
                use crate::parse::rust::RustParser;
                let parser = RustParser::new();
                parser
                    .get_signatures(source)
                    .map_err(|e| ValidationError::Parse(format!("Failed to parse Rust: {}", e)))
            }
            "go" => {
                use crate::parse::go::GoParser;
                let parser = GoParser::new();
                parser
                    .get_signatures(source)
                    .map_err(|e| ValidationError::Parse(format!("Failed to parse Go: {}", e)))
            }
            "java" => {
                use crate::parse::java::JavaParser;
                let parser = JavaParser::new();
                parser
                    .get_signatures(source)
                    .map_err(|e| ValidationError::Parse(format!("Failed to parse Java: {}", e)))
            }
            _ => {
                // For unsupported languages, return empty
                Ok(Vec::new())
            }
        }
    }

    /// Compare signatures to detect drift
    fn compare_signatures(
        &self,
        change: &ResolvedEditChange,
        original: &[SignatureInfo],
        new: &[SignatureInfo],
    ) -> Result<Vec<DriftItem>, ValidationError> {
        let mut drift_items = Vec::new();

        let original_map: HashMap<_, _> = original.iter().map(|sig| (&sig.name, sig)).collect();

        let new_map: HashMap<_, _> = new.iter().map(|sig| (&sig.name, sig)).collect();

        // Check for removed symbols
        for name in original_map.keys() {
            if !new_map.contains_key(name) {
                // Find location in original content
                let location =
                    self.find_signature_location(change, original_map.get(name).unwrap());
                drift_items.push(DriftItem::removed(name.to_string(), location));
            }
        }

        // Check for added symbols
        for name in new_map.keys() {
            if !original_map.contains_key(name) {
                let location = self.find_signature_location(change, new_map.get(name).unwrap());
                drift_items.push(DriftItem::added(name.to_string(), location));
            }
        }

        // Check for modified symbols
        for name in original_map.keys() {
            if let Some(new_sig) = new_map.get(name) {
                if let Some(original_sig) = original_map.get(name) {
                    if let Some(drift) =
                        self.detect_signature_drift(change, original_sig, new_sig)?
                    {
                        drift_items.push(drift);
                    }
                }
            }
        }

        Ok(drift_items)
    }

    /// Detect drift between two signatures
    fn detect_signature_drift(
        &self,
        change: &ResolvedEditChange,
        original: &SignatureInfo,
        new: &SignatureInfo,
    ) -> Result<Option<DriftItem>, ValidationError> {
        let location = self.find_signature_location(change, new);

        // Check for signature changes (parameters)
        if original.parameters != new.parameters {
            return Ok(Some(DriftItem::signature_changed(
                new.name.clone(),
                location,
                &format!("{:?}", original.parameters),
                &format!("{:?}", new.parameters),
            )));
        }

        // Check for return type changes
        if original.return_type != new.return_type {
            return Ok(Some(DriftItem::type_changed(
                new.name.clone(),
                location,
                format!(
                    "Return type changed from {:?} to {:?}",
                    original.return_type, new.return_type
                ),
            )));
        }

        // Check for visibility changes
        if original.visibility != new.visibility {
            return Ok(Some(DriftItem::visibility_changed(
                new.name.clone(),
                location,
                &format!("{:?}", original.visibility),
                &format!("{:?}", new.visibility),
            )));
        }

        Ok(None)
    }

    /// Find the location of a signature in the edit change
    fn find_signature_location(
        &self,
        change: &ResolvedEditChange,
        sig: &SignatureInfo,
    ) -> Location {
        let byte_offset = sig.byte_range.0;
        let mut line = 1;
        let mut column = 1;

        for (i, byte) in change.new_content.bytes().enumerate() {
            if i == byte_offset {
                break;
            }
            if byte == b'\n' {
                line += 1;
                column = 1;
            } else {
                column += 1;
            }
        }

        Location { line, column }
    }

    /// Check if a symbol is part of the public API
    pub fn is_public_api(&self, symbol_name: &str) -> bool {
        if let Some(node_id) = self.pdg.find_by_symbol(symbol_name) {
            if let Some(node) = self.pdg.get_node(node_id) {
                // For now, consider all functions and classes as potential API
                // In a full implementation, this would check visibility modifiers
                return matches!(
                    node.node_type,
                    NodeType::Function | NodeType::Method | NodeType::Class
                );
            }
        }
        false
    }
}

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

    #[test]
    fn test_drift_type_equality() {
        assert_eq!(DriftType::SignatureChanged, DriftType::SignatureChanged);
        assert_ne!(DriftType::SignatureChanged, DriftType::TypeChanged);
    }

    #[test]
    fn test_drift_item_signature_changed() {
        let item = DriftItem::signature_changed(
            "my_func".to_string(),
            Location { line: 1, column: 1 },
            "old_sig",
            "new_sig",
        );
        assert_eq!(item.symbol_name, "my_func");
        assert_eq!(item.drift_type, DriftType::SignatureChanged);
        assert!(item.impact_description.contains("old_sig"));
        assert!(item.impact_description.contains("new_sig"));
        assert!(item.is_breaking());
    }

    #[test]
    fn test_drift_item_type_changed() {
        let item = DriftItem::type_changed(
            "my_func".to_string(),
            Location { line: 1, column: 1 },
            "return type changed".to_string(),
        );
        assert_eq!(item.drift_type, DriftType::TypeChanged);
        assert!(item.is_breaking());
    }

    #[test]
    fn test_drift_item_visibility_changed() {
        let item = DriftItem::visibility_changed(
            "my_func".to_string(),
            Location { line: 1, column: 1 },
            "private",
            "public",
        );
        assert_eq!(item.drift_type, DriftType::VisibilityChanged);
        // Visibility changes are not considered breaking in this implementation
        assert!(!item.is_breaking());
    }

    #[test]
    fn test_drift_item_removed() {
        let item = DriftItem::removed("my_func".to_string(), Location { line: 1, column: 1 });
        assert_eq!(item.drift_type, DriftType::Removed);
        assert!(item.is_breaking());
        assert!(item.impact_description.contains("removed"));
    }

    #[test]
    fn test_drift_item_added() {
        let item = DriftItem::added("new_func".to_string(), Location { line: 1, column: 1 });
        assert_eq!(item.drift_type, DriftType::Added);
        assert!(!item.is_breaking()); // Adding is not breaking
        assert!(item.impact_description.contains("added"));
    }

    #[test]
    fn test_drift_report_new() {
        let report = DriftReport::new();
        assert!(report.breaking_changes.is_empty());
        assert!(report.api_changes.is_empty());
        assert!(!report.has_breaking_changes());
        assert_eq!(report.total_changes(), 0);
    }

    #[test]
    fn test_drift_report_default() {
        let report = DriftReport::default();
        assert!(report.breaking_changes.is_empty());
    }

    #[test]
    fn test_drift_report_add_drift() {
        let mut report = DriftReport::new();
        let item = DriftItem::removed("foo".to_string(), Location { line: 1, column: 1 });
        report.add_drift(item);
        assert_eq!(report.total_changes(), 1);
        assert!(report.has_breaking_changes());
        assert_eq!(report.breaking_changes.len(), 1);
    }

    #[test]
    fn test_drift_report_add_non_breaking() {
        let mut report = DriftReport::new();
        let item = DriftItem::added("foo".to_string(), Location { line: 1, column: 1 });
        report.add_drift(item);
        assert_eq!(report.total_changes(), 1);
        assert!(!report.has_breaking_changes());
        assert_eq!(report.breaking_changes.len(), 0);
    }

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

    #[test]
    fn test_analyze_semantic_drift_empty_changes() {
        let pdg = Arc::new(ProgramDependenceGraph::new());
        let analyzer = SemanticDriftAnalyzer::new(pdg);
        let changes: &[ResolvedEditChange] = &[];
        let result = analyzer.analyze_semantic_drift(changes).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn test_is_public_api() {
        let mut pdg = ProgramDependenceGraph::new();
        let _node_id = pdg.add_node(crate::graph::Node {
            id: "my_func".to_string(),
            node_type: NodeType::Function,
            name: "my_func".to_string(),
            file_path: Arc::from("test.py"),
            byte_range: (0, 10),
            complexity: 1,
            language: "python".to_string(),
        });

        let analyzer = SemanticDriftAnalyzer::new(Arc::new(pdg));
        assert!(analyzer.is_public_api("my_func"));
        assert!(!analyzer.is_public_api("nonexistent"));
    }

    #[test]
    fn test_find_signature_location() {
        let pdg = Arc::new(ProgramDependenceGraph::new());
        let analyzer = SemanticDriftAnalyzer::new(pdg);

        let change = ResolvedEditChange::new(
            PathBuf::from("test.py"),
            String::new(),
            "def foo():\n    pass".to_string(),
        );

        // Create a signature at position 0
        let sig = SignatureInfo {
            name: "foo".to_string(),
            qualified_name: "foo".to_string(),
            parameters: vec![],
            return_type: None,
            visibility: crate::parse::traits::Visibility::Public,
            is_async: false,
            is_method: false,
            docstring: None,
            calls: vec![],
            imports: vec![],
            byte_range: (0, 14),
            cyclomatic_complexity: 0,
        };

        let location = analyzer.find_signature_location(&change, &sig);
        assert_eq!(location.line, 1);
        assert_eq!(location.column, 1);
    }
}