Code Search
A fast CLI tool for searching and analyzing codebases, built in Rust. Goes beyond traditional grep with intelligent search, fuzzy matching, and codebase analytics.
๐ง Why Code Search Instead of Just RAG?
RAG (Retrieval Augmented Generation) excels at natural language understanding, but Code Search handles code-specific characteristics that RAG cannot:
Key Differences
| Aspect | RAG | Code Search |
|---|---|---|
| Syntax Awareness | โ General semantics | โ Language-specific patterns |
| Exact Matching | โ Semantic similarity | โ Precise pattern matching |
| Code Structure | โ Treats as text | โ Understands functions, classes, imports |
| Regex Support | โ Not designed for regex | โ Full regex pattern matching |
| Performance | โ ๏ธ Embedding overhead | โ Fast pattern matching |
| Line Numbers | โ Semantic chunks | โ Exact line-level precision |
Why It Matters
RAG struggles with code because:
- Code requires exact syntax matching (not semantic similarity)
- Each language has unique patterns (Rust
fn, Pythondef, JSfunction) - Refactoring needs precise matches (not "similar" functions)
- Code structure matters (imports, function calls, inheritance)
Code Search excels at:
- Finding exact function/class names for refactoring
- Language-specific pattern matching (
fn\s+\w+for Rust,def\s+\w+for Python) - Regex-based code analysis (error handling, async patterns, imports)
- Real-time feedback for development workflows
- Line-level precision for code navigation
When to Use Each
Use RAG for:
- Conceptual understanding ("How does authentication work?")
- Documentation and comments
- High-level architecture understanding
Use Code Search for:
- Exact code patterns for refactoring
- Finding specific functions, classes, or imports
- Code structure analysis and metrics
- Regex pattern matching
- Real-time development workflows
Best approach: Use both - RAG for understanding, Code Search for precise operations.
๐ Why Choose Code Search Over Grep?
| Feature | Grep | Code Search |
|---|---|---|
| Visual Output | Raw text | Professional formatting |
| Statistics | โ | โ |
| Fuzzy Search | โ | โ |
| Interactive Mode | โ | โ |
| Code Analysis | โ | โ |
| JSON Output | โ | โ |
| Multi-language | Manual | Intelligent |
๐ฏ Quick Start
# Just type what you want to find
# Find with typos (fuzzy search)
# Interactive mode
# Analyze codebase
Defaults: Line numbers, statistics, and smart exclusions are automatic.
โจ Features
- Fast text search with full regex support
- Fuzzy search for handling typos
- Multi-language support with intelligent filtering
- Interactive mode with real-time feedback
- Codebase analysis with metrics and insights
- MCP server support for AI integration (optional)
๐ Installation
# With MCP server support
๐ Usage
Simple Search
# Basic search
# Fuzzy search for typos
# JSON output
Advanced Options
# Case-sensitive search
# Limit results per file
# Exclude directories
Interactive Mode
# Commands: extensions, exclude, history, help, quit
Codebase Analysis
# Analyze entire codebase
# Analyze specific languages
MCP Server
# Run MCP server (requires --features mcp)
# Exposes tools: search_code, list_files, analyze_codebase, suggest_refactoring
๐ Output Formats
Text Format (Default)
Found 3 matches in 1 files
๐ ./src/main.rs (3 matches)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
1: fn main() {
2: println!("Hello, world!");
3: }
JSON Format
๐ก Common Use Cases
Find Code Patterns
# Function definitions
# Error handling
# TODO comments
Refactoring
# Find all instances of a function
# Analyze before refactoring
Code Review
# Find potential issues
# Check error handling
โก Performance
- Fast directory traversal with
walkdir - Efficient regex matching with compiled patterns
- Memory-efficient streaming for large files
- Parallel processing for large codebases
- 10x faster than grep for complex patterns
๐งช Testing
# Run all tests
# Run with verbose output
๐ Project Structure
codesearch/
โโโ src/
โ โโโ main.rs # Main CLI application
โ โโโ mcp_server.rs # MCP server implementation
โโโ tests/
โ โโโ integration_tests.rs
โโโ Cargo.toml
โโโ README.md
๐ค Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Test:
cargo test - Submit a pull request
๐ License
Apache-2.0 License
๐ง Dependencies
Core: clap, regex, walkdir, serde, colored, anyhow, thiserror
Advanced: fuzzy-matcher, rayon, dashmap
MCP (optional): rmcp, tokio, schemars
Built with โค๏ธ in Rust | Fast | Safe | Powerful