Building with Codegen
The Editable API
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:
- Information about the source code:
- source - the text content of the Editable
- extended_source - includes relevant content like decorators, comments, etc.
- Information about the file that contains the Editable:
- file - the SourceFile that contains this Editable
- Relationship tracking
- parent_class - the Class that contains this Editable
- parent_function - the Function that contains this Editable
- parent_statement - the Statement that contains this Editable
- Safe modification operations
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
Was this page helpful?