strop-syntax 0.18.0

strop syntax: statically-linked tree-sitter highlighting, curated language registry
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! strop-syntax: tree-sitter highlighting. Parsers statically linked
//! (0002 §2.2 — never dlopen'd grammars); queries are data (0001 §5.11),
//! embedded defaults now, runtime overrides when config lands (0005).
//!
//! The whole pipeline is rope-backed: parsing walks rope chunks, and
//! query predicates (`#eq?`/`#match?`/…) read node text through a
//! [`tree_sitter::TextProvider`] over the same chunks — no full-text
//! `String` is materialized on any input or render path, and language
//! detection never touches the filesystem.

use streaming_iterator::StreamingIterator;
mod guides;
pub mod languages;
pub use guides::{GuideFrame, IndentGuides};
mod injections;
mod spans;
pub use spans::Emphasis;
use spans::{CaptureStyle, LayeredSpan};

use ropey::Rope;
use strop_core::id::BufferRevision;
use tree_sitter::{Parser, Query, QueryCursor, TextProvider};

/// Semantic classes the renderer maps to palette colors. Kept small and
/// stable; the query capture names map onto these.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum Class {
    Keyword,
    Function,
    Type,
    String,
    Comment,
    Number,
    Operator,
    Punctuation,
    Constant,
    Variable,
    Attribute,
    Heading,
    Link,
    Code,
    Quote,
    List,
    Tag,
}

impl Class {
    fn from_capture(name: &str) -> Self {
        if name.starts_with("constant.numeric.") {
            return Class::Number;
        }
        if let Some(markup) = name.strip_prefix("markup.") {
            return match markup.split('.').next() {
                Some("heading") => Class::Heading,
                Some("link") => Class::Link,
                Some("raw") => Class::Code,
                Some("quote") => Class::Quote,
                Some("list") => Class::List,
                _ => Class::Variable,
            };
        }
        let head = name.split('.').next().unwrap_or(name);
        match head {
            "keyword" => Class::Keyword,
            "function" | "constructor" => Class::Function,
            "type" | "namespace" | "label" => Class::Type,
            "string" | "character" => Class::String,
            "comment" => Class::Comment,
            "number" | "float" => Class::Number,
            "operator" => Class::Operator,
            "punctuation" => Class::Punctuation,
            "constant" | "boolean" => Class::Constant,
            "attribute" | "property" => Class::Attribute,
            "tag" => Class::Tag,
            _ => Class::Variable,
        }
    }
}

/// A colored span, in byte offsets.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Span {
    pub start: usize,
    pub end: usize,
    pub class: Class,
    pub emphasis: Emphasis,
}

/// Highlighting failed out loud: no hidden full-text reparse fallback, no
/// swallowed error, no panic — the caller decides what the failure means
/// for its surface.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HighlightError {
    /// tree-sitter refused the chunked input (encoding breach or an
    /// internal limit). Cached spans and the kept tree stay untouched, so
    /// the next call retries from the same state.
    Parse,
    /// Superseded analysis is not a parser failure.
    Cancelled,
    /// Recursive injected language structure exceeded the owned parser bound.
    InjectionDepth,
}

impl std::fmt::Display for HighlightError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            HighlightError::Parse => f.write_str("tree-sitter produced no parse tree"),
            HighlightError::Cancelled => f.write_str("syntax analysis superseded"),
            HighlightError::InjectionDepth => {
                f.write_str("syntax injection nesting exceeds eight levels")
            }
        }
    }
}

impl std::error::Error for HighlightError {}

/// Query text source over the rope itself: each node's text is yielded as
/// the rope's own chunk slices, so a node inside one chunk is compared
/// with zero copy. Only a node straddling a chunk boundary is assembled —
/// by tree-sitter, into its reusable buffers, bounded by node size.
struct RopeText<'a> {
    rope: &'a Rope,
}

impl<'a> TextProvider<&'a [u8]> for RopeText<'a> {
    type I = RopeSlices<'a>;

    fn text(&mut self, node: tree_sitter::Node<'_>) -> Self::I {
        RopeSlices {
            rope: self.rope,
            start: node.start_byte(),
            end: node.end_byte(),
        }
    }
}

/// The chunk slices covering `[start, end)`, in document order. An empty
/// range yields nothing — an empty node's text is empty, exactly as with
/// a plain byte-slice provider.
struct RopeSlices<'a> {
    rope: &'a Rope,
    start: usize,
    end: usize,
}

impl<'a> Iterator for RopeSlices<'a> {
    type Item = &'a [u8];

