Codegen’s GraphSitter library provides powerful APIs for performing complex renaming operations throughout your codebase. This guide will walk you through the process of renaming various code elements and updating their references.

Common use cases include:

  • Renaming functions, classes, or variables across multiple files
  • Updating import statements after renaming modules
  • Refactoring method names in a class hierarchy

Overview

To perform complex renaming, you’ll typically need to rename the symbol and then update all its references. Here’s a basic example:

This operation will rename the function and update all call sites and imports referencing this function.

Renaming Functions and Updating Call Sites

When renaming a function, GraphSitter automatically updates all call sites:

Renaming Classes and Updating References

Renaming a class will update all references, including inheritance and instantiations:

Renaming Across Multiple Files

GraphSitter handles renaming across multiple files seamlessly:

This will rename all functions starting with “deprecated_” across all files in the codebase.

Renaming Methods in a Class Hierarchy

When renaming methods in a class hierarchy, you need to update the method in the base class and all overrides:

Best Practices

  1. Commit After Major Changes: If you’re making multiple significant changes that may have overlapping edits, use codebase.commit() between them to ensure the codebase graph is up-to-date.

  2. Re-fetch References: After a commit, re-fetch any file or symbol references you’re working with, as they may have become stale.

  3. Handle Errors: Be prepared to handle cases where symbols might not exist, or where renaming might fail due to naming conflicts.

By following these guidelines, you can effectively perform complex renaming operations throughout your codebase while maintaining its integrity and functionality.