pub struct CodeGraph {
pub graph: DiGraph<GraphNode, GraphEdgeKind>,
pub file_nodes: HashMap<FileId, NodeIndex>,
pub path_to_file: HashMap<String, NodeIndex>,
pub suffix_to_file: HashMap<String, NodeIndex>,
pub module_to_file: HashMap<String, NodeIndex>,
pub external_modules: HashMap<String, NodeIndex>,
pub function_nodes: HashMap<(FileId, String), NodeIndex>,
pub class_nodes: HashMap<(FileId, String), NodeIndex>,
}Expand description
The main code graph structure.
Fields§
§graph: DiGraph<GraphNode, GraphEdgeKind>§file_nodes: HashMap<FileId, NodeIndex>Quick lookup: file_id -> node index for the file node.
path_to_file: HashMap<String, NodeIndex>Quick lookup: file path -> node index for the file node.
suffix_to_file: HashMap<String, NodeIndex>Quick lookup: path suffix -> node index (for fast import resolution) Maps “module.py”, “pkg/module.py”, etc. to their file nodes
module_to_file: HashMap<String, NodeIndex>Quick lookup: module path (dot-separated) -> node index Maps “pkg.module” to its file node
external_modules: HashMap<String, NodeIndex>Quick lookup: external module name -> node index
function_nodes: HashMap<(FileId, String), NodeIndex>Quick lookup: (file_id, function_name) -> node index
class_nodes: HashMap<(FileId, String), NodeIndex>Quick lookup: (file_id, class_name) -> node index
Implementations§
Source§impl CodeGraph
impl CodeGraph
pub fn new() -> Self
Sourcepub fn get_or_create_external_module(
&mut self,
name: &str,
category: ModuleCategory,
) -> NodeIndex
pub fn get_or_create_external_module( &mut self, name: &str, category: ModuleCategory, ) -> NodeIndex
Get or create an external module node
Sourcepub fn find_file_by_path(&self, path: &str) -> Option<NodeIndex>
pub fn find_file_by_path(&self, path: &str) -> Option<NodeIndex>
Find a file node by path (supports partial matching for relative imports)
This uses pre-built indexes for O(1) lookups instead of O(n) iteration:
- Exact path match via path_to_file
- Module path (dot-separated) via module_to_file
- Path suffix match via suffix_to_file
Sourcepub fn get_importers(&self, file_id: FileId) -> Vec<FileId>
pub fn get_importers(&self, file_id: FileId) -> Vec<FileId>
Get all files that directly import a given file
Sourcepub fn get_imports(&self, file_id: FileId) -> Vec<FileId>
pub fn get_imports(&self, file_id: FileId) -> Vec<FileId>
Get all files that a given file directly imports
Sourcepub fn get_transitive_importers(
&self,
file_id: FileId,
max_depth: usize,
) -> Vec<(FileId, usize)>
pub fn get_transitive_importers( &self, file_id: FileId, max_depth: usize, ) -> Vec<(FileId, usize)>
Get all files that transitively import a given file (up to max_depth hops)
Sourcepub fn get_external_dependencies(&self, file_id: FileId) -> Vec<String>
pub fn get_external_dependencies(&self, file_id: FileId) -> Vec<String>
Get external libraries used by a file
Sourcepub fn get_files_using_library(&self, library_name: &str) -> Vec<FileId>
pub fn get_files_using_library(&self, library_name: &str) -> Vec<FileId>
Get all files that use a specific external library
Sourcepub fn stats(&self) -> GraphStats
pub fn stats(&self) -> GraphStats
Get statistics about the graph
Sourcepub fn rebuild_indexes(&mut self)
pub fn rebuild_indexes(&mut self)
Rebuild all lookup indexes from the graph.
This must be called after deserializing a CodeGraph to restore the quick-lookup HashMaps that are skipped during serialization.