    fn next(&mut self) -> Option<Self::Item> {
        if self.start >= self.end {
            return None;
        }
        let (chunk, chunk_start, ..) = self.rope.chunk_at_byte(self.start);
        let head = &chunk[self.start - chunk_start..];
        // stop at the chunk's end or the node's end, whichever is first;
        // both are char boundaries (rope chunks and tree-sitter node
        // ranges always are), so the slice is valid UTF-8 as-is
        let take = head.len().min(self.end - self.start);
        let slice = &head.as_bytes()[..take];
        self.start += take;
        Some(slice)
    }
}

/// One language's parser + highlight query. The tree tracks the buffer's
/// journal (0022 §1): edits apply as a cheap pointer walk at commit time
/// and reparsing is incremental against the kept tree — a full parse is
/// the cold-start path, not the rule.
pub struct Highlighter {
    parser: Parser,
    query: Query,
    /// Capture index → class, resolved once at construction.
    classes: Vec<CaptureStyle>,
    source_hash: Option<BufferRevision>,
    spans: Vec<Span>,
    /// The parse tree covering `tree_revision`.
    tree: Option<tree_sitter::Tree>,
    tree_revision: BufferRevision,
    span_window: Option<(usize, usize)>,
    injection_query: Option<Query>,
    injection_depth: usize,
    children: Vec<injections::InjectedHighlighter>,
}

impl Highlighter {
    /// Drop the kept tree (0023: a mutation path that can't produce
    /// exact edit coordinates invalidates rather than lying).
    pub fn invalidate(&mut self) {
        self.tree = None;
        self.source_hash = None;
        self.span_window = None;
        self.children.clear();
    }

    /// Feed the pre-edit journal to the kept tree (0022 §1): a cheap
    /// pointer walk at commit time; the reparse stays lazy. The edits
    /// arrive exactly as `strop-core` published them — tuple points
    /// converted to tree-sitter points once, here — so the editor hands
    /// over `&[change.edit]` with `change.revision` untouched.
    pub fn apply_edits(&mut self, edits: &[strop_core::InputEdit], revision: BufferRevision) {
        if revision == self.tree_revision {
            return;
        }
        if let Some(tree) = &mut self.tree {
            for edit in edits {
                tree.edit(&tree_sitter::InputEdit {
                    start_byte: edit.start_byte,
                    old_end_byte: edit.old_end_byte,
                    new_end_byte: edit.new_end_byte,
                    start_position: tree_sitter::Point {
                        row: edit.start_point.0,
                        column: edit.start_point.1,
                    },
                    old_end_position: tree_sitter::Point {
                        row: edit.old_end_point.0,
                        column: edit.old_end_point.1,
                    },
                    new_end_position: tree_sitter::Point {
                        row: edit.new_end_point.0,
                        column: edit.new_end_point.1,
                    },
                });
            }
        }
        // with no kept tree the next parse builds it — the revision
        // still advances so reparse-once stays the rule, not per frame
        self.tree_revision = revision;
        for child in &mut self.children {
            child.apply_edits(edits, revision);
        }
    }

    /// Pure constructor: the path plus the rope that backs the document.
    /// When basename and extension both miss, the rope's first line —
    /// bounded to 256 bytes, assembled chunk-wise — is the shebang
    /// fallback. Nothing here reads the filesystem: UI dispatch never
    /// blocks on disk.
    pub fn for_path(path: &std::path::Path, rope: &Rope) -> Option<Self> {
        let spec = languages::detect(path, Some(&first_line_bounded(rope)))?;
        Self::from_spec(spec)
    }

    fn from_spec(spec: languages::LanguageSpec) -> Option<Self> {
        let mut parser = Parser::new();
        parser.set_language(&spec.language).ok()?;
        let query = Query::new(&spec.language, spec.highlights).ok()?;
        let injection_query = if spec.injections.is_empty() {
            None
        } else {
            Some(Query::new(&spec.language, spec.injections).ok()?)
        };
        let classes = query
            .capture_names()
            .iter()
            .map(|name| CaptureStyle {
                class: Class::from_capture(name),
                emphasis: Emphasis::from_capture(name),
            })
            .collect();
        Some(Self {
            parser,
            query,
            classes,
            source_hash: None,
            spans: Vec::new(),
            tree: None,
            tree_revision: BufferRevision::new(0),
            span_window: None,
            injection_query,
            injection_depth: 0,
            children: Vec::new(),
        })
    }

    /// Highlight spans intersecting `[first_byte, last_byte)` of the rope.
    /// Reparses only when the text changed. `revision` is the document's
    /// edit counter (0020 §5: the len+first+last key under-invalidated
    /// same-length middle edits deterministically). A parse that cannot
    /// complete is a typed [`HighlightError`] — never a hidden reparse
    /// fallback, never a panic.
    pub fn highlight(
        &mut self,
        rope: &Rope,
        revision: BufferRevision,
        first_byte: usize,
        last_byte: usize,
    ) -> Result<Vec<Span>, HighlightError> {
        self.highlight_while(rope, revision, first_byte, last_byte, || false)
    }

