Inherits from

CodeBlock, Expression, Editable

Properties


assignment_statements

Returns list of top level assignment statements in the code block.

def assignment_statements(self) -> list[AssignmentStatement[Parent, Self]]:
    ...

assignments

Returns all assignments in the code block, for all nest levels.

def assignments(self) -> list[Assignment[Parent, Self]]:
    ...

attributes

Returns top level attribute statements in the code block.

def attributes(self) -> list[Attribute[Parent, Self]]:
    ...

comments

Gets list of top level comments in the code block.

def comments(self) -> list[Comment[Parent, Self]]:
    ...

extended

Returns a SymbolGroup of all extended nodes. This allows a common edit interface for all of extended nodes.

def extended(self) -> SymbolGroup:
    ...

extended_source

Text representation of all its extended nodes

def extended_source(self) -> str:
    ...

file

The file object that this Editable instance belongs to

def file(self) -> File:
    ...

filepath

The file path of the file that this Editable instance belongs to

def filepath(self) -> str:
    ...

function_calls

Returns a list of all function calls in the code block.

def function_calls(self) -> list[FunctionCall]:
    ...

if_blocks

Returns list of top level if statements in the code block.

def if_blocks(self) -> list[IfBlockStatement[Parent, Self]]:
    ...

local_var_assignments

Returns all local variable assignment in the code block, for all nest levels.

def local_var_assignments(self) -> list[Assignment[Parent, Self]]:
    ...

resolved_value

If the expression is a resolvable value, returns the last assigned expression value. Else, returns itself.

Example: a = 1 b = a foo(b)

If we call resolve_value on b, it returns 1.

def resolved_value(self) -> Expression | list[Expression]:
    ...

return_statements

Returns list of top level return statements in the code block.

def return_statements(self) -> list[ReturnStatement[Parent, Self]]:
    ...

source

Text representation of the Editable instance

def source(self) -> str:
    ...

statements

Returns a list of top level statements in the code block The statements are ordered by appearance in the code block.

def statements(self) -> MultiLineCollection[Statement, Self]:
    ...

symbol_statements

Returns list of top level symbol statements in the code block.

def symbol_statements(self) -> list[SymbolStatement]:
    ...

variable_usages

Returns Editables for all TreeSitter node instances of variable usages. (Excludes: property identifiers and argument keywords) This is useful for renaming variables locally and analyzing variable usages.

def variable_usages(self) -> list[Editable]:
    ...

Methods


commit

Commit just this node

def commit(self) -> None:
    ...

edit

Replace the source of this Editable with new_src. When fix_indentation is True, the indentation of new_src will be adjusted to match the current text’s indentation.

def edit(self, new_src: str, fix_indentation: bool = False, priority: int = 0, dedupe: bool = True) -> None:
    ...

find

Returns a list of all substring match in strings_to_match, similar to python’s str.find(..)

Args: exact: Only match individual nodes which exactly match the query

def find(self, strings_to_match: list[str] | str, *, exact: bool = False) -> list[Editable]:
    ...

find_string_literals

Returns a list of all editable substrings within string literals that match strings_to_match

def find_string_literals(self, strings_to_match: list[str], fuzzy_match: bool = False) -> list[Editable]:
    ...

flag

Adds a comment so the developer can see this Editable

def flag(self) -> None:
    ...

get_assignments

Returns the first code statement that assigns a local variable with specified name var_name.

def get_assignments(self, var_name: str) -> list[Assignment[Parent, Self]]:
    ...

get_attributes

Returns top level attribute statements in the code block that are private.

def get_attributes(self, private: bool) -> list[Attribute[Parent, Self]]:
    ...

get_comment

Returns the first comment statement that matches the specified comment. Searches all nested statements in the code block.

def get_comment(self, comment_src: str) -> Comment[Parent, Self] | None:
    ...

get_local_var_assignment

Returns the first code statement that assigns a local variable with specified name var_name.

def get_local_var_assignment(self, var_name: str) -> Assignment[Parent, Self] | None:
    ...

get_local_var_assignments

Returns all instances of code statements that assigns to the local variable with specified name var_name. When fuzzy_match is True, it matches on local variables with names that contain var_name, otherwise defaults on exact match of local var name.

def get_local_var_assignments(self, var_name: str, fuzzy_match: bool = False) -> list[Assignment[Parent, Self]]:
    ...

get_statements

Returns all statements up to the specified block level.

Arguments: max_level (int): specifies the maximum block level of statements to return. Default: None (returns all)

def get_statements(self, statement_type: StatementType | None = None, max_level: int | None = None) -> list[Statement[Parent, Self]]:
    ...

get_variable_usages

Returns all instances of variable usages in the code block as a list of Editable. When fuzzy_match is True, it matches on usages that contains var_name, otherwise defaults on exact match.

def get_variable_usages(self, var_name: str, fuzzy_match: bool = False) -> list[Editable[Self]]:
    ...

indent

Adjusts the indentation level on the whole code block by the specified level. If the level is < 0, the block is unindented to the left. If the level is > 0, the block is indented to the right.

def indent(self, level: int) -> None:
    ...

insert_after

Inserts new_src at the bottom of the code block

def insert_after(self, new_src: str, fix_indentation=True, newline=True) -> None:
    ...

insert_before

Inserts new_src at the top of the code block

def insert_before(self, new_src: str) -> None:
    ...

remove

Deletes this Node, and optionally its related extended nodes (e.g. decorators, comments, etc.)

def remove(self, delete_formatting: bool = True, priority: int = 0, dedupe: bool = True) -> None:
    ...

rename_variable_usages

Renames all instances of variable usages in the code block. When fuzzy_match is True, it matches on usages that contains old_var_name, otherwise defaults on exact match

def rename_variable_usages(self, old_var_name: str, new_var_name: str, fuzzy_match: bool = False) -> None:
    ...

replace

Search and replace an instance of substring within this node’s source. Similar to python’s string.replace(..) Throws ValueError if there are more than one occurrence of substring in this node’s source.

Arguments: old: the occurrences of this string to replace new: the string to replace with count: the max number of occurrences to replace. Default value: -1 (replace all) is_regex: whether old is a regex pattern. Default value: False

Returns: The total count of old occurrences replaced.

def replace(self, old: str, new: str, count: int = -1, is_regex: bool = False, priority: int = 0) -> int:
    ...

Returns a list of all regex match of regex_pattern, similar to python’s re.search(…)

When include_strings is False, the search will exclude the contents of string literals from the search. When include_comments is False, the search will exclude the contents of comments from the search.

def search(self, regex_pattern: str, include_strings: bool = True, include_comments: bool = True) -> list[Editable]:
    ...

unwrap

Unwraps the code block by removing the opening and closing braces.

def unwrap(self) -> None:
    ...

wrap

Wraps the block with a statement.

Ex: wrap a block with an if, else, or for loop, etc.

For example: return b =>

if b: return b

def wrap(self, before_src: str, after_src: str = "") -> None:
    ...