Skip to content

Code Intelligence

Xberg integrates tree-sitter-language-pack (TSLP) to parse source code files. When you extract a source code file, Xberg detects the programming language, parses it with tree-sitter, and emits the source as content split at semantic boundaries – functions, classes, and modules – rather than fixed line counts.

Language support covers 371 programming languages via tree-sitter grammars. See the TSLP documentation for the full language list.

See the TreeSitterConfig reference for all configuration options.

Extracting a source code file produces:

  • content – the source text, split into code segments at tree-sitter chunk boundaries and rendered in the configured output format. When chunking is enabled, each segment is preceded by a heading naming its enclosing function, class, or module. With chunking disabled (the default), the whole source is emitted as a single code block.
  • metadata.format – tagged as code (format_type: "code"), carrying the structural chunks and, when data extraction is enabled, the hierarchical data tree.
  • code_intelligence – tree-sitter’s full analysis as an opaque JSON object: language, metrics, structure, imports, exports, comments, docstrings, symbols, diagnostics, chunks, and the data tree. It is populated for source files when the tree-sitter feature is enabled, and null otherwise.

code_intelligence is the serialized tree_sitter_language_pack::ProcessResult. It is deliberately untyped so every binding (Go, Java, C#, …) can read it as a raw JSON object; deserialize it against TSLP’s own schema if you need typed access.

Code extraction is enabled by default when the tree-sitter feature flag is active. Extract a source code file and read content:

basic.rs
use xberg::{extract, ExtractInput, ExtractionConfig};
let config = ExtractionConfig::default();
let output = extract(ExtractInput::from_uri("app.py"), &config).await?;
let result = &output.results[0];
// The rendered source is in the content field.
println!("{}", result.content);
// metadata.format is tagged as Code and carries the structural chunks.
if let Some(xberg::types::FormatMetadata::Code(code)) = &result.metadata.format {
println!("Detected a source code file with {} chunks", code.chunks.len());
}
// The full tree-sitter analysis is an opaque JSON value.
if let Some(intelligence) = &result.code_intelligence {
println!("{intelligence}");
}

Use TreeSitterConfig to control tree-sitter processing. Set enabled: false to skip code intelligence entirely. chunk_max_size controls where the source is split into segments; when unset (the default), the whole file is emitted as a single block.

config.rs
use xberg::{ExtractionConfig, TreeSitterConfig, TreeSitterProcessConfig};
let config = ExtractionConfig {
tree_sitter: Some(TreeSitterConfig {
process: TreeSitterProcessConfig {
chunk_max_size: Some(4096), // split source at chunk boundaries
..Default::default()
},
..Default::default()
}),
..Default::default()
};

See TreeSitterConfig and TreeSitterProcessConfig for all fields.

With chunk_max_size set, tree-sitter splits the source at function, class, and module boundaries and Xberg emits each chunk as a separate code segment in content, preceded by a heading naming its enclosing scope:

chunked.py
import xberg
config = xberg.ExtractionConfig(
tree_sitter={"process": {"chunk_max_size": 2048}}
)
output = await xberg.extract(
xberg.ExtractInput(kind="uri", uri="large_module.py"), config=config
)
result = output.results[0]
# Chunk boundaries are reflected in the layout of result.content.
print(result.content)

Structured per-chunk data is also available without parsing content: each entry of metadata.format.chunks carries the chunk text, its context_path (enclosing scopes), the tree-sitter node_types, and the byte_start/byte_end offsets into the source. For general-purpose text chunking across all formats – for example, in a RAG pipeline – use Xberg’s chunking pipeline instead.

Xberg detects the programming language in two ways:

  1. File extension (fast path) – when using extract, the extension is matched against 248 known language extensions
  2. Shebang line (fallback) – when the extension is missing or ambiguous, the first line is checked for #!/usr/bin/env python, #!/bin/bash, and so on.

If neither method identifies the language, extraction returns an UnsupportedFormat error. The detected language drives parsing and is reported in code_intelligence.

Tree-sitter-language-pack supports 371 programming languages, including Python, Rust, TypeScript, JavaScript, Go, Java, C/C++, Ruby, PHP, C#, Swift, Kotlin, and Elixir. For the full list, see the TSLP language reference.