zalo 0.2.28

A code highlighter giving the same output as VSCode
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
use std::fmt;
use std::sync::LazyLock;

use serde::{Deserialize, Serialize};

use crate::scope::Scope;

/// Regex for tokenizing injection selectors (matches vscode-textmate exactly except for \* added)
static TOKEN_REGEX: LazyLock<onig::Regex> = LazyLock::new(|| {
    onig::Regex::new(r"([LR]:|[\w.:]+[\w\*.:\-]*|[,|\-()])").expect("Invalid selector regex")
});

// Only Left matters, Right is the same as no precedence. We keep both just for debug reasons
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum InjectionPrecedence {
    /// L: prefix
    Left,
    /// R: prefix
    Right,
}

/// A compiled injection selector matcher with priority
#[derive(Clone, PartialEq, Serialize, Deserialize)]
pub struct CompiledInjectionMatcher {
    matcher: SelectorMatcher,
    priority: Option<InjectionPrecedence>,
}

impl CompiledInjectionMatcher {
    #[inline]
    pub fn matches(&self, scope_stack: &[Scope]) -> bool {
        self.matcher.matches(scope_stack)
    }

    /// Returns the precedence (Left/Right) for this injection matcher
    #[inline]
    pub fn precedence(&self) -> InjectionPrecedence {
        self.priority.unwrap_or(InjectionPrecedence::Right)
    }
}

impl fmt::Debug for CompiledInjectionMatcher {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let selector = match self.priority {
            Some(InjectionPrecedence::Left) => format!("L:{}", self.matcher),
            Some(InjectionPrecedence::Right) => format!("R:{}", self.matcher),
            None => self.matcher.to_string(),
        };
        write!(f, "\"{}\"", selector)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SelectorMatcher {
    Scope(Scope),
    /// All matchers must succeed (space-separated)
    And(Vec<SelectorMatcher>),
    /// Any matcher can succeed (OR operation `|` or `,` separated)
    Or(Vec<SelectorMatcher>),
    /// Matcher must NOT succeed (NOT operation `-` prefix)
    Not(Box<SelectorMatcher>),
}

impl SelectorMatcher {
    /// Recursively evaluates this matcher against scope stack
    fn matches(&self, scope_stack: &[Scope]) -> bool {
        match self {
            SelectorMatcher::Scope(scope) => {
                // Single scope: check if ANY scope in stack is a match
                scope_stack
                    .iter()
                    .any(|&stack_scope| scope.is_prefix_of(stack_scope))
            }
            SelectorMatcher::And(matchers) => {
                // Sequential matching: each matcher must find a match at or after the previous match
                let mut start_index = 0;
                for matcher in matchers {
                    match matcher {
                        SelectorMatcher::Scope(scope) => {
                            // For individual scopes, check sequentially
                            let mut found = false;
                            for (i, scope_item) in scope_stack.iter().enumerate().skip(start_index)
                            {
                                if scope.is_prefix_of(*scope_item) {
                                    start_index = i + 1;
                                    found = true;
                                    break;
                                }
                            }
                            if !found {
                                return false;
                            }
                        }
                        _ => {
                            // For compound matchers (OR, NOT), check against entire scope stack
                            if !matcher.matches(scope_stack) {
                                return false;
                            }
                            // For compound matchers, we don't advance position since they're global
                        }
                    }
                }
                true
            }
            SelectorMatcher::Or(matchers) => {
                // Any matcher can succeed
                matchers.iter().any(|m| m.matches(scope_stack))
            }
            SelectorMatcher::Not(matcher) => {
                // Matcher must NOT succeed
                !matcher.matches(scope_stack)
            }
        }
    }
}

impl fmt::Display for SelectorMatcher {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SelectorMatcher::Scope(scope) => write!(f, "{}", scope),
            SelectorMatcher::And(matchers) => {
                let parts: Vec<String> = matchers.iter().map(|m| m.to_string()).collect();
                write!(f, "{}", parts.join(" "))
            }
            SelectorMatcher::Or(matchers) => {
                if matchers.len() == 1 {
                    // If there's only one item in Or, don't wrap in parentheses
                    write!(f, "{}", matchers[0])
                } else {
                    let parts: Vec<String> = matchers.iter().map(|m| m.to_string()).collect();
                    write!(f, "({})", parts.join(" | "))
                }
            }
            SelectorMatcher::Not(matcher) => {
                write!(f, "-{}", matcher)
            }
        }
    }
}

#[inline]
fn is_identifier(s: &str) -> bool {
    if s.is_empty() || s == "-" {
        return false;
    }

    s.chars().all(|c| {
        c.is_ascii_alphanumeric() || c == '_' || c == '.' || c == ':' || c == '-' || c == '*'
    })
}

fn parse_inner_expression(tokens: &[&str], position: &mut usize) -> SelectorMatcher {
    let mut out = Vec::new();
    while let Some(m) = parse_conjunction(tokens, position) {
        out.push(m);
        if *position < tokens.len() && matches!(tokens[*position], "|" | ",") {
            *position += 1;
        } else {
            break;
        }
    }

    let mut deduplicated = Vec::new();
    for matcher in out {
        if !deduplicated.contains(&matcher) {
            deduplicated.push(matcher);
        }
    }

    if deduplicated.len() == 1 {
        deduplicated.pop().unwrap()
    } else {
        SelectorMatcher::Or(deduplicated)
    }
}