    pub fn highlight_while(
        &mut self,
        rope: &Rope,
        revision: BufferRevision,
        first_byte: usize,
        last_byte: usize,
        cancelled: impl Fn() -> bool,
    ) -> Result<Vec<Span>, HighlightError> {
        self.highlight_cancellable(rope, revision, first_byte, last_byte, &cancelled)
    }

    fn highlight_cancellable(
        &mut self,
        rope: &Rope,
        revision: BufferRevision,
        first_byte: usize,
        last_byte: usize,
        cancelled: &dyn Fn() -> bool,
    ) -> Result<Vec<Span>, HighlightError> {
        if cancelled() {
            return Err(HighlightError::Cancelled);
        }
        if Some(revision) != self.source_hash {
            // A skipped edit journal must never make an unchanged old tree
            // masquerade as the new rope. Exact journals retain incremental parse.
            if self.tree_revision != revision {
                self.tree = None;
            }
            let mut progress = |_: &tree_sitter::ParseState| cancelled();
            let tree = self.parser.parse_with_options(
                &mut |byte: usize, _| {
                    if byte >= rope.len_bytes() {
                        return "";
                    }
                    let (chunk, start, _, _) = rope.chunk_at_byte(byte);
                    &chunk[byte - start..]
                },
                self.tree.as_ref(),
                Some(tree_sitter::ParseOptions::new().progress_callback(&mut progress)),
            );
            let Some(tree) = tree else {
                self.parser.reset();
                return Err(if cancelled() {
                    HighlightError::Cancelled
                } else {
                    HighlightError::Parse
                });
            };
            self.tree = Some(tree);
            self.tree_revision = revision;
            self.source_hash = Some(revision);
            self.span_window = None;
        }
        let window = (
            first_byte.min(rope.len_bytes()),
            last_byte.min(rope.len_bytes()),
        );
        if self.span_window != Some(window) {
            let tree = self.tree.as_ref().ok_or(HighlightError::Parse)?;
            let mut cursor = QueryCursor::new();
            cursor.set_byte_range(window.0..window.1);
            let mut progress = |_: &tree_sitter::QueryCursorState| cancelled();
            let mut captures = Vec::new();
            let mut matches = cursor.matches_with_options(
                &self.query,
                tree.root_node(),
                RopeText { rope },
                tree_sitter::QueryCursorOptions::new().progress_callback(&mut progress),
            );
            while let Some(m) = { StreamingIterator::next(&mut matches) } {
                if cancelled() {
                    return Err(HighlightError::Cancelled);
                }
                for cap in m.captures {
                    let node = cap.node;
                    if node.end_byte() <= window.0 || node.start_byte() >= window.1 {
                        continue;
                    }
                    let style = self.classes[cap.index as usize];
                    captures.push(LayeredSpan {
                        span: Span {
                            start: node.start_byte(),
                            end: node.end_byte(),
                            class: style.class,
                            emphasis: style.emphasis,
                        },
                        injected: false,
                    });
                }
            }
            if cancelled() {
                return Err(HighlightError::Cancelled);
            }
            drop(matches);
            captures.extend(self.injection_spans(rope, revision, window.0, window.1, cancelled)?);
            self.spans = spans::flatten(captures, window.0, window.1);
            self.span_window = Some(window);
        }
        Ok(self.spans.clone())
    }
}

/// Largest char-boundary-aligned length of `head` that is at most `want`.
/// A byte cap can land inside a multibyte character; walking back at most
/// three bytes keeps the slice valid UTF-8.
fn cut_at_boundary(head: &str, want: usize) -> usize {
    let mut take = want.min(head.len());
    while take > 0 && !head.is_char_boundary(take) {
        take -= 1;
    }
    take
}

/// The rope's first line, capped at 256 bytes so a minified no-newline
/// blob can't turn shebang detection into a full-text copy. Assembled
/// chunk-wise, so a first line spanning rope chunks comes out whole
/// (up to the cap).
fn first_line_bounded(rope: &Rope) -> String {
    const CAP: usize = 256;
    // only a shebang can match — skip the copy for the common case
    if rope.len_bytes() == 0 || rope.byte(0) != b'#' {
        return String::new();
    }
    let limit = rope.len_bytes().min(CAP);
    let mut line = String::new();
    let mut byte = 0;
    while byte < limit {
        let (chunk, start, ..) = rope.chunk_at_byte(byte);
        let head = &chunk[byte - start..];
        let stop = head.find('\n').unwrap_or(head.len());
        let take = cut_at_boundary(head, stop.min(limit - byte));
        if take == 0 {
            break; // the cap cut inside a multibyte char
        }
        line.push_str(&head[..take]);
        if take == stop {
            break; // consumed through the newline (or the whole chunk had none)
        }
        byte += take;
    }
    line
}

#[cfg(test)]
mod tests;