Skip to content

Similar Code

geoffrey fernald edited this page Jan 28, 2026 · 1 revision

Similar Code Search

Find code semantically similar to what you're about to write. Returns relevant examples with patterns and conventions to use as templates.

Overview

When creating new code, find existing code that does something similar. This ensures consistency and helps you follow established patterns.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    SIMILAR CODE SEARCH                           β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                  β”‚
β”‚  Query: "user preferences CRUD"                                  β”‚
β”‚  Intent: api_endpoint                                            β”‚
β”‚                                                                  β”‚
β”‚  Results:                                                        β”‚
β”‚                                                                  β”‚
β”‚  1. src/controllers/settings.ts (92% match)                      β”‚
β”‚     └─ User settings CRUD operations                             β”‚
β”‚     └─ Patterns: REST controller, validation, auth               β”‚
β”‚                                                                  β”‚
β”‚  2. src/controllers/profile.ts (87% match)                       β”‚
β”‚     └─ User profile management                                   β”‚
β”‚     └─ Patterns: REST controller, DTO validation                 β”‚
β”‚                                                                  β”‚
β”‚  3. src/controllers/notifications.ts (78% match)                 β”‚
β”‚     └─ Notification preferences                                  β”‚
β”‚     └─ Patterns: REST controller, partial updates                β”‚
β”‚                                                                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

MCP Tool

drift_similar

drift_similar({
  intent: "api_endpoint",              // What kind of code
  description: "user preferences CRUD", // Natural language description
  limit: 3,                            // Max results
  scope: "src/controllers"             // Optional: limit to directory
})

Intent Options:

Intent Description
api_endpoint REST/GraphQL endpoints
service Business logic services
component UI components
hook React/Vue hooks
utility Helper functions
test Test files
middleware Request middleware

Response:

{
  "summary": "Found 3 similar api_endpoint example(s). Top: src/controllers/settings.ts",
  "data": {
    "matches": [
      {
        "file": "src/controllers/settings.ts",
        "function": "getSettings",
        "class": "SettingsController",
        "similarity": 0.92,
        "reason": "Has api_endpoint decorator. Matches 3 api_endpoint patterns",
        "preview": "@Get export async function getSettings(user: AuthUser): Promise<Settings>",
        "patterns": ["REST Controller", "DTO Validation", "Auth Middleware"]
      },
      {
        "file": "src/controllers/profile.ts",
        "function": "updateProfile",
        "similarity": 0.87,
        "reason": "Has api_endpoint decorator. Name matches 2 keywords",
        "preview": "@Put export async function updateProfile(dto: UpdateProfileDto): Promise<Profile>",
        "patterns": ["REST Controller", "DTO Validation"]
      },
      {
        "file": "src/controllers/notifications.ts",
        "function": "getNotificationPreferences",
        "similarity": 0.78,
        "reason": "Matches 2 api_endpoint patterns",
        "preview": "@Get export async function getNotificationPreferences(): Promise<NotificationSettings>",
        "patterns": ["REST Controller"]
      }
    ],
    "conventions": {
      "naming": "camelCase",
      "errorHandling": "Async with try/catch",
      "imports": "Path alias (@/)"
    }
  },
  "hints": {
    "nextActions": [
      "Use drift_signature to get full signature for \"getSettings\"",
      "Use drift_imports to get correct imports for your new file"
    ],
    "relatedTools": ["drift_signature", "drift_imports", "drift_code_examples"]
  }
}

Use Cases

1. Creating a New API Endpoint

// Find similar endpoints
const similar = await drift_similar({
  intent: "api_endpoint",
  description: "order history with pagination"
});

// Use the most similar as template
// similar.data.results[0].snippet

2. Creating a New Component

drift_similar({
  intent: "component",
  description: "modal dialog with form validation"
})

3. Creating a New Service

drift_similar({
  intent: "service",
  description: "email notification service"
})

4. Creating a New Hook

drift_similar({
  intent: "hook",
  description: "data fetching with caching"
})

How It Works

1. Semantic Analysis

Drift analyzes your description using:

  • Keyword extraction
  • Intent classification
  • Domain matching

2. Code Indexing

All code is indexed by:

  • Function/class names
  • Comments and documentation
  • Pattern associations
  • File structure

3. Similarity Scoring

Results are scored by:

  • Semantic similarity to description
  • Pattern overlap
  • Structural similarity
  • Recency (newer code preferred)

Integration with Workflow

Recommended Flow

// 1. Get context for your task
const context = await drift_context({
  intent: "add_feature",
  focus: "user preferences"
});

// 2. Find similar code
const similar = await drift_similar({
  intent: "api_endpoint",
  description: "user preferences CRUD"
});

// 3. Get detailed examples if needed
const examples = await drift_code_examples({
  pattern: similar.data.commonPatterns[0].id
});

// 4. Generate code using templates

// 5. Validate generated code
await drift_validate_change({
  file: "src/controllers/preferences.ts",
  content: generatedCode
});

Scoping Results

By Directory

drift_similar({
  intent: "service",
  description: "payment processing",
  scope: "src/services"  // Only search in services
})

By Pattern Category

// First find patterns
const patterns = await drift_patterns_list({
  categories: ["api"]
});

// Then find similar code with those patterns
drift_similar({
  intent: "api_endpoint",
  description: "user management"
})

Best Practices

1. Be Specific in Descriptions

// βœ… Good - specific
drift_similar({
  description: "paginated list endpoint with filtering and sorting"
})

// ❌ Bad - too vague
drift_similar({
  description: "list endpoint"
})

2. Use Appropriate Intent

// βœ… Match intent to what you're creating
drift_similar({
  intent: "hook",  // Creating a hook
  description: "form validation with error handling"
})

// ❌ Wrong intent
drift_similar({
  intent: "api_endpoint",  // But creating a hook
  description: "form validation"
})

3. Scope When Possible

// βœ… Scoped search is faster and more relevant
drift_similar({
  intent: "component",
  description: "data table",
  scope: "src/components"
})

4. Review Conventions

The response includes conventions β€” these show the coding style used in similar code:

{
  "conventions": {
    "naming": "camelCase",
    "errorHandling": "Async with try/catch",
    "imports": "Path alias (@/)"
  }
}

Follow these conventions when writing your new code.


---

## Next Steps

- [Code Examples](Code-Examples) β€” Get detailed code snippets
- [Validate Change](Validate-Change) β€” Verify generated code
- [Pattern Categories](Pattern-Categories) β€” All 15 categories

Clone this wiki locally