code_analyze_core/lib.rs
1// SPDX-FileCopyrightText: 2026 code-analyze-mcp contributors
2// SPDX-License-Identifier: Apache-2.0
3//! Multi-language code structure analysis library using tree-sitter.
4//!
5//! This crate provides core analysis functionality for extracting code structure
6//! from multiple programming languages. It is designed to be used as a library
7//! by MCP servers and other tools.
8//!
9//! # Features
10//!
11//! - **Language support**: Rust, Go, Java, Python, TypeScript, TSX, Fortran, JavaScript, C/C++, C# (feature-gated)
12//! - **Schema generation**: Optional JSON schema support via the `schemars` feature
13//! - **Async-friendly**: Uses tokio for concurrent analysis
14//! - **Cancellation support**: Built-in cancellation token support
15//!
16//! # Examples
17//!
18//! ```no_run
19//! use code_analyze_core::analyze::analyze_directory;
20//! use std::path::Path;
21//!
22//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
23//! let output = analyze_directory(Path::new("src"), None)?;
24//! println!("Files: {:?}", output.files.len());
25//! # Ok(())
26//! # }
27//! ```
28
29pub mod analyze;
30pub mod cache;
31pub mod completion;
32mod config;
33pub mod formatter;
34pub mod formatter_defuse;
35pub mod graph;
36pub mod lang;
37pub mod languages;
38pub mod pagination;
39pub mod parser;
40pub mod test_detection;
41pub mod traversal;
42pub mod types;
43
44#[cfg(feature = "schemars")]
45pub mod schema_helpers;
46
47pub(crate) const EXCLUDED_DIRS: &[&str] = &[
48 "node_modules",
49 "vendor",
50 ".git",
51 "__pycache__",
52 "target",
53 "dist",
54 "build",
55 ".venv",
56];
57
58// Re-exports of key public APIs
59pub use analyze::{
60 AnalysisOutput, AnalyzeError, CallChainEntry, FileAnalysisOutput, FocusedAnalysisConfig,
61 FocusedAnalysisOutput, analyze_directory, analyze_directory_with_progress, analyze_file,
62 analyze_focused, analyze_focused_with_progress, analyze_focused_with_progress_with_entries,
63 analyze_module_file, analyze_str,
64};
65pub use config::AnalysisConfig;
66pub use lang::{language_for_extension, supported_languages};
67pub use parser::ParserError;
68pub use types::*;
69
70/// Captures from a custom tree-sitter query.
71#[derive(Debug, Clone)]
72pub struct QueryCapture {
73 /// The capture name from the query (without leading `@`).
74 pub capture_name: String,
75 /// The matched source text.
76 pub text: String,
77 /// Start line (0-indexed).
78 pub start_line: usize,
79 /// End line (0-indexed, inclusive).
80 pub end_line: usize,
81 /// Start byte offset.
82 pub start_byte: usize,
83 /// End byte offset.
84 pub end_byte: usize,
85}
86
87/// Execute a custom tree-sitter query against source code.
88///
89/// # Arguments
90///
91/// * `language` - Language name (e.g., "rust", "python"). Must be an enabled language feature.
92/// * `source` - Source code to query.
93/// * `query` - A tree-sitter query string (S-expression syntax).
94///
95/// # Returns
96///
97/// A vector of [`QueryCapture`] results, or a [`ParserError`] if the query is malformed
98/// or the language is not supported.
99///
100/// # Security note
101///
102/// This function accepts user-controlled `query` strings. Pathological queries against
103/// large `source` inputs may cause CPU exhaustion. Callers in untrusted environments
104/// should bound the length of both `source` and `query` before calling this function.
105/// `Query::new()` returns `Err` on malformed queries rather than panicking.
106pub fn execute_query(
107 language: &str,
108 source: &str,
109 query: &str,
110) -> Result<Vec<QueryCapture>, parser::ParserError> {
111 parser::execute_query_impl(language, source, query)
112}