Building with Codegen
Local Variables
This document explains how to work with local variables in Codegen.
Overview
Through the CodeBlock class, Graph-sitter exposes APIs for analyzing and manipulating local variables within code blocks.
- local_var_assignments: find all Assignments in this scope
- get_local_var_assignment(…): get specific Assignments by name
- rename_local_variable(…): rename variables safely across the current scope
Basic Usage
Every code block (function body, loop body, etc.) provides access to its local variables:
Fuzzy Matching
Graph-sitter supports fuzzy matching when searching for local variables. This allows you to find variables whose names contain a substring, rather than requiring exact matches:
Be careful with fuzzy matching when renaming variables, as it will replace the
matched substring in all variable names. This might lead to unintended renames
like config_settings
becoming settings_settings
.