Morph LogoMorph
Back to the main blog

How Morph Determines Files to Edit

Understanding how Morph intelligently applies changes to the right files

Posted by Tejas Bhakta

3 minute read


How Morph Determines Files to Edit

One of the most powerful features of Morph is its ability to intelligently determine which files need to be updated. Let's dive into how this works.

The Challenge

When an AI assistant suggests code changes, it often:

  1. Doesn't specify the full file path
  2. Only shows parts of the code that need to change
  3. Might affect multiple files
  4. Could impact related files (like tests or types)

How Morph Handles It

Morph uses a combination of techniques to identify the correct files:

1. Semantic Understanding

Unlike traditional tools that rely on exact text matches, Morph understands code semantically:

# Original suggestion from AI
def calculate_total(items):
    return sum(item.price for item in items)

# Morph can match this even if your actual code is:
def calculate_total(item_list):
    total = 0
    for current_item in item_list:
        total += current_item.price
    return total

2. Context Analysis

Morph analyzes:

  • Function and class names
  • Import statements
  • File structure
  • Code dependencies

This helps it identify not just the primary file, but also related files that might need updates.

3. Project Structure

Morph understands common project structures:

  • Source code directories (src/, lib/)
  • Test files (test_, *_test, __tests__)
  • Type definition files (.d.ts, .pyi)
  • Configuration files

Examples

Single File Update

# AI suggests:
def add_tax(price):
    return price * 1.1

# Morph will find:
src/utils/pricing.py  # Based on context and function name

Multiple File Updates

# AI suggests:
class User:
    def get_total_spent(self):
        return sum(order.total for order in self.orders)

# Morph will update:
src/models/user.py     # Main class definition
tests/test_user.py     # Related test file
types/user.d.ts        # Type definitions

Best Practices

  1. Be Specific: If you know the file path, include it in your request
  2. Provide Context: Include relevant imports and class names
  3. Use Standard Patterns: Follow common naming conventions
  4. Check Related Files: Morph will suggest updates to related files

Common Questions

Q: What if Morph selects the wrong file?

A: Morph will show you which files it plans to update before making changes. You can always specify the exact file if needed.

Q: Can Morph create new files?

A: Yes! Morph can create new files when needed, following your project's structure and conventions.

Q: How does Morph handle multiple similar matches?

A: Morph uses context and project structure to rank matches. It will ask for clarification if there's ambiguity.

Next Steps