lattice_syntax/lib.rs
1//! Tree-sitter-backed syntax highlighting for `lattice` (DESIGN.md §5.3).
2//!
3//! ## Status
4//!
5//! - Languages bundled: Rust, Python, JavaScript, Markdown (block +
6//! inline split), plus a `Plain` no-op fallback.
7//! - **Incremental reparse landed (Option B, slices B.1–B.5).**
8//! `Syntax::parse_at_with_edits` accepts `EditDelta`s, applies
9//! `tree.edit()` to the cached tree, and runs
10//! `Parser::parse(_, Some(&old_tree))` so unchanged subtrees are
11//! reused. `SyntaxHandle` runs the parse on a `spawn_blocking`
12//! worker; `request_reparse` takes `Buffer` (O(1) Arc bump) so
13//! the input thread doesn't allocate the source. App-side cache
14//! on `App.visible_highlights_key` short-circuits the per-frame
15//! `highlight_lines` walk when nothing changed.
16//! - **Flicker-free updates landed (C-series, slices C.1–C.5).**
17//! The C-series turns the algorithmically-correct B-series into
18//! a user-visibly-flicker-free experience. The two-stage parse
19//! exposes `Syntax::try_apply_intermediate` (fast: tree.edit +
20//! source/version update, no parse) and `reparse_with_cached_tree`
21//! (slow: Parser::parse with cached tree as seed) so the
22//! `SyntaxHandle` worker can publish a byte-aligned intermediate
23//! snapshot before the parse completes. `App` synchronously
24//! line-shifts and byte-shifts its `visible_highlights` cache on
25//! every edit so held spans stay aligned with current content;
26//! the renderer never sees an empty/wrong intermediate. Grammar-
27//! driven edits (operators) flow through the same chokepoint
28//! so the C.x logic applies uniformly.
29//! - Plugin extension API used by builtins, not yet by plugins.
30//!
31//! Adding a new language:
32//!
33//! 1. Add the `tree-sitter-<lang>` crate as a dep.
34//! 2. Register it in [`registry::LangRegistry::standard`].
35//! 3. Add a variant to `Lang` (and update `Lang::detect_from_path`
36//! for the canonical extension).
37//!
38//! ## Injections
39//!
40//! The shared [`registry::LangRegistry`] holds every registered
41//! grammar's `HighlightConfiguration`; the per-document `Syntax`
42//! borrows refs through it. The injection callback looks up
43//! sibling configs by name -- so a markdown `\`\`\`rust ... \`\`\``
44//! block recurses into the rust config, and a markdown paragraph
45//! injects the inline-markdown parser. New languages drop in to
46//! the registry and become injection targets without further
47//! wiring.
48//!
49//! Capture-name -> `Style` mapping lives in `style.rs`. The mapping
50//! is the v1 stand-in for the themable name-to-color tables
51//! described in §5.6 style mappings.
52
53pub mod handle;
54// IN.1: the indent engine. Beside `motions` / `text_objects` --
55// same shape (computation over the tree, driven by this crate's
56// `.scm` files). IN.2 adds the `indents.scm` evaluator here.
57pub mod indent;
58pub mod lang;
59pub mod modes;
60// LG.2: languages registered at runtime rather than compiled in.
61pub mod motions;
62pub mod oneshot;
63// H.2 (2026-08-29): per-language display-time elision rules.
64// See `docs/dev/architecture/conceal.md`.
65pub mod conceal;
66pub mod plugin_lang;
67pub mod registry;
68pub mod style;
69pub mod syntax;
70pub mod text_objects;
71// LG.3b: loading grammars from wasm, and the stores parsers need for them.
72pub mod wasm_grammar;
73// DX.2 (BC.6): the syntax->theme-element style bridge, moved down from
74// `lattice-host::ui::theme` so `lattice-diff` can reach it.
75pub mod theme_style;
76
77pub use crate::handle::SyntaxHandle;
78pub use crate::indent::{
79 BracketSyntax, electric_columns, indent_columns_for_new_line, indent_for_new_line,
80 is_electric_trigger, tree_levels_for_line, tree_levels_for_new_line,
81};
82pub use crate::lang::Lang;
83pub use crate::modes::{
84 JavascriptMode, MarkdownMode, PythonMode, RustMode, TREE_SITTER_COMPLETION_SOURCE_ID,
85 TreeSitterCompletionMode, TreeSitterSymbolSource, lang_for_mode_id, major_mode_id_for_lang,
86 register_language_modes,
87};
88pub use crate::motions::{SyntaxMotionIds, register_syntax_motions};
89pub use crate::oneshot::oneshot_highlight_lines;
90pub use crate::plugin_lang::{
91 LanguageName, LanguageRegistration, LanguageRegistrationError, PluginLanguages,
92 PluginLanguagesHandle,
93};
94// LG.3a: what a runtime-registered language supplies beyond its identity.
95// `register_with_grammar` is the complete registration; the loader's
96// `language` seam (LG.3b) is its only production caller.
97pub use crate::registry::GrammarSpec;
98pub use crate::registry::LangRegistry;
99pub use crate::style::{Style, StyledSpan};
100pub use crate::syntax::{Syntax, SyntaxError, SyntaxSnapshot};
101pub use crate::text_objects::{SyntaxTextObjectIds, register_syntax_text_objects};
102pub use crate::theme_style::{resolve_syntax_style, syntax_element_id};