fn parse_operand(tokens: &[&str], position: &mut usize) -> Option<SelectorMatcher> {
    if *position >= tokens.len() {
        return None;
    }

    match tokens[*position] {
        "-" => {
            *position += 1;
            let negated = parse_operand(tokens, position)?;
            Some(SelectorMatcher::Not(Box::new(negated)))
        }
        "(" => {
            *position += 1;
            let inner = parse_inner_expression(tokens, position);
            if *position < tokens.len() && tokens[*position] == ")" {
                *position += 1;
            }
            Some(inner)
        }
        _ => {
            let mut scopes = vec![];

            while *position < tokens.len() && is_identifier(tokens[*position]) {
                let token = tokens[*position];
                let scope = if let Some(pos) = token.find(".*") {
                    let base = &token[..pos];
                    Scope::new(base.trim_end_matches("."))[0]
                } else {
                    Scope::new(token)[0]
                };

                if !scopes.contains(&scope) {
                    scopes.push(scope);
                }
                *position += 1;
            }

            match scopes.len() {
                0 => None,
                1 => Some(SelectorMatcher::Scope(scopes.pop().unwrap())),
                _ => Some(SelectorMatcher::And(
                    scopes.into_iter().map(SelectorMatcher::Scope).collect(),
                )),
            }
        }
    }
}

fn parse_conjunction(tokens: &[&str], position: &mut usize) -> Option<SelectorMatcher> {
    let mut matchers = Vec::new();

    while let Some(m) = parse_operand(tokens, position) {
        matchers.push(m);
    }

    match matchers.len() {
        0 => None,
        1 => matchers.into_iter().next(),
        _ => Some(SelectorMatcher::And(matchers)),
    }
}

/// Parse injection selector string into compiled matchers.
/// A selector can correspond to multiple matcher, each with their own optional priority
pub fn parse_injection_selector(selector: &str) -> Vec<CompiledInjectionMatcher> {
    let selector = selector.trim();
    if selector.is_empty() {
        return Vec::new();
    }

    let tokens: Vec<_> = TOKEN_REGEX
        .find_iter(selector)
        .map(|(start, end)| &selector[start..end])
        .filter(|s| !s.is_empty())
        .collect();

    let mut position = 0;
    let mut res = Vec::new();

    let mut priority = None;
    while position < tokens.len() {
        let token = tokens[position];

        match token {
            "L:" => {
                priority = Some(InjectionPrecedence::Left);
                position += 1;
                continue;
            }
            "R:" => {
                priority = Some(InjectionPrecedence::Right);
                position += 1;
                continue;
            }
            _ => (),
        };

        if let Some(matcher) = parse_conjunction(&tokens, &mut position) {
            res.push(CompiledInjectionMatcher { matcher, priority });
            priority = None;
            if position < tokens.len() && tokens[position] == "," {
                position += 1;
            } else {
                break;
            }
        }
    }

    res
}

#[cfg(test)]
mod tests {
    use super::*;
    use insta::{assert_debug_snapshot, with_settings};

    #[test]
    fn test_parse_injection_selector_snapshots() {
        let test_cases = vec![
            // Simple patterns
            "L:text.html.markdown", // mermaid.json
            "L:text.html -comment", // angular-template.json
            "text.html",
            "L:meta.decorator.ts -comment -text.html", // angular-inline-template.json
            // Complex negations
            "L:text.pug -comment -string.comment, L:text.html.derivative -comment.block", // vue-interpolations.json
            "L:meta.tag -meta.attribute -meta.ng-binding -entity.name.tag.pug", // vue-directives.json
            // Parenthesized groups with OR operators
            "L:(meta.script.svelte | meta.style.svelte) (meta.lang.js | meta.lang.javascript) - (meta source)", // svelte.json
            "L:(source.ts, source.js, source.coffee)", // svelte.json
            // Specific contexts
            "L:meta.script.svelte - meta.lang - (meta source)", // svelte.json
            "L:(meta.script.astro) (meta.lang.json) - (meta source)", // astro.json
            "text.html.php.blade - (meta.embedded | meta.tag | comment.block.blade), L:(text.html.php.blade meta.tag - (comment.block.blade | meta.embedded.block.blade)), L:(source.js.embedded.html - (comment.block.blade | meta.embedded.block.blade))", // blade.json
            "R:text.html - (comment.block, text.html meta.embedded, meta.tag.*.*.html, meta.tag.*.*.*.html, meta.tag.*.*.*.*.html)",
            "L:source.css -comment, L:source.postcss -comment, L:source.sass -comment, L:source.stylus -comment",
            // es-tag-css.json
            "L:source.js -comment -string, L:source.js -comment -string, L:source.jsx -comment -string,  L:source.js.jsx -comment -string, L:source.ts -comment -string, L:source.tsx -comment -string, L:source.rescript -comment -string, L:source.vue -comment -string, L:source.svelte -comment -string, L:source.php -comment -string, L:source.rescript -comment -string",
        ];

        for (i, test_case) in test_cases.into_iter().enumerate() {
            let result = parse_injection_selector(test_case);
            with_settings!({description => test_case}, {
                assert_debug_snapshot!(format!("injection_{i}"), result);
            })
        }
    }

