highlight: Bound capture retokenization

685d97735a4ff7ca3843e42b08b138b3f135d3e5
Use the captured substring for nested end-pattern matching so capture
offsets cannot extend past the retokenized region.

Add coverage that proves nested matching runs without assigning scopes
to a delimiter outside the capture.

Assisted-by: Codex:gpt-5.6-sol
Alexis Sellier committed ago 1 parent 59aef797
crates/highlight/src/grammar/pattern.rs +2 -2
436 436
    start: usize,
437 437
    end: usize,
438 438
    context: &mut PatternSearch<'_, '_, '_>,
439 439
) {
440 440
    let sets = context.sets;
441 -
    let line = context.line;
442 -
    let mut search = PatternSearch::new(sets, &line[..end], end, context.cache, context.anchors);
441 +
    let line = &context.line[..end];
442 +
    let mut search = PatternSearch::new(sets, line, end, context.cache, context.anchors);
443 443
    let emit_open_content =
444 444
        |spans: &mut Vec<ScopeSpan>, stack: &[OpenRule], from: usize, to: usize| {
445 445
            for open in stack.iter().rev() {
446 446
                if let Some(pattern) = pattern_by_rule(sets, open.rule_id) {
447 447
                    pattern.push_content_visible(spans, from, to);
crates/highlight/src/tests.rs +40 -0
889 889
890 890
    assert!(scopes.contains(&"meta.tag.content.test"));
891 891
    assert!(scopes.contains(&"entity.name.tag.test"));
892 892
}
893 893
894 +
#[test]
895 +
fn capture_retokenization_stays_inside_captured_text() {
896 +
    let grammar = Grammar::from_str(
897 +
        r#"{
898 +
            "name":"Test",
899 +
            "scopeName":"source.test",
900 +
            "patterns":[
901 +
                {
902 +
                    "match":"tag\\((\\[body)\\]",
903 +
                    "captures":{
904 +
                        "1":{
905 +
                            "patterns":[
906 +
                                {
907 +
                                    "name":"meta.brackets.test",
908 +
                                    "begin":"\\[",
909 +
                                    "end":"\\]",
910 +
                                    "endCaptures":{
911 +
                                        "0":{
912 +
                                            "patterns":[{"match":"\\]","name":"punctuation.test"}]
913 +
                                        }
914 +
                                    }
915 +
                                }
916 +
                            ]
917 +
                        }
918 +
                    }
919 +
                }
920 +
            ],
921 +
            "repository":{}
922 +
        }"#,
923 +
    )
924 +
    .unwrap();
925 +
    let mut state = LineState::default();
926 +
927 +
    let spans = grammar.tokenize_line(&mut state, "tag([body]");
928 +
    let scopes = scope_names(&grammar, &spans);
929 +
930 +
    assert!(scopes.contains(&"meta.brackets.test"));
931 +
    assert!(!scopes.contains(&"punctuation.test"));
932 +
}
933 +
894 934
#[test]
895 935
fn theme_matches_space_separated_scope_stacks() {
896 936
    let theme = r##"{
897 937
        "name":"Stack Theme",
898 938
        "settings":[