Skip to main content

CodeGraph

Struct CodeGraph 

Source
pub struct CodeGraph {
    pub graph: StableGraph<GirNode, GirEdge>,
    /* private fields */
}
Expand description

The central code knowledge graph.

Wraps a petgraph StableGraph with indexes for fast lookups by ID, name, and file.

Fields§

§graph: StableGraph<GirNode, GirEdge>

Implementations§

Source§

impl CodeGraph

Source

pub fn new() -> Self

Source

pub fn add_node(&mut self, node: GirNode) -> NodeIndex

Source

pub fn add_edge( &mut self, source: SymbolId, target: SymbolId, edge: GirEdge, ) -> bool

Source

pub fn merge(&mut self, output: ParseOutput)

Merge a ParseOutput into the graph.

Source

pub fn remove_file(&mut self, path: &Path)

Remove all nodes belonging to a file (for incremental re-indexing).

petgraph’s StableGraph::remove_node() also removes all edges incident to the node, so no dangling edges are left behind.

Source

pub fn validate(&self) -> Vec<String>

Validate graph invariants. Returns a list of issues found.

Checks that all indexes point to valid nodes and that no stale entries exist. Useful for debugging and post-load verification.

Source

pub fn node_count(&self) -> usize

Source

pub fn edge_count(&self) -> usize

Source

pub fn get_node(&self, id: SymbolId) -> Option<&GirNode>

Source

pub fn get_node_index(&self, id: SymbolId) -> Option<NodeIndex>

Source

pub fn find_by_name(&self, name: &str) -> Vec<&GirNode>

Source

pub fn find_by_file(&self, path: &Path) -> Vec<&GirNode>

Source

pub fn find_by_kind(&self, kind: NodeKind) -> Vec<&GirNode>

Source

pub fn outgoing(&self, id: SymbolId, edge_kind: EdgeKind) -> Vec<&GirNode>

Get all nodes connected by outgoing edges of a specific kind.

Source

pub fn incoming(&self, id: SymbolId, edge_kind: EdgeKind) -> Vec<&GirNode>

Get all nodes connected by incoming edges of a specific kind.

Source

pub fn callees(&self, id: SymbolId) -> Vec<&GirNode>

Get callees of a function/method.

Source

pub fn callers(&self, id: SymbolId) -> Vec<&GirNode>

Get callers of a function/method.

Source

pub fn children(&self, id: SymbolId) -> Vec<&GirNode>

Get children (contained symbols) of a node.

Source

pub fn parent(&self, id: SymbolId) -> Option<&GirNode>

Get the parent (container) of a node.

Source

pub fn is_phantom(&self, id: SymbolId) -> bool

Check if a node is a “phantom” call target — i.e. created by the parser to represent a call expression, not an actual symbol definition. Phantom nodes have no parent (no incoming Contains edge) and are not top-level File/Folder/Module nodes.

Source

pub fn is_interface_impl(&self, id: SymbolId) -> bool

Check if a callable node is an interface/trait implementation method. Works across all languages: checks whether the method’s parent type has any Implements or Inherits edges, meaning its methods may be called via dynamic dispatch and shouldn’t be flagged as dead.

Source

pub fn is_method_on_used_type(&self, id: SymbolId) -> bool

Structural check: is this a method on a type that is actively used?

Methods called via self.method() / this.method() are filtered at parse time to avoid noise, so they have no incoming Calls edges. Instead of hardcoding self/this dispatch, we use a structural signal: if the parent type (struct/class/enum) has incoming edges (it’s constructed or referenced), its private methods are likely alive via self-dispatch.

Source

pub fn is_decorated(&self, id: SymbolId) -> bool

Structural check: does this symbol (or a parent module) have any decorator / attribute annotations?

This is a language-agnostic heuristic for framework-registered symbols (tests, route handlers, event listeners, DI-managed beans, etc.). Instead of hardcoding per-language name prefixes like test_, we rely on the graph structure: if the symbol (or an ancestor module) carries an AnnotatedWith edge to a Decorator node, some framework knows about it and it should not be considered dead code.

Source

pub fn remove_phantom_nodes(&mut self) -> usize

Remove orphaned phantom nodes — phantoms with no incoming edges at all. Keeps phantom nodes that are call targets (have incoming Calls edges) since they represent useful callee information in context views.

Source

pub fn indexed_files(&self) -> Vec<&PathBuf>

All indexed file paths.

Source

pub fn all_nodes(&self) -> impl Iterator<Item = &GirNode>

Iterator over all nodes.

Source

pub fn all_nodes_mut(&mut self) -> impl Iterator<Item = &mut GirNode>

Mutable iterator over all nodes.

Source

pub fn remove_edges_by_kind(&mut self, kind: EdgeKind) -> usize

Remove all edges of a given kind from the entire graph. Used to clear analysis-phase edges before re-running analysis.

Source

pub fn remove_edges_by_kinds(&mut self, kinds: &[EdgeKind]) -> usize

Remove all edges of any of the given kinds from the entire graph.

Trait Implementations§

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<'de>>(deserializer: D) -> Result<Self, D::Error>

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

impl Serialize for CodeGraph

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

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