Skip to main content

Crate termosaic_tree_sitter

Crate termosaic_tree_sitter 

Source
Expand description

§termosaic-tree-sitter

Tree-sitter integration for Termosaic’s semantic document model.

The crate supports two complementary paths:

  • SemanticHighlighter converts Tree-sitter highlight events into fixed-width, semantically tokenized termosaic::Doc values.
  • TreeFormatter lets a language integration walk its syntax tree and construct structural layout choices directly.

It owns no grammar dependencies. Applications provide a Tree-sitter language, queries, and optional injection configurations while Termosaic supplies the semantic vocabulary and theme resolution.

use termosaic::tokens;
use termosaic_tree_sitter::{
    CaptureMap, CodeOptions, LanguageConfiguration, SemanticHighlighter,
};

let mut captures = CaptureMap::standard().customize();
captures.set("string.special.key", [tokens::SYNTAX_PROPERTY])?;
captures.set("escape", [tokens::SYNTAX_STRING_ESCAPE])?;

let language = LanguageConfiguration::from_language(
    tree_sitter_json::LANGUAGE.into(),
    "json",
    tree_sitter_json::HIGHLIGHTS_QUERY,
    "",
    "",
    captures.build(),
)?;
let options = CodeOptions::new().root_token(tokens::LANGUAGE_JSON);
let doc = SemanticHighlighter::new().document(
    &language,
    r#"{"status":"ready"}"#,
    &options,
)?;

The grammar in this example is a development dependency only; consumers select and version their own Tree-sitter grammars. The crate supports Rust 1.88 and newer, matching Termosaic itself. LanguageConfiguration::standard(language, name, highlights_query) is the shortest path for grammars whose queries use the standard capture vocabulary. standard_with_queries adds injection and local-variable queries, from_language accepts a customized CaptureMap, and new remains available for applications that already own a raw HighlightConfiguration.

Injected languages use the same ordered capture vocabulary because Tree-sitter highlight indices are positional. LanguageConfiguration::for_injection reuses the parent map. Independently built maps are accepted when CaptureMap::is_compatible_with confirms matching names, order, and token stacks.

§Dependency compatibility

This release targets tree-sitter and tree-sitter-highlight 0.26.11 and compatible 0.26 patch releases. Both crates are re-exported as termosaic_tree_sitter::tree_sitter and termosaic_tree_sitter::tree_sitter_highlight. These paths avoid duplicate-version type mismatches. Grammar crates remain application-selected and are not dependencies of this adapter.

§License

MIT

Re-exports§

pub use tree_sitter;
pub use tree_sitter_highlight;

Structs§

CaptureMap
Ordered Tree-sitter highlight names and their Termosaic semantic tokens.
CaptureMapBuilder
CodeOptions
LanguageConfiguration
A Tree-sitter language configuration bound to one capture map.
SemanticHighlighter
Reusable Tree-sitter parser/query state.
TreeLayoutContext
Source access and semantic leaf construction for language-specific tree formatters.

Enums§

CaptureMapError
HighlightError
TreeSourceError

Traits§

TreeFormatter
Language-owned conversion from syntax nodes to structural Termosaic layout.

Functions§

document_from_tree
Walk a parsed tree through a language formatter and attach configured root semantics.