Skip to main content

tree_sitter_language_pack/
process_config.rs

1use std::borrow::Cow;
2
3use ahash::AHashMap;
4
5/// Configuration for the `process()` function.
6///
7/// Controls which analysis features are enabled and whether chunking is performed.
8///
9/// # Examples
10///
11/// ```
12/// use tree_sitter_language_pack::ProcessConfig;
13///
14/// // Defaults: structure + imports + exports enabled
15/// let config = ProcessConfig::new("python");
16///
17/// // With chunking
18/// let config = ProcessConfig::new("python").with_chunking(1000);
19///
20/// // Everything enabled
21/// let config = ProcessConfig::new("python").all();
22/// ```
23#[derive(Debug, Clone)]
24#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
25pub struct ProcessConfig {
26    /// Language name (required).
27    pub language: Cow<'static, str>,
28    /// Extract structural items (functions, classes, etc.). Default: true.
29    #[cfg_attr(feature = "serde", serde(default = "default_true"))]
30    pub structure: bool,
31    /// Extract import statements. Default: true.
32    #[cfg_attr(feature = "serde", serde(default = "default_true"))]
33    pub imports: bool,
34    /// Extract export statements. Default: true.
35    #[cfg_attr(feature = "serde", serde(default = "default_true"))]
36    pub exports: bool,
37    /// Extract comments. Default: false.
38    #[cfg_attr(feature = "serde", serde(default))]
39    pub comments: bool,
40    /// Extract docstrings. Default: false.
41    #[cfg_attr(feature = "serde", serde(default))]
42    pub docstrings: bool,
43    /// Extract symbol definitions. Default: false.
44    #[cfg_attr(feature = "serde", serde(default))]
45    pub symbols: bool,
46    /// Include parse diagnostics. Default: false.
47    #[cfg_attr(feature = "serde", serde(default))]
48    pub diagnostics: bool,
49    /// Maximum chunk size in bytes. `None` disables chunking.
50    #[cfg_attr(feature = "serde", serde(default))]
51    pub chunk_max_size: Option<usize>,
52    /// Custom extraction patterns to run against the parsed tree.
53    /// Keys become the keys in `ProcessResult::extractions`.
54    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none", default))]
55    pub extractions: Option<AHashMap<String, crate::extract::ExtractionPattern>>,
56}
57
58#[cfg(feature = "serde")]
59fn default_true() -> bool {
60    true
61}
62
63impl Default for ProcessConfig {
64    fn default() -> Self {
65        Self {
66            language: Cow::Borrowed(""),
67            structure: true,
68            imports: true,
69            exports: true,
70            comments: false,
71            docstrings: false,
72            symbols: false,
73            diagnostics: false,
74            chunk_max_size: None,
75            extractions: None,
76        }
77    }
78}
79
80impl ProcessConfig {
81    /// Create a new config for the given language with default settings.
82    pub fn new(language: impl Into<String>) -> Self {
83        Self {
84            language: Cow::Owned(language.into()),
85            ..Default::default()
86        }
87    }
88
89    /// Enable chunking with the given maximum chunk size in bytes.
90    pub fn with_chunking(mut self, max_size: usize) -> Self {
91        self.chunk_max_size = Some(max_size);
92        self
93    }
94
95    /// Enable all analysis features.
96    pub fn all(mut self) -> Self {
97        self.structure = true;
98        self.imports = true;
99        self.exports = true;
100        self.comments = true;
101        self.docstrings = true;
102        self.symbols = true;
103        self.diagnostics = true;
104        self
105    }
106
107    /// Disable all analysis features (only metrics computed).
108    pub fn minimal(mut self) -> Self {
109        self.structure = false;
110        self.imports = false;
111        self.exports = false;
112        self.comments = false;
113        self.docstrings = false;
114        self.symbols = false;
115        self.diagnostics = false;
116        self
117    }
118}