File
Represents a generic file. Could represent a source file or a non-code file such as a markdown file or image file.
Inherits from
Properties
content
Returns the content of the file as a UTF-8 encoded string.
Gets the content of the file, either from pending changes or by reading from disk. Binary files cannot be read as strings.
Args: None
Returns: str: The content of the file as a UTF-8 encoded string.
Raises: ValueError: If the file is binary. Use content_bytes instead for binary files.
directory
Returns the directory that contains this file.
The file can be housed within a directory in the codebase, and this property will return that directory instance.
Returns: Directory | None: The directory containing this file, or None if the file is not in any directory.
extended
Returns a SymbolGroup of all extended nodes associated with this element.
Creates a SymbolGroup that provides a common interface for editing all extended nodes, such as decorators, modifiers, and comments associated with the element.
Args: None
Returns: SymbolGroup: A group containing this node and its extended nodes that allows batch modification through a common interface.
extended_source
Returns the source text representation of all extended nodes.
Gets the source text of all extended nodes combined. This property allows reading the source text of all extended nodes (e.g. decorators, export statements) associated with this node.
Returns: str: The combined source text of all extended nodes.
extension
Returns the file extension.
Returns: str: The file extension including the dot (e.g., ‘.py’, ‘.ts’, ‘.js’).
file
A property that returns the file object for non-source files.
This is used by Editable.file to work with non-source files, allowing consistent interface usage across both source and non-source files.
Returns: Self: The current file object.
filepath
Retrieves the file path of the file that this Editable instance belongs to.
Returns: str: The file path of the file.
function_calls
Returns a list of all function calls contained within this expression.
Traverses the extended nodes of this expression to find all function calls within it. This is useful for tasks like analyzing call patterns or renaming function invocations.
Returns: list[FunctionCall]: A list of FunctionCall objects representing all function calls contained within this expression.
is_binary
Indicates whether the file contains binary data.
A property that returns True if the file contains binary data, False if it contains text data.
Returns: bool: True if the file contains binary data, False if it contains text data.
owners
Returns the CODEOWNERS of the file.
Returns all Github CODEOWNERS associated with this file. If there is no CODEOWNERS file in the codebase, returns an empty set.
Returns: set[str]: A set of Github usernames or team names that own this file. Empty if no CODEOWNERS file exists.
source
Text representation of the Editable instance.
Returns the source text of the Editable instance. This is the main property used to access the text content of any code element in GraphSitter.
Returns: str: The text content of this Editable instance.
start_byte
Returns the starting byte position of a file in its content.
The start byte is always 0 for a file as it represents the beginning of the file’s content.
Returns: int: Always returns 0.
variable_usages
Returns Editables for all TreeSitter node instances of variable usages within this node’s scope.
This method finds all variable identifier nodes in the TreeSitter AST, excluding:
- Function names in function calls
- Import names in import statements
- Property access identifiers (except the base object)
- Keyword argument names (in Python and TypeScript)
This is useful for variable renaming and usage analysis within a scope.
Returns: list[Editable]: A list of Editable nodes representing variable usages. Each Editable corresponds to a TreeSitter node instance where the variable is referenced.
Methods
commit
Commits any pending transactions for the current node to the codebase.
Commits only the transactions that affect the file this node belongs to. This is useful when you want to commit changes made to a specific node without committing all pending transactions in the codebase.
Args: None
Returns: None
edit
Replace the source of this Editable
with new_src
.
Replaces the text representation of this Editable instance with new text content. The method handles indentation adjustments and transaction management.
Args:
new_src (str): The new source text to replace the current text with.
fix_indentation (bool): If True, adjusts the indentation of new_src
to match the current text’s indentation level. Defaults to False.
priority (int): The priority of the edit transaction. Higher priority edits are applied first. Defaults to 0.
dedupe (bool): If True, deduplicates identical transactions. Defaults to True.
Returns: None
find
Find and return matching nodes or substrings within an Editable instance.
This method searches through the extended_nodes of the Editable instance and returns all nodes or substrings that match the given search criteria.
Args: strings_to_match (Union[list[str], str]): One or more strings to search for. exact (bool): If True, only return nodes whose source exactly matches one of the strings_to_match. If False, return nodes that contain any of the strings_to_match as substrings. Defaults to False.
Returns: list[Editable]: A list of Editable instances that match the search criteria.
find_string_literals
Returns a list of string literals within this node’s source that match any of the given strings.
Args: strings_to_match (list[str]): A list of strings to search for in string literals. fuzzy_match (bool): If True, matches substrings within string literals. If False, only matches exact strings. Defaults to False.
Returns: list[Editable]: A list of Editable objects representing the matching string literals.
flag
Adds a visual flag comment to the end of this Editable’s source text.
Flags this Editable by appending a comment with emoji flags at the end of its source text. This is useful for visually highlighting specific nodes in the source code during development and debugging.
Returns: None
get_variable_usages
Returns Editables for all TreeSitter nodes corresponding to instances of variable usage that matches the given variable name.
Retrieves a list of variable usages that match a specified name, with an option for fuzzy matching. By default, excludes property identifiers and argument keywords.
Args: var_name (str): The variable name to search for. fuzzy_match (bool): If True, matches variables where var_name is a substring. If False, requires exact match. Defaults to False.
Returns: list[Editable]: List of Editable objects representing variable usage nodes matching the given name.
insert_after
Inserts code after this node.
Args: new_src (str): The source code to insert after this node. fix_indentation (bool, optional): Whether to adjust the indentation of new_src to match the current node. Defaults to False. newline (bool, optional): Whether to add a newline before the new_src. Defaults to True. priority (int, optional): Priority of the insertion transaction. Defaults to 0. dedupe (bool, optional): Whether to deduplicate identical transactions. Defaults to True.
Returns: None
insert_before
Inserts text before this node’s source with optional indentation and newline handling.
This method inserts the provided text before the current node’s source code. It can automatically handle indentation and newline placement.
Args: new_src (str): The text to insert before this node. fix_indentation (bool): Whether to fix the indentation of new_src to match the current node. Defaults to False. newline (bool): Whether to add a newline after new_src. Defaults to True. priority (int): Transaction priority for managing multiple edits. Defaults to 0. dedupe (bool): Whether to deduplicate identical transactions. Defaults to True.
Returns: None
parse
Parses the file representation into the graph.
This method is called during file initialization to parse the file and build its graph representation within the codebase graph.
Args: G (CodebaseGraph): The codebase graph that the file belongs to.
Returns: None
reduce_condition
Reduces an editable to the following condition
remove
Removes the file from the file system and graph.
Queues the file to be removed during the next commit operation. The file will be removed from the filesystem and its node will be removed from the graph.
Args: None
Returns: None
rename
Renames the file to the specified name, preserving the file extension.
Args: new_name (str): The new name for the file. If the new name includes the file extension, it will be used as-is. Otherwise, the original file extension will be preserved.
Returns: None
Note: This method will update all imports that reference this file to use the new filepath. The file will be physically moved on disk and all graph references will be updated.
replace
Search and replace occurrences of text within this node’s source and its extended nodes.
This method performs string replacement similar to Python’s string.replace(), with support for regex patterns. It operates on both the main node and any extended nodes (e.g. decorators, exports).
Args: old (str): The text or pattern to search for. new (str): The text to replace matches with. count (int, optional): Maximum number of replacements to make. Defaults to -1 (replace all). is_regex (bool, optional): Whether to treat ‘old’ as a regex pattern. Defaults to False. priority (int, optional): Priority of the replacement operation. Defaults to 0.
Returns: int: The total number of replacements made.
Raises: ValueError: If there are multiple occurrences of the substring in a node’s source.
search
Returns a list of all regex match of regex_pattern
, similar to python’s re.search().
Searches for matches of a regular expression pattern within the text of this node and its extended nodes.
Args: regex_pattern (str): The regular expression pattern to search for. include_strings (bool): When False, excludes the contents of string literals from the search. Defaults to True. include_comments (bool): When False, excludes the contents of comments from the search. Defaults to True.
Returns: list[Editable]: A list of Editable objects corresponding to the matches found.
update_filepath
Updates the file path and inbound imports of a file.
Updates the file path of the file on disk and in the codebase graph. Additionally updates all inbound imports to reference the new file path.
Args: new_filepath (str): The new file path to rename the file to.
Raises: BadWriteError: If there are pending file writes that haven’t been committed. ValueError: If the new file path already exists in the codebase graph.
Was this page helpful?