Skip to main content

mago_analyzer/plugin/hook/
program.rs

1//! Program hooks for intercepting program-level analysis.
2
3use mago_database::file::File;
4use mago_syntax::cst::Program;
5
6use crate::plugin::context::HookContext;
7use crate::plugin::hook::HookAction;
8use crate::plugin::hook::HookResult;
9use crate::plugin::provider::Provider;
10
11/// Hook trait for intercepting program-level analysis.
12///
13/// This hook is called before and after analyzing a complete program (file).
14/// It receives the source file, full AST, and mutable context, allowing hooks to:
15/// - Access file information (id, path, content)
16/// - Inspect the entire program structure
17/// - Report issues at the program level
18/// - Pre-register variables before analysis
19/// - Skip analysis entirely
20pub trait ProgramHook: Provider {
21    /// Called before a program is analyzed.
22    ///
23    /// Return `HookAction::Continue` to proceed with normal analysis, or
24    /// `HookAction::Skip` to skip analysis of this program entirely.
25    ///
26    /// # Errors
27    ///
28    /// Returns [`HookError`] if the underlying plugin implementation propagates one.
29    fn before_program(
30        &self,
31        _file: &File,
32        _program: &Program<'_>,
33        _context: &mut HookContext<'_, '_>,
34    ) -> HookResult<HookAction> {
35        Ok(HookAction::Continue)
36    }
37
38    /// Called after a program has been analyzed.
39    ///
40    /// # Errors
41    ///
42    /// Returns [`HookError`] if the underlying plugin implementation propagates one.
43    fn after_program(
44        &self,
45        _file: &File,
46        _program: &Program<'_>,
47        _context: &mut HookContext<'_, '_>,
48    ) -> HookResult<()> {
49        Ok(())
50    }
51}