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