Skip to main content
Every code element in Codegen is an Editable - meaning it can be manipulated while maintaining correctness. All higher-level code manipulation APIs are built on top of the atomic Editable API.

Core Concepts

Every Editable provides:

Basic Editing

There are several fundamental ways to modify code with Editables:

Finding and Searching

Editables provide powerful search capabilities:

Smart Formatting

Codegen handles formatting details automatically:

Safe Removals

Removing code elements is safe and clean:

Working with References

Editables track their relationships to other code elements:

Understanding Context

Editables provide rich information about their location and context in the code:

Parent Relationships

Statement Containment

The is_wrapped_in method lets you check if an Editable is contained within specific types of statements:

Common Use Cases

Best Practices

  1. Use High-Level Operations: Prefer operations like rename() over direct source editing when possible.
  2. Leverage Search: Use the built-in search capabilities rather than parsing text manually.
  3. Work with Extended Sources: Use extended_source when you need to modify decorators or comments together with the code.
  4. Mind the Context: Remember that each Editable knows its location and relationships in the code.
  5. Trust the Formatting: Let Codegen handle whitespace and syntax details automatically.