    #[test]
    fn can_match_scopes() {
        // (selector, scope_names, expected)
        let test_cases = vec![
            // === Simple Scope Matching ===
            ("text.html", vec!["text.html"], true),
            ("text.html", vec!["text.html.markdown"], true), // prefix match
            ("text.html", vec!["source.js"], false),
            ("text.html", vec!["source.js", "text.html"], true), // found in stack
            ("comment", vec!["comment.line.double-slash"], true), // prefix match
            // === Sequential Scope Matching (AND) ===
            ("text.html meta.tag", vec!["text.html", "meta.tag"], true),
            (
                "text.html meta.tag",
                vec!["text.html", "meta.function", "meta.tag"],
                true,
            ), // with intermediate
            ("text.html meta.tag", vec!["text.html"], false), // missing meta.tag
            ("text.html meta.tag", vec!["meta.tag"], false),  // missing text.html
            ("text.html meta.tag", vec!["meta.tag", "text.html"], false), // wrong order
            (
                "source.js comment",
                vec!["source.js", "meta.function", "comment.line"],
                true,
            ),
            // === NOT Operations ===
            ("text.html -comment", vec!["text.html"], true),
            (
                "text.html -comment",
                vec!["text.html", "comment.block"],
                false,
            ),
            ("text.html -comment", vec!["source.js"], false), // no text.html
            ("comment -comment.block", vec!["comment.line"], true),
            ("comment -comment.block", vec!["comment.block"], false),
            // === Parenthesized Groups ===
            ("(meta.script | meta.style)", vec!["meta.script"], true),
            ("(meta.script | meta.style)", vec!["meta.style"], true),
            ("(meta.script | meta.style)", vec!["meta.tag"], false),
            (
                "(source.js | source.ts) comment",
                vec!["source.js", "comment.line"],
                true,
            ),
            (
                "(source.js | source.ts) comment",
                vec!["source.py", "comment.line"],
                false,
            ),
            // === Complex Boolean Logic ===
            (
                "L:(meta.script.svelte | meta.style.svelte) (meta.lang.js | meta.lang.javascript) - (meta source)",
                vec!["meta.script.svelte", "meta.lang.js"],
                true,
            ),
            (
                "L:(meta.script.svelte | meta.style.svelte) (meta.lang.js | meta.lang.javascript) - (meta source)",
                vec!["meta.style.svelte", "meta.lang.javascript"],
                true,
            ),
            (
                "L:(meta.script.svelte | meta.style.svelte) (meta.lang.js | meta.lang.javascript) - (meta source)",
                vec![
                    "meta.script.svelte",
                    "meta.lang.js",
                    "meta.embedded",
                    "source.js",
                ],
                false, // has "source" which contains "meta source"
            ),
            (
                "L:(meta.script.svelte | meta.style.svelte) (meta.lang.js | meta.lang.javascript) - (meta source)",
                vec!["meta.tag", "meta.lang.js"],
                false, // missing script.svelte or style.svelte
            ),
            // === Precedence Prefixes ===
            ("L:text.html", vec!["text.html"], true),
            ("R:text.html", vec!["text.html"], true),
            ("L:source.js -comment", vec!["source.js"], true),
            (
                "R:source.js -comment",
                vec!["source.js", "comment.block"],
                false,
            ),
            // === Real-world Examples from Snapshot Tests ===
            ("L:text.html.markdown", vec!["text.html.markdown"], true),
            ("L:text.html -comment", vec!["text.html"], true),
            (
                "L:text.html -comment",
                vec!["text.html", "comment.line"],
                false,
            ),
            (
                "L:meta.decorator.ts -comment -text.html",
                vec!["meta.decorator.ts"],
                true,
            ),
            (
                "L:meta.decorator.ts -comment -text.html",
                vec!["meta.decorator.ts", "comment.block"],
                false,
            ),
            (
                "L:meta.decorator.ts -comment -text.html",
                vec!["meta.decorator.ts", "text.html"],
                false,
            ),
            // === Edge Cases ===
            ("text.html", vec![], false), // empty scope stack
        ];

        for (selector_str, scope_names, expected) in test_cases {
            let matchers = parse_injection_selector(selector_str);
            let scope_stack: Vec<Scope> =
                scope_names.iter().map(|name| Scope::new(name)[0]).collect();

            println!("{selector_str} {matchers:?}");

            let result = matchers.iter().any(|x| x.matches(&scope_stack));

            assert_eq!(
                result, expected,
                "Selector '{}' with scopes {:?}: expected {}, got {}",
                selector_str, scope_names, expected, result
            );
        }
    }
}