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

Best Practices for Integration

For optimal results with Morph, we recommend implementing a tool-based approach similar to how Cursor handles file edits:

Using Tool Calls with Explicit File Paths

The most effective way to use Morph is through tool calls where the model explicitly specifies the target filename:

// Example tool call integration
function editFile(filename, codeEdit) {
  const originalCode = readFile(filename);
  const updatedCode = morph.apply(originalCode, codeEdit);
  writeFile(filename, updatedCode);
}

// Usage by AI assistant
editFile("src/utils/pricing.py", 
  `def add_tax(price):
      return price * 1.1`);

This approach ensures Morph receives clear instructions about which files to modify, reducing ambiguity and increasing accuracy.

Examples

Single File Update

# AI suggests with filename:
editFile("src/utils/pricing.py",
  `def add_tax(price):
      return price * 1.1`)

# Morph applies the change to the specified file

Multiple File Updates

# AI makes sequential tool calls:
editFile("src/models/user.py", 
  `class User:
      def get_total_spent(self):
          return sum(order.total for order in self.orders)`)

editFile("tests/test_user.py",
  `def test_get_total_spent():
      # Updated test code`)

Common Questions

Q: What if Morph selects the wrong file?

A: When using explicit tool calls with filenames, this problem is eliminated. You can always specify the exact file in your implementation.

Q: Can Morph create new files?

A: Yes! Your tool integration can support file creation by passing a new filename to the tool call.

Q: How does Morph handle multiple similar matches?

A: With explicit filename specification, Morph doesn't need to guess which file to update.

Next Steps