Skip to main content

CodeGraph

Struct CodeGraph 

Source
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

Source

pub fn new() -> Self

Source

pub fn get_or_create_external_module( &mut self, name: &str, category: ModuleCategory, ) -> NodeIndex

Get or create an external module node

Source

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:

  1. Exact path match via path_to_file
  2. Module path (dot-separated) via module_to_file
  3. Path suffix match via suffix_to_file
Source

pub fn get_importers(&self, file_id: FileId) -> Vec<FileId>

Get all files that directly import a given file

Source

pub fn get_imports(&self, file_id: FileId) -> Vec<FileId>

Get all files that a given file directly imports

Source

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)

Source

pub fn get_external_dependencies(&self, file_id: FileId) -> Vec<String>

Get external libraries used by a file

Source

pub fn get_files_using_library(&self, library_name: &str) -> Vec<FileId>

Get all files that use a specific external library

Source

pub fn stats(&self) -> GraphStats

Get statistics about the graph

Source

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.

Trait Implementations§

Source§

impl Clone for CodeGraph

Source§

fn clone(&self) -> CodeGraph

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CodeGraph

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CodeGraph

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for CodeGraph

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for CodeGraph

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,