opengrep 1.1.0

Advanced AST-aware code search tool with tree-sitter parsing and AI integration capabilities
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
//! Main search engine implementation
//!
//! This module contains the `SearchEngine` which orchestrates the entire search process,
//! managing file discovery, parallel searching, and result aggregation.

use anyhow::Result;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::mpsc;
use tracing::{error, info, warn};

use crate::config::Config;
use super::{SearchOptions, SearchResult, SearchStats};

/// The main search engine that coordinates all search operations
pub struct SearchEngine {
    config: Arc<Config>,
}

impl SearchEngine {
    /// Create a new search engine with the given configuration
    pub fn new(config: Config) -> Self {
        Self {
            config: Arc::new(config),
        }
    }
    
    /// Perform a search with the given pattern across the specified paths
    pub async fn search(
        &self,
        pattern: &str,
        paths: &[PathBuf],
    ) -> Result<Vec<SearchResult>> {
        let start_time = Instant::now();
        
        // Validate input
        if pattern.is_empty() {
            anyhow::bail!("Search pattern cannot be empty");
        }
        
        // Create search options
        let options = SearchOptions::new(pattern, self.config.clone())?;
        
        info!("Starting search for pattern: '{}'", pattern);
        info!("Search paths: {:?}", paths);
        
        // Determine paths to search
        let search_paths = if paths.is_empty() {
            vec![PathBuf::from(".")]
        } else {
            paths.to_vec()
        };
        
        // Create channels for communication
        let (file_tx, file_rx) = mpsc::channel(1000);
        let (result_tx, mut result_rx) = mpsc::channel(100);
        
        // Spawn file discovery task
        let discovery_handle = {
            let search_paths = search_paths.clone();
            let file_tx = file_tx.clone();
            
            tokio::spawn(async move {
                // Use simple walker for now
                for path in search_paths {
                    match super::walker::walk_simple(path) {
                        Ok(files) => {
                            for file_path in files {
                                if file_tx.send(file_path).await.is_err() {
                                    break;
                                }
                            }
                        }
                        Err(e) => {
                            error!("File discovery failed: {}", e);
                        }
                    }
                }
            })
        };
        
        // Spawn searcher tasks - they will all share the same receiver
        let file_rx = Arc::new(tokio::sync::Mutex::new(file_rx));
        let searcher_handles: Vec<_> = (0..self.config.search.threads)
            .map(|worker_id| {
                let file_rx = file_rx.clone();
                let options = options.clone();
                let result_tx = result_tx.clone();
                
                tokio::spawn(async move {
                    let mut files_processed = 0;
                    
                    loop {
                        let file_path = {
                            let mut guard = file_rx.lock().await;
                            guard.recv().await
                        };

                        if let Some(file_path) = file_path {
                            files_processed += 1;
                            
                            match super::search_file(&file_path, &options).await {
                                Ok(Some(result)) => {
                                    if let Err(e) = result_tx.send(Ok(result)).await {
                                        error!("Failed to send search result: {}", e);
                                        break;
                                    }
                                }
                                Ok(None) => {
                                    // No matches found, continue
                                }
                                Err(e) => {
                                    warn!("Search failed for {}: {}", file_path.display(), e);
                                    if let Err(e) = result_tx.send(Err(e)).await {
                                        error!("Failed to send error result: {}", e);
                                        break;
                                    }
                                }
                            }
                        } else {
                            break;
                        }
                    }
                    
                    info!("Worker {} processed {} files", worker_id, files_processed);
                })
            })
            .collect();
        
        // Drop original senders so receivers complete when done
        drop(file_tx);
        drop(result_tx);
        
        // Collect results
        let mut results = Vec::new();
        let mut errors = Vec::new();
        
        while let Some(result) = result_rx.recv().await {
            match result {
                Ok(search_result) => results.push(search_result),
                Err(e) => errors.push(e),
            }
        }
        
        // Wait for all tasks to complete
        let _ = discovery_handle.await;
        for handle in searcher_handles {
            let _ = handle.await;
        }
        
        let search_duration = start_time.elapsed();
        
        // Log search completion
        info!(
            "Search completed in {}ms: {} files with matches, {} total matches",
            search_duration.as_millis(),
            results.len(),
            results.iter().map(|r| r.matches.len()).sum::<usize>()
        );
        
        if !errors.is_empty() {
            warn!("Search completed with {} errors", errors.len());
        }
        
        // Add AI insights if enabled
        #[cfg(feature = "ai")]
        if self.should_add_ai_insights() {
            self.add_ai_insights(&mut results).await?;
        }
        
        // Sort results by relevance
        self.sort_results(&mut results);
        
        Ok(results)
    }
    
    /// Start an interactive search session
    pub async fn interactive_search(&self) -> Result<Vec<SearchResult>> {
        use dialoguer::{Input, Select, MultiSelect, Confirm};
        
        println!("OpenGrep Interactive Search");
        println!("==============================");
        
        // Get search pattern
        let pattern: String = Input::new()
            .with_prompt("Enter search pattern")
            .interact()?;
        
        if pattern.is_empty() {
            anyhow::bail!("Search pattern cannot be empty");
        }
        
        // Get search options
        let search_type = Select::new()
            .with_prompt("Search type")
            .items(&["Literal", "Regular Expression", "AI-assisted"])
            .default(0)
            .interact()?;
        
        // Get file filters
        let languages = if Confirm::new()
            .with_prompt("Filter by programming language?")
            .default(false)
            .interact()?
        {
            let available_languages = [
                "rust", "python", "javascript", "typescript", "go", "java",
                "c", "cpp", "csharp", "ruby", "bash", "yaml", "json", "html", "css"
            ];
            
            let selected = MultiSelect::new()
                .with_prompt("Select languages")
                .items(&available_languages)
                .interact()?;
            
            selected.into_iter().map(|i| available_languages[i].to_string()).collect()
        } else {
            vec![]
        };
        
        // Get paths
        let paths: String = Input::new()
            .with_prompt("Search paths (comma-separated, empty for current directory)")
            .default(".".to_string())
            .interact()?;
        
        let search_paths: Vec<PathBuf> = if paths.trim().is_empty() || paths.trim() == "." {
            vec![PathBuf::from(".")]
        } else {
            paths.split(',').map(|p| PathBuf::from(p.trim())).collect()
        };
        
        // Show AST context?
        let show_ast_context = Confirm::new()
            .with_prompt("Show AST context?")
            .default(false)
            .interact()?;
        
        // Create modified configuration
        let mut config = (*self.config).clone();
        config.search.regex = search_type == 1;
        config.output.show_ast_context = show_ast_context;
        
        // Apply language filter
        if !languages.is_empty() {
            info!("Filtering for languages: {:?}", languages);
        }
        
        println!("\nStarting search...\n");
        
        // Perform search based on type
        let results = match search_type {
            0 | 1 => {
                let engine = SearchEngine::new(config);
                engine.search(&pattern, &search_paths).await?
            }
            2 => {
                #[cfg(feature = "ai")]
                {
                    self.ai_assisted_search(&pattern, &search_paths).await?
                }
                #[cfg(not(feature = "ai"))]
                {
                    println!("AI features not enabled. Using regular search.");
                    let engine = SearchEngine::new(config);
                    engine.search(&pattern, &search_paths).await?
                }
            }
            _ => unreachable!(),
        };
        
        // Display summary
        println!("\nSearch Summary:");
        println!("==================");
        println!("Files with matches: {}", results.len());
        println!("Total matches: {}", results.iter().map(|r| r.matches.len()).sum::<usize>());
        
        if !results.is_empty() {
            println!("\nPress Enter to see detailed results...");
            std::io::stdin().read_line(&mut String::new())?;
        }
        
        Ok(results)
    }
    
    /// Perform AI-assisted search with pattern suggestion and analysis
    #[cfg(feature = "ai")]
    async fn ai_assisted_search(
        &self,
        query: &str,
        paths: &[PathBuf],
    ) -> Result<Vec<SearchResult>> {
        use crate::ai::AiService;
        
        if let Some(ai_config) = &self.config.ai {
            let ai_service = AiService::new(ai_config.clone())?;
            
            println!("Asking AI for search suggestions...");
            
            // Get AI-suggested search patterns
            let patterns = ai_service.suggest_patterns(query, "").await?;
            
            println!("AI suggested patterns:");
            for (i, suggestion) in patterns.iter().enumerate() {
                println!("  {}. {} - {}", i + 1, suggestion.pattern, suggestion.description);
            }
            
            // Search with all patterns and combine results
            let mut all_results = Vec::new();
            
            for suggestion in patterns {
                println!("\nSearching with pattern: {}", suggestion.pattern);
                let results = self.search(&suggestion.pattern, paths).await?;
                all_results.extend(results);
            }
            
            // Deduplicate and rank results
            self.deduplicate_and_rank_results(&mut all_results);
            
            Ok(all_results)
        } else {
            anyhow::bail!("AI configuration not available");
        }
    }
    
    /// Add AI insights to search results
    #[cfg(feature = "ai")]
    async fn add_ai_insights(&self, results: &mut [SearchResult]) -> Result<()> {
        use crate::ai::AiService;
        
        if let Some(ai_config) = &self.config.ai {
            let ai_service = AiService::new(ai_config.clone())?;
            
            info!("Generating AI insights for {} files", results.len());
            
            // Process results in parallel but limit concurrency
            let semaphore = Arc::new(tokio::sync::Semaphore::new(5)); // Max 5 concurrent AI requests
            let mut handles = Vec::new();
            
            for result in results.iter_mut() {
                if !result.matches.is_empty() {
                    let permit = semaphore.clone().acquire_owned().await?;
                    let ai_service = ai_service.clone();
                    let result_clone = SearchResult {
                        path: result.path.clone(),
                        matches: result.matches.clone(),
                        metadata: result.metadata.clone(),
                        #[cfg(feature = "ai")]
                        ai_insights: result.ai_insights.clone(),
                    };
                    
                    let handle = tokio::spawn(async move {
                        let _permit = permit; // Keep permit alive
                        ai_service.generate_insights(&result_clone).await
                    });
                    
                    handles.push(handle);
                } else {
                    handles.push(tokio::spawn(async { 
                        Ok(crate::search::AiInsights {
                            summary: "No matches found".to_string(),
                            explanation: None,
                            suggestions: vec![],
                            related_locations: vec![],
                        })
                    }));
                }
            }
            
            // Collect AI insights
            for (result, handle) in results.iter_mut().zip(handles) {
                match handle.await? {
                    Ok(insights) => {
                        result.ai_insights = Some(insights);
                    }
                    Err(e) => {
                        warn!("Failed to generate AI insights for {}: {}", result.path.display(), e);
                    }
                }
            }
        }
        
        Ok(())
    }
    
    /// Check if AI insights should be added
    #[cfg(feature = "ai")]
    fn should_add_ai_insights(&self) -> bool {
        self.config.ai
            .as_ref()
            .map(|ai| ai.enable_insights)
            .unwrap_or(false)
    }
    
    /// Sort search results by relevance
    fn sort_results(&self, results: &mut [SearchResult]) {
        results.sort_by(|a, b| {
            // Sort by average match score first
            let avg_score_a = a.matches.iter().map(|m| m.score).sum::<f64>() / a.matches.len() as f64;
            let avg_score_b = b.matches.iter().map(|m| m.score).sum::<f64>() / b.matches.len() as f64;
            
            avg_score_b.partial_cmp(&avg_score_a)
                .unwrap_or(std::cmp::Ordering::Equal)
                // Then by number of matches
                .then_with(|| b.matches.len().cmp(&a.matches.len()))
                // Finally by path
                .then_with(|| a.path.cmp(&b.path))
        });
        
        // Sort matches within each file
        for result in results {
            result.matches.sort_by(|a, b| {
                b.score.partial_cmp(&a.score)
                    .unwrap_or(std::cmp::Ordering::Equal)
                    .then_with(|| a.line_number.cmp(&b.line_number))
            });
        }
    }
    
    /// Deduplicate and rank search results
    fn deduplicate_and_rank_results(&self, results: &mut Vec<SearchResult>) {
        use std::collections::HashMap;
        
        // Group by file path
        let mut grouped: HashMap<PathBuf, SearchResult> = HashMap::new();
        
        for result in results.drain(..) {
            let path = result.path.clone();
            match grouped.entry(path) {
                std::collections::hash_map::Entry::Occupied(mut entry) => {
                    // Merge matches and deduplicate
                    entry.get_mut().matches.extend(result.matches);
                    entry.get_mut().matches.sort_by_key(|m| (m.line_number, m.column_range.start));
                    entry.get_mut().matches.dedup_by(|a, b| {
                        a.line_number == b.line_number && 
                        a.column_range == b.column_range
                    });
                }
                std::collections::hash_map::Entry::Vacant(entry) => {
                    entry.insert(result);
                }
            }
        }
        
        // Collect and sort results
        *results = grouped.into_values().collect();
        self.sort_results(results);
    }
    
    /// Generate comprehensive search statistics
    pub fn generate_stats(&self, results: &[SearchResult], duration: std::time::Duration) -> SearchStats {
        let total_matches = results.iter().map(|r| r.matches.len()).sum();
        let files_searched = results.iter().map(|_r| 1).sum(); // Approximation
        let files_matched = results.len();
        
        // Calculate language statistics
        let mut language_stats = std::collections::HashMap::new();
        for result in results {
            if let Some(lang) = &result.metadata.language {
                *language_stats.entry(lang.clone()).or_insert(0) += 1;
            }
        }
        
        // Calculate performance metrics
        let total_bytes: u64 = results.iter().map(|r| r.metadata.size).sum();
        let _bytes_per_second = if duration.as_secs_f64() > 0.0 {
            total_bytes as f64 / duration.as_secs_f64()
        } else {
            0.0
        };
        
        SearchStats {
            files_searched,
            files_matched,
            total_matches,
            files_skipped: 0, // Would need to track this
            duration,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;
    use tokio::fs;
    
    #[tokio::test]
    async fn test_search_engine_creation() {
        let config = Config::default();
        let engine = SearchEngine::new(config);
        assert!(engine.config.search.threads > 0);
    }
    
    #[tokio::test]
    async fn test_basic_search() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.rs");
        fs::write(&test_file, "fn main() {\n    println!(\"Hello, world!\");\n}").await.unwrap();
        
        let config = Config::default();
        let engine = SearchEngine::new(config);
        
        let results = engine.search("main", &[temp_dir.path().to_path_buf()]).await.unwrap();
        
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].matches.len(), 1);
        assert_eq!(results[0].matches[0].line_number, 1);
    }
    
    #[tokio::test]
    async fn test_empty_pattern() {
        let config = Config::default();
        let engine = SearchEngine::new(config);
        
        let result = engine.search("", &[PathBuf::from(".")]).await;
        assert!(result.is_err());
    }
    
    #[tokio::test]
    async fn test_nonexistent_path() {
        let config = Config::default();
        let engine = SearchEngine::new(config);
        
        let results = engine.search("test", &[PathBuf::from("/nonexistent/path")]).await.unwrap();
        assert!(results.is_empty());
    }
    
    #[test]
    fn test_sort_results() {
        let config = Config::default();
        let engine = SearchEngine::new(config);
        
        let mut results = vec![
            SearchResult {
                path: PathBuf::from("file1.rs"),
                matches: vec![
                    crate::search::Match {
                        line_number: 1,
                        column_range: 0..4,
                        line_text: "test".to_string(),
                        before_context: vec![],
                        after_context: vec![],
                        ast_context: None,
                        score: 0.5,
                    }
                ],
                metadata: crate::search::FileMetadata {
                    size: 100,
                    language: Some("rust".to_string()),
                    encoding: "UTF-8".to_string(),
                    modified: std::time::SystemTime::now(),
                },
                #[cfg(feature = "ai")]
                ai_insights: None,
            },
            SearchResult {
                path: PathBuf::from("file2.rs"),
                matches: vec![
                    crate::search::Match {
                        line_number: 1,
                        column_range: 0..4,
                        line_text: "test".to_string(),
                        before_context: vec![],
                        after_context: vec![],
                        ast_context: None,
                        score: 0.8,
                    }
                ],
                metadata: crate::search::FileMetadata {
                    size: 200,
                    language: Some("rust".to_string()),
                    encoding: "UTF-8".to_string(),
                    modified: std::time::SystemTime::now(),
                },
                #[cfg(feature = "ai")]
                ai_insights: None,
            },
        ];
        
        engine.sort_results(&mut results);
        
        // Should be sorted by score (0.8 > 0.5)
        assert_eq!(results[0].path, PathBuf::from("file2.rs"));
        assert_eq!(results[1].path, PathBuf::from("file1.rs"));
    }
}