Skip to content

Kotlin (Android) API Reference

Kotlin (Android) API Reference v1.9.0-rc.17

Functions

detectLanguageFromExtension()

Detect language name from a file extension (without leading dot).

Returns null for unrecognized extensions. The match is case-insensitive.

Signature:

fun detectLanguageFromExtension(ext: String): String?

Parameters:

Name Type Required Description
ext String Yes The ext

Returns: String?


detectLanguageFromPath()

Detect language name from a file path.

Extracts the file extension and looks it up. Returns null if the path has no extension or the extension is not recognized.

Signature:

fun detectLanguageFromPath(path: String): String?

Parameters:

Name Type Required Description
path String Yes Path to the file

Returns: String?


detectLanguageFromContent()

Detect language name from file content using the shebang line (#!).

Inspects only the first line of content. If it begins with #!, the interpreter name is extracted and mapped to a language name.

Handles common patterns:

  • #!/usr/bin/env python3"python"
  • #!/bin/bash"bash"
  • #!/usr/bin/env node"javascript"

The -S flag accepted by some env implementations is skipped automatically. Version suffixes (e.g. python3.11, ruby3.2) are stripped before matching.

Returns null when content does not start with #!, the shebang is malformed, or the interpreter is not recognised.

Signature:

fun detectLanguageFromContent(content: String): String?

Parameters:

Name Type Required Description
content String Yes The content to process

Returns: String?


getHighlightsQuery()

Get the highlights query for a language, if bundled.

Returns the contents of highlights.scm as a static string, or null if no highlights query is bundled for this language.

Signature:

fun getHighlightsQuery(language: String): String?

Parameters:

Name Type Required Description
language String Yes The language

Returns: String?


getInjectionsQuery()

Get the injections query for a language, if bundled.

Returns the contents of injections.scm as a static string, or null if no injections query is bundled for this language.

Signature:

fun getInjectionsQuery(language: String): String?

Parameters:

Name Type Required Description
language String Yes The language

Returns: String?


getLocalsQuery()

Get the locals query for a language, if bundled.

Returns the contents of locals.scm as a static string, or null if no locals query is bundled for this language.

Signature:

fun getLocalsQuery(language: String): String?

Parameters:

Name Type Required Description
language String Yes The language

Returns: String?


getLanguage()

Get a tree-sitter Language by name using the global registry.

Resolves language aliases (e.g., "shell" maps to "bash"). When the download feature is enabled (default), automatically downloads the parser from GitHub releases if not found locally.

Errors:

Returns Error.LanguageNotFound if the language is not recognized, or Error.Download if auto-download fails.

Signature:

@Throws(Error::class)
fun getLanguage(name: String): Language

Parameters:

Name Type Required Description
name String Yes The name

Returns: Language Errors: Throws Error.


getParser()

Get a Parser pre-configured for the given language.

This is a convenience function that calls get_language and configures a new parser in one step.

Errors:

Returns Error.LanguageNotFound if the language is not recognized, or Error.ParserSetup if the language cannot be applied to the parser.

Signature:

@Throws(Error::class)
fun getParser(name: String): Parser

Parameters:

Name Type Required Description
name String Yes The name

Returns: Parser Errors: Throws Error.


detectLanguage()

Detect language name from a file path or extension.

This compatibility alias matches the pre-Alef Python binding API.

Signature:

fun detectLanguage(path: String): String?

Parameters:

Name Type Required Description
path String Yes Path to the file

Returns: String?


availableLanguages()

List all available language names (sorted, deduplicated, includes aliases).

Returns names of both statically compiled and dynamically loadable languages, plus any configured aliases.

Signature:

fun availableLanguages(): List<String>

Returns: List<String>


hasLanguage()

Check if a language is available by name or alias.

Returns true if the language can be loaded (statically compiled, dynamically available, or a known alias for one of these).

Signature:

fun hasLanguage(name: String): Boolean

Parameters:

Name Type Required Description
name String Yes The name

Returns: Boolean


languageCount()

Return the number of available languages.

Includes statically compiled languages, dynamically loadable languages, and aliases.

Signature:

fun languageCount(): Long

Returns: Long


process()

Process source code and extract file intelligence using the global registry.

Parses the source with tree-sitter and extracts metrics, structure, imports, exports, comments, docstrings, symbols, diagnostics, and/or chunks based on the flags set in ProcessConfig.

Errors:

Returns an error if the language is not found or parsing fails.

Signature:

@Throws(Error::class)
fun process(source: String, config: ProcessConfig): ProcessResult

Parameters:

Name Type Required Description
source String Yes The source
config ProcessConfig Yes The configuration options

Returns: ProcessResult Errors: Throws Error.


init()

Initialize the language pack with the given configuration.

Applies any custom cache directory, then downloads all languages and groups specified in the config. This is the recommended entry point when you want to pre-warm the cache before use.

Errors:

Returns an error if configuration cannot be applied or if downloads fail.

Signature:

@Throws(Error::class)
fun init(config: PackConfig)

Parameters:

Name Type Required Description
config PackConfig Yes The configuration options

Returns: Unit Errors: Throws Error.


configure()

Apply download configuration without downloading anything.

Use this to set a custom cache directory before the first call to get_language or any download function. Changing the cache dir after languages have been registered has no effect on already-loaded languages.

Errors:

Returns an error if the lock cannot be acquired.

Signature:

@Throws(Error::class)
fun configure(config: PackConfig)

Parameters:

Name Type Required Description
config PackConfig Yes The configuration options

Returns: Unit Errors: Throws Error.


download()

Download specific languages to the local cache.

Returns the number of requested languages available after the call. Already compiled or cached languages are included in the count.

Errors:

Returns an error if any language is not available in the manifest or if the download fails.

Signature:

@Throws(Error::class)
fun download(names: List<String>): Long

Parameters:

Name Type Required Description
names List<String> Yes The names

Returns: Long Errors: Throws Error.


downloadAll()

Download all available languages from the remote manifest.

Downloads the platform bundle and extracts every library it contains. Languages that appear in the manifest but are absent from the bundle (e.g. grammars that failed to compile at release time) are silently skipped — they are not treated as an error.

Returns the total number of languages now available (statically compiled plus downloaded and cached).

Errors:

Returns an error if the manifest cannot be fetched or the bundle download fails.

Signature:

@Throws(Error::class)
fun downloadAll(): Long

Returns: Long Errors: Throws Error.


downloadGroup()

Download every language in a named group (e.g. "web", "data").

Groups are defined in the remote manifest and let you ensure a curated set of related grammars in one call instead of listing each name to download. Already-cached languages are skipped.

Returns the total number of languages now available (statically compiled plus downloaded and cached).

Errors:

Returns an error if the manifest cannot be fetched, the group is unknown, or any constituent language fails to download.

Signature:

@Throws(Error::class)
fun downloadGroup(name: String): Long

Parameters:

Name Type Required Description
name String Yes The name

Returns: Long Errors: Throws Error.


manifestLanguages()

Return all language names available in the remote manifest (306).

Fetches (and caches) the remote manifest to discover the full list of downloadable languages. Use downloaded_languages to list what is already cached locally.

Errors:

Returns an error if the manifest cannot be fetched.

Signature:

@Throws(Error::class)
fun manifestLanguages(): List<String>

Returns: List<String> Errors: Throws Error.


downloadedLanguages()

Return languages that are already downloaded and cached locally.

Does not perform any network requests. Returns an empty list if the cache directory does not exist or cannot be read.

Signature:

fun downloadedLanguages(): List<String>

Returns: List<String>


cleanCache()

Delete all cached parser shared libraries.

Resets the cache registration so the next call to get_language or a download function will re-register the (now empty) cache directory.

Errors:

Returns an error if the cache directory cannot be removed.

Signature:

@Throws(Error::class)
fun cleanCache()

Returns: Unit Errors: Throws Error.


cacheDir()

Return the effective cache directory path.

This is either the custom path set via configure / init or the default: ~/.cache/tree-sitter-language-pack/v{version}/libs/.

Errors:

Returns an error if the system cache directory cannot be determined.

Signature:

@Throws(Error::class)
fun cacheDir(): String

Returns: String Errors: Throws Error.


Types

ByteRange

A byte range — start (inclusive) to end (exclusive).

Field Type Default Description
start Long Inclusive start byte offset.
end Long Exclusive end byte offset.

ChunkContext

Metadata for a single chunk of source code.

Field Type Default Description
language String Language
chunkIndex Long Chunk index
totalChunks Long Total chunks
nodeTypes List<String> [] Node types
contextPath List<String> [] Context path
symbolsDefined List<String> [] Symbols defined
comments List<CommentInfo> [] Comments
docstrings List<DocstringInfo> [] Docstrings
hasErrorNodes Boolean Whether error nodes

CodeChunk

A chunk of source code with rich metadata.

Field Type Default Description
content String The extracted text content
startByte Long Start byte
endByte Long End byte
startLine Long Start line
endLine Long End line
metadata ChunkContext Document metadata

CommentInfo

A comment extracted from source code.

Field Type Default Description
text String Text
kind CommentKind CommentKind.Line Kind (comment kind)
span Span Span (span)
associatedNode String? null Associated node

Diagnostic

A diagnostic (syntax error, missing node, etc.) from parsing.

Field Type Default Description
message String Message
severity DiagnosticSeverity DiagnosticSeverity.Error Severity (diagnostic severity)
span Span Span (span)

DocSection

A section within a docstring (e.g., Args, Returns, Raises).

Field Type Default Description
kind String Kind
name String? null The name
description String Human-readable description

DocstringInfo

A docstring extracted from source code.

Field Type Default Description
text String Text
format DocstringFormat DocstringFormat.PythonTripleQuote Format (docstring format)
span Span Span (span)
associatedItem String? null Associated item
parsedSections List<DocSection> [] Parsed sections

DownloadManager

Manages downloading and caching of pre-built parser shared libraries.

Methods

new()

Create a new download manager for the given version.

Signature:

@Throws(Error::class)
@JvmStatic
fun new(version: String): DownloadManager

withCacheDir()

Create a download manager with a custom cache directory.

Signature:

@JvmStatic
fun withCacheDir(version: String, cacheDir: Path): DownloadManager

installedLanguages()

List languages that are already downloaded and cached.

Signature:

fun installedLanguages(): List<String>

downloadAllBestEffort()

Download the platform bundle and extract every library file it contains.

Unlike ensure_languages, this does not check the manifest language list against archive contents — it simply extracts all .so/.dylib/.dll files from the bundle. Languages in the manifest that are missing from the archive are silently ignored rather than returning an error.

Returns the number of library files extracted (including those already cached).

Signature:

@Throws(Error::class)
fun downloadAllBestEffort(): Long

cleanCache()

Remove all cached parser libraries.

Acquires the cross-process lock so clean_cache cannot race a concurrent downloader (avoids Windows sharing-violation errors against an in-flight bundle write). The .download.lock file itself is not removed — it is permanent infrastructure; deleting it could allow a concurrent process that already opened the file to continue holding a stale lock handle while a new process opens a fresh inode, breaking the mutual-exclusion guarantee.

Signature:

@Throws(Error::class)
fun cleanCache()

ExportInfo

An export statement extracted from source code.

Field Type Default Description
name String The name
kind ExportKind ExportKind.Named Kind (export kind)
span Span Span (span)

FileMetrics

Aggregate metrics for a source file.

Field Type Default Description
totalLines Long Total lines
codeLines Long Code lines
commentLines Long Comment lines
blankLines Long Blank lines
totalBytes Long Total bytes
nodeCount Long Number of nodes
errorCount Long Number of errors
maxDepth Long Maximum depth

ImportInfo

An import statement extracted from source code.

Field Type Default Description
source String Source
items List<String> [] Items
alias String? null Alias
isWildcard Boolean Whether wildcard
span Span Span (span)

Language


LanguageRegistry

Thread-safe registry of tree-sitter language parsers.

Manages both statically compiled and dynamically loaded language grammars. Use LanguageRegistry.new() for the default registry, or access the global instance via the module-level convenience functions (get_language, available_languages, etc.).

Methods

getLanguage()

Get a tree-sitter Language by name.

Resolves aliases (e.g., "shell" -> "bash", "makefile" -> "make"), then looks up the language in the static table. When the dynamic-loading feature is enabled, falls back to loading a shared library on demand.

Errors:

Returns Error.LanguageNotFound if the name (after alias resolution) does not match any known grammar.

Signature:

@Throws(Error::class)
fun getLanguage(name: String): Language

availableLanguages()

List all available language names, sorted and deduplicated.

Includes statically compiled languages, dynamically loadable languages (if the dynamic-loading feature is enabled), and all configured aliases.

Signature:

fun availableLanguages(): List<String>

hasParser()

Check whether a parser is statically compiled into this build.

Returns true only when the grammar was compiled in at build time (i.e. it appears in the STATIC_LANGUAGES table). This is independent of the extension-to-language mapping: detect_language_from_extension consults the static ext table for all 306 grammars regardless of which parsers are compiled in.

Use this when you need to distinguish "we know the language name" from "we can actually parse files in that language right now".

use tree_sitter_language_pack::{detect_language_from_extension, LanguageRegistry};

let registry = LanguageRegistry::new();
// Extension detection uses the static table — independent of compiled parsers.
let lang = detect_language_from_extension("feature"); // always returns Some("gherkin")
// Parser availability depends on which grammars were compiled in.
let can_parse = lang.map(|name| registry.has_parser(name)).unwrap_or(false);

Signature:

fun hasParser(name: String): Boolean

hasLanguage()

Check whether a language is available by name or alias.

Returns true if the language can be loaded, either from the static table or from a dynamic library on disk.

Signature:

fun hasLanguage(name: String): Boolean

languageCount()

Return the total number of available languages (including aliases).

Signature:

fun languageCount(): Long

process()

Parse source code and extract file intelligence based on config in a single pass.

Signature:

@Throws(Error::class)
fun process(source: String, config: ProcessConfig): ProcessResult

default()

Signature:

@JvmStatic
fun default(): LanguageRegistry

Node

A single syntax node within a Tree.

Nodes hold a strong reference to their parent tree so they remain valid regardless of how the tree is moved or stored at the FFI boundary.

Methods

clone()

Signature:

fun clone(): Node

kind()

Return the node's kind name (e.g. "function_definition").

Signature:

fun kind(): String

kindId()

Return the node's numeric kind ID.

Tree-sitter assigns a stable u16 ID to every node kind in a grammar (e.g. "function_definition" → 42). Comparing kind_id() is cheaper than comparing the string kind() in tight AST loops.

Signature:

fun kindId(): Short

startByte()

Return the inclusive start byte offset of this node.

Signature:

fun startByte(): Long

endByte()

Return the exclusive end byte offset of this node.

Signature:

fun endByte(): Long

byteRange()

Return the node's byte range as a ByteRange.

Callers should slice their own source bytes — this is a zero-copy text accessor.

Signature:

fun byteRange(): ByteRange

startPosition()

Return the start Point (row, column).

Signature:

fun startPosition(): Point

endPosition()

Return the end Point (row, column).

Signature:

fun endPosition(): Point

isNamed()

True when this node is named (not punctuation/whitespace).

Signature:

fun isNamed(): Boolean

isError()

True when this is an error node.

Signature:

fun isError(): Boolean

isMissing()

True when this is a missing-token node.

Signature:

fun isMissing(): Boolean

isExtra()

True when this is an "extra" node (e.g. a comment).

Signature:

fun isExtra(): Boolean

hasError()

True when this node or any descendant is an error.

Signature:

fun hasError(): Boolean

parent()

Return this node's parent, if any.

Signature:

fun parent(): Node?

child()

Return the i-th child of this node, if any.

Signature:

fun child(index: Int): Node?

childCount()

Total number of children (including unnamed).

Signature:

fun childCount(): Long

namedChild()

Return the i-th named child of this node, if any.

Signature:

fun namedChild(index: Int): Node?

namedChildCount()

Number of named children of this node.

Signature:

fun namedChildCount(): Long

childByFieldName()

Look up a child by its grammar-defined field name.

Signature:

fun childByFieldName(name: String): Node?

toSexp()

Return the S-expression form of this node's subtree.

Signature:

fun toSexp(): String

walk()

Return a TreeCursor positioned at this node.

Signature:

fun walk(): TreeCursor

PackConfig

Configuration for the tree-sitter language pack.

Controls cache directory and which languages to pre-download. Can be loaded from a TOML file, constructed programmatically, or passed as a dict/object from language bindings.

Field Type Default Description
cacheDir Path? null Override default cache directory. Default: ~/.cache/tree-sitter-language-pack/v{version}/libs/
languages List<String>? [] Languages to pre-download on init. Each entry is a language name (e.g. "python", "rust").
groups List<String>? [] Language groups to pre-download (e.g. "web", "systems", "scripting").

Parser

A tree-sitter parser configured for one language at a time.

Methods

setLanguage()

Configure the parser to use the language identified by name (e.g. "python").

Resolves the language through the global registry — auto-downloading if necessary, when the download feature is enabled.

Errors:

Returns Error.LanguageNotFound if the language is not recognized, or Error.ParserSetup if the language ABI is incompatible.

Signature:

@Throws(Error::class)
fun setLanguage(name: String)

parse()

Parse a UTF-8 source string. Returns null if parsing was cancelled or no language is set.

Signature:

fun parse(source: String): Tree?

parseBytes()

Parse a raw byte slice. Returns null if parsing was cancelled or no language is set.

Signature:

fun parseBytes(source: ByteArray): Tree?

reset()

Reset internal state. The next call to parse will not be incremental.

Signature:

fun reset()

default()

Signature:

@JvmStatic
fun default(): Parser

Point

A source position — row + column, zero-indexed.

Field Type Default Description
row Long Zero-indexed row number.
column Long Zero-indexed column number, in UTF-16 code units.

ProcessConfig

Configuration for the process() function.

Controls which analysis features are enabled and whether chunking is performed.

Field Type Default Description
language String Language name (required).
structure Boolean true Extract structural items (functions, classes, etc.). Default: true.
imports Boolean true Extract import statements. Default: true.
exports Boolean true Extract export statements. Default: true.
comments Boolean false Extract comments. Default: false.
docstrings Boolean false Extract docstrings. Default: false.
symbols Boolean false Extract symbol definitions. Default: false.
diagnostics Boolean false Include parse diagnostics. Default: false.
chunkMaxSize Long? null Maximum chunk size in bytes. null disables chunking.

Methods

default()

Signature:

@JvmStatic
fun default(): ProcessConfig

withChunking()

Enable chunking with the given maximum chunk size in bytes.

Signature:

fun withChunking(maxSize: Long): ProcessConfig

all()

Enable all analysis features.

Signature:

fun all(): ProcessConfig

minimal()

Disable all analysis features (only metrics computed).

Signature:

fun minimal(): ProcessConfig

ProcessResult

Complete analysis result from processing a source file.

Contains metrics, structural analysis, imports/exports, comments, docstrings, symbols, diagnostics, and optionally chunked code segments. Fields are populated based on the ProcessConfig flags.

Field Type Default Description
language String Language
metrics FileMetrics Metrics (file metrics)
structure List<StructureItem> [] Structure
imports List<ImportInfo> [] Imports
exports List<ExportInfo> [] Exports
comments List<CommentInfo> [] Comments
docstrings List<DocstringInfo> [] Docstrings
symbols List<SymbolInfo> [] Symbols
diagnostics List<Diagnostic> [] Diagnostics
chunks List<CodeChunk> [] Text chunks for chunking/embedding

Span

Byte and line/column range in source code.

Represents both byte offsets (for slicing) and human-readable line/column positions (for display and diagnostics).

Field Type Default Description
startByte Long Start byte
endByte Long End byte
startLine Long Start line
startColumn Long Start column
endLine Long End line
endColumn Long End column

StructureItem

A structural item (function, class, struct, etc.) in source code.

Field Type Default Description
kind StructureKind StructureKind.Function Kind (structure kind)
name String? null The name
visibility String? null Visibility
span Span Span (span)
children List<StructureItem> [] Children
decorators List<String> [] Decorators
docComment String? null Doc comment
signature String? null Signature
bodySpan Span? null Body span (span)

SymbolInfo

A symbol (variable, function, type, etc.) extracted from source code.

Field Type Default Description
name String The name
kind SymbolKind SymbolKind.Variable Kind (symbol kind)
span Span Span (span)
typeAnnotation String? null Type annotation
doc String? null Doc

Tree

A parsed syntax tree. Cheap to clone (refcount bump).

Methods

rootNode()

Return the root Node of this tree.

Signature:

fun rootNode(): Node

walk()

Return a TreeCursor positioned at the root.

Signature:

fun walk(): TreeCursor

TreeCursor

A cursor for traversing a Tree.

Methods

node()

Return the Node at the cursor's current position.

Signature:

fun node(): Node

gotoFirstChild()

Move the cursor to the first child of the current node. Returns true if a child existed.

Signature:

fun gotoFirstChild(): Boolean

gotoParent()

Move the cursor to the parent of the current node. Returns true if a parent existed.

Signature:

fun gotoParent(): Boolean

gotoNextSibling()

Move the cursor to the next sibling of the current node. Returns true if a sibling existed.

Signature:

fun gotoNextSibling(): Boolean

fieldName()

Return the field name for the current node, if any.

Signature:

fun fieldName(): String?

Enums

StructureKind

The kind of structural item found in source code.

Categorizes top-level and nested declarations such as functions, classes, structs, enums, traits, and more. Use Other for language-specific constructs that do not fit a standard category.

Value Description
Function Function
Method Method
Class Class
Struct Struct
Interface Interface
Enum Enum
Module Module
Trait Trait
Impl Impl
Namespace Namespace
Other Other — Fields: 0: String

CommentKind

The kind of a comment found in source code.

Distinguishes between single-line comments, block (multi-line) comments, and documentation comments.

Value Description
Line Line
Block Block
Doc Doc

DocstringFormat

The format of a docstring extracted from source code.

Identifies the docstring convention used, which varies by language (e.g., Python triple-quoted strings, JSDoc, Rustdoc /// comments).

Value Description
PythonTripleQuote Python triple quote
JsDoc J s doc
Rustdoc Rustdoc
GoDoc Go doc
JavaDoc Java doc
Other Other — Fields: 0: String

ExportKind

The kind of an export statement found in source code.

Covers named exports, default exports, and re-exports from other modules.

Value Description
Named Named
Default Default
ReExport Re export

SymbolKind

The kind of a symbol definition found in source code.

Categorizes symbol definitions such as variables, constants, functions, classes, types, interfaces, enums, and modules.

Value Description
Variable Variable
Constant Constant
Function Function
Class Class
Type Type
Interface Interface
Enum Enum
Module Module
Other Other — Fields: 0: String

DiagnosticSeverity

Severity level of a diagnostic produced during parsing.

Used to classify parse errors, warnings, and informational messages found in the syntax tree.

Value Description
Error Error
Warning Warning
Info Info

Errors

Error

Errors that can occur when using the tree-sitter language pack.

Covers language lookup failures, parse errors, query errors, and I/O issues. Feature-gated variants are included when config, download, or related features are enabled.

Variant Description
LanguageNotFound Language '{0}' not found
DynamicLoad Dynamic library load error:
NullLanguagePointer Language function returned null pointer for '{0}'
ParserSetup Failed to set parser language:
LockPoisoned Registry lock poisoned:
Config Configuration error:
ParseFailed Parse failed: parsing returned no tree
QueryError Query error:
InvalidRange Invalid byte range:
Io IO error:
Download Download error:
ChecksumMismatch Checksum mismatch for '{file}': expected {expected}, got {actual}
CacheLock Download cache lock error:

Edit this page on GitHub