toon-lsp 0.7.21

Language Server Protocol implementation for TOON (Token-Oriented Object Notation)
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2024-2025 w0wl0lxd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Semantic token types and modifiers for TOON language server.
//!
//! This module defines the token types and modifiers used for semantic
//! highlighting in the LSP. These map to the semantic token legend declared
//! in the server capabilities.
//!
//! # Token Types
//! - Property: Object keys
//! - String: String values
//! - Number: Numeric values
//! - Keyword: true, false, null
//! - Operator: colons, brackets
//!
//! # Token Modifiers
//! - DEFINITION: First occurrence of a key
//! - READONLY: Immutable literals

use crate::ast::Span;

/// Token type for semantic highlighting.
///
/// Each variant corresponds to an index in the semantic token legend
/// declared in server capabilities. The order and indices MUST match
/// exactly with the legend array.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum ToonTokenType {
    /// Object keys
    Property = 0,
    /// String values
    String = 1,
    /// Numeric values
    Number = 2,
    /// Boolean and null keywords (true, false, null)
    Keyword = 3,
    /// Operators (colons, brackets)
    Operator = 4,
    /// References / environment substitutions (`${path}`, `${env:VAR}`)
    Reference = 5,
}

impl ToonTokenType {
    /// Convert token type to u32 index for LSP protocol.
    #[must_use]
    pub const fn as_u32(self) -> u32 {
        self as u32
    }
}

/// Convert `ToonTokenType` to `u32` for LSP protocol.
///
/// This conversion is used when encoding tokens for transmission
/// to the LSP client. The u32 value corresponds to an index in the
/// semantic token legend declared in server capabilities.
impl From<ToonTokenType> for u32 {
    fn from(token_type: ToonTokenType) -> Self {
        token_type.as_u32()
    }
}

bitflags::bitflags! {
    /// Token modifiers for semantic highlighting.
    ///
    /// Modifiers can be combined using bitwise operations to indicate
    /// multiple properties of a token.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct ToonTokenModifier: u32 {
        /// First occurrence of a key (definition site)
        const DEFINITION = 1 << 0;
        /// Immutable literal value
        const READONLY = 1 << 1;
    }
}

impl ToonTokenModifier {
    /// Convert modifiers to u32 bitmask for LSP protocol.
    #[must_use]
    pub const fn as_u32(self) -> u32 {
        self.bits()
    }
}

/// A semantic token with position and classification.
///
/// Used to provide syntax highlighting information to the LSP client.
/// Positions are 0-based line/column in UTF-8 encoding.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SemanticToken {
    /// 0-based line number
    pub line: u32,
    /// 0-based UTF-8 column
    pub start_col: u32,
    /// Token length in UTF-8 bytes
    pub length: u32,
    /// Token classification
    pub token_type: ToonTokenType,
    /// Token modifiers (combined bitflags)
    pub modifiers: ToonTokenModifier,
}

impl SemanticToken {
    /// Create a new semantic token.
    #[must_use]
    pub const fn new(
        line: u32,
        start_col: u32,
        length: u32,
        token_type: ToonTokenType,
        modifiers: ToonTokenModifier,
    ) -> Self {
        Self { line, start_col, length, token_type, modifiers }
    }

    /// Create a semantic token from a span.
    #[must_use]
    pub fn from_span(span: &Span, token_type: ToonTokenType, modifiers: ToonTokenModifier) -> Self {
        let length = span.end.offset - span.start.offset;
        Self { line: span.start.line, start_col: span.start.column, length, token_type, modifiers }
    }

    /// Check if this token has a specific modifier.
    #[must_use]
    pub const fn has_modifier(&self, modifier: ToonTokenModifier) -> bool {
        self.modifiers.contains(modifier)
    }
}

/// Collect semantic tokens from an AST.
///
/// Traverses the AST and generates semantic tokens for all syntax elements
/// including object keys, values, and literals. Tokens contain position
/// information and classification for syntax highlighting.
///
/// # Arguments
/// * `ast` - The root AST node to traverse
/// * `text` - The source text (used for position calculations)
///
/// # Returns
/// Vector of semantic tokens in document order (top-to-bottom, left-to-right)
#[must_use]
pub fn collect_semantic_tokens(ast: &crate::ast::AstNode) -> Vec<SemanticToken> {
    let mut tokens = Vec::new();
    visit_node(ast, &mut tokens);
    tokens
}

/// Recursively visit AST nodes and collect semantic tokens.
///
/// Traverses the AST in depth-first order, generating tokens for all
/// syntax elements. Each node type contributes different token types:
/// - Object entries: Property tokens for keys with DEFINITION modifier
/// - String/Number/Bool/Null: Literal tokens with READONLY modifier
/// - Arrays and Documents: Recursively process children
///
/// # Arguments
///
/// * `node` - The AST node to visit
/// * `tokens` - Accumulator vector for collected tokens
///
fn visit_node(node: &crate::ast::AstNode, tokens: &mut Vec<SemanticToken>) {
    use crate::ast::AstNode;

    match node {
        AstNode::Document { children, .. } => {
            for child in children {
                visit_node(child, tokens);
            }
        }
        AstNode::Object { entries, .. } => {
            for entry in entries {
                // Add Property token for key with DEFINITION modifier
                tokens.push(SemanticToken::from_span(
                    &entry.key_span,
                    ToonTokenType::Property,
                    ToonTokenModifier::DEFINITION,
                ));
                // Visit value
                visit_node(&entry.value, tokens);
            }
        }
        AstNode::Array { items, .. } => {
            for item in items {
                visit_node(item, tokens);
            }
        }
        AstNode::String { span, .. } => {
            tokens.push(SemanticToken::from_span(
                span,
                ToonTokenType::String,
                ToonTokenModifier::READONLY,
            ));
        }
        AstNode::Number { span, .. } => {
            tokens.push(SemanticToken::from_span(
                span,
                ToonTokenType::Number,
                ToonTokenModifier::READONLY,
            ));
        }
        AstNode::Bool { span, .. } => {
            tokens.push(SemanticToken::from_span(
                span,
                ToonTokenType::Keyword,
                ToonTokenModifier::READONLY,
            ));
        }
        AstNode::Null { span } => {
            tokens.push(SemanticToken::from_span(
                span,
                ToonTokenType::Keyword,
                ToonTokenModifier::READONLY,
            ));
        }
        AstNode::Reference { span, .. } => {
            tokens.push(SemanticToken::from_span(
                span,
                ToonTokenType::Reference,
                ToonTokenModifier::READONLY,
            ));
        }
    }
}

/// Encode semantic tokens into LSP relative format.
///
/// Converts absolute position tokens into the LSP delta-encoded format.
/// Each token is represented as a tower_lsp::lsp_types::SemanticToken with:
/// - delta_line: Line offset from previous token (or 0 for first token)
/// - delta_start: If same line, offset from previous token's start; if different line, absolute column
/// - length: Token length in UTF-16 code units
/// - token_type: Index into SemanticTokensLegend.tokenTypes
/// - token_modifiers_bitset: Bitmask of modifier indices
///
/// The encoding uses relative positions for efficiency:
/// - First token: deltaLine and deltaStartChar from document start
/// - Same line tokens: deltaLine=0, deltaStartChar=offset from previous token
/// - New line tokens: deltaLine=line difference, deltaStartChar=absolute column
///
/// # Arguments
/// * `tokens` - Semantic tokens with absolute positions
/// * `text` - The source text (used for UTF-16 conversion)
///
/// # Returns
/// Vector of LSP SemanticToken structs in delta-encoded format
#[must_use]
pub fn encode_tokens(
    tokens: &[SemanticToken],
    text: &str,
) -> Vec<tower_lsp::lsp_types::SemanticToken> {
    if tokens.is_empty() {
        return Vec::new();
    }

    let mut result = Vec::with_capacity(tokens.len());
    let mut prev_line = 0u32;
    let mut prev_col = 0u32;

    // Split text into lines for UTF-16 conversion
    let lines: Vec<&str> = text.lines().collect();

    // LSP delta encoding requires ascending (line, column) order; error recovery
    // can emit spans out of order, so sort defensively.
    let mut ordered: Vec<&SemanticToken> = tokens.iter().collect();
    ordered.sort_by(|a, b| a.line.cmp(&b.line).then(a.start_col.cmp(&b.start_col)));

    for token in ordered {
        // saturating_sub: degrade to 0 rather than panic on any residual disorder.
        let delta_line = token.line.saturating_sub(prev_line);

        // Convert UTF-8 positions to UTF-16 for LSP compliance
        let line_idx = token.line as usize;
        let line_text = if line_idx < lines.len() { lines[line_idx] } else { "" };

        let utf16_col = super::utf16::utf8_to_utf16_col(line_text, token.start_col);
        let utf16_length =
            super::utf16::utf8_to_utf16_col(line_text, token.start_col + token.length)
                .saturating_sub(utf16_col);

        let utf16_delta_col = if delta_line == 0 {
            // Same line: compute delta in UTF-16 space
            let prev_utf16_col = super::utf16::utf8_to_utf16_col(line_text, prev_col);
            utf16_col.saturating_sub(prev_utf16_col)
        } else {
            // New line: use absolute UTF-16 column
            utf16_col
        };

        result.push(tower_lsp::lsp_types::SemanticToken {
            delta_line,
            delta_start: utf16_delta_col,
            length: utf16_length,
            token_type: token.token_type.as_u32(),
            token_modifiers_bitset: token.modifiers.bits(),
        });

        prev_line = token.line;
        prev_col = token.start_col;
    }

    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ast::Position;

    #[test]
    fn test_token_type_indices() {
        // Verify token type indices match LSP legend order
        assert_eq!(ToonTokenType::Property.as_u32(), 0);
        assert_eq!(ToonTokenType::String.as_u32(), 1);
        assert_eq!(ToonTokenType::Number.as_u32(), 2);
        assert_eq!(ToonTokenType::Keyword.as_u32(), 3);
        assert_eq!(ToonTokenType::Operator.as_u32(), 4);
    }

    #[test]
    fn test_token_type_conversion() {
        // Test From<ToonTokenType> for u32
        let property: u32 = ToonTokenType::Property.into();
        assert_eq!(property, 0);

        let keyword: u32 = ToonTokenType::Keyword.into();
        assert_eq!(keyword, 3);
    }

    #[test]
    fn test_token_modifier_definition() {
        // Test DEFINITION modifier
        let def = ToonTokenModifier::DEFINITION;
        assert_eq!(def.bits(), 1);
        assert_eq!(def.as_u32(), 1);
    }

    #[test]
    fn test_token_modifier_readonly() {
        // Test READONLY modifier
        let readonly = ToonTokenModifier::READONLY;
        assert_eq!(readonly.bits(), 2);
        assert_eq!(readonly.as_u32(), 2);
    }

    #[test]
    fn test_token_modifier_combined() {
        // Test combining modifiers with bitwise OR
        let combined = ToonTokenModifier::DEFINITION | ToonTokenModifier::READONLY;
        assert_eq!(combined.bits(), 3);
        assert_eq!(combined.as_u32(), 3);

        // Test contains
        assert!(combined.contains(ToonTokenModifier::DEFINITION));
        assert!(combined.contains(ToonTokenModifier::READONLY));
    }

    #[test]
    fn test_token_modifier_empty() {
        // Test empty modifiers
        let empty = ToonTokenModifier::empty();
        assert_eq!(empty.bits(), 0);
        assert_eq!(empty.as_u32(), 0);
        assert!(!empty.contains(ToonTokenModifier::DEFINITION));
    }

    #[test]
    fn test_semantic_token_new() {
        // Test creating a semantic token
        let token =
            SemanticToken::new(5, 10, 4, ToonTokenType::Property, ToonTokenModifier::DEFINITION);

        assert_eq!(token.line, 5);
        assert_eq!(token.start_col, 10);
        assert_eq!(token.length, 4);
        assert_eq!(token.token_type, ToonTokenType::Property);
        assert_eq!(token.modifiers, ToonTokenModifier::DEFINITION);
    }

    #[test]
    fn test_semantic_token_from_span() {
        // Test creating semantic token from span
        let start = Position::new(2, 5, 25);
        let end = Position::new(2, 9, 29);
        let span = Span::new(start, end);

        let token =
            SemanticToken::from_span(&span, ToonTokenType::String, ToonTokenModifier::READONLY);

        assert_eq!(token.line, 2);
        assert_eq!(token.start_col, 5);
        assert_eq!(token.length, 4); // 29 - 25
        assert_eq!(token.token_type, ToonTokenType::String);
        assert_eq!(token.modifiers, ToonTokenModifier::READONLY);
    }

    #[test]
    fn test_semantic_token_has_modifier() {
        // Test modifier checking
        let token = SemanticToken::new(
            0,
            0,
            5,
            ToonTokenType::Property,
            ToonTokenModifier::DEFINITION | ToonTokenModifier::READONLY,
        );

        assert!(token.has_modifier(ToonTokenModifier::DEFINITION));
        assert!(token.has_modifier(ToonTokenModifier::READONLY));
    }

    #[test]
    fn test_semantic_token_no_modifier() {
        // Test token without specific modifier
        let token = SemanticToken::new(0, 0, 5, ToonTokenType::Number, ToonTokenModifier::empty());

        assert!(!token.has_modifier(ToonTokenModifier::DEFINITION));
        assert!(!token.has_modifier(ToonTokenModifier::READONLY));
    }

    // ===================================================================

    // ===================================================================

    use crate::parser::parse_with_errors;

    // Test semantic tokens for object keys
    ///
    /// Validates that object keys are correctly identified as Property tokens.
    /// Should detect all keys at the top level of an object.
    #[test]
    fn test_semantic_tokens_object_keys() {
        let source = "name: Alice\nage: 30";
        let (ast, _) = parse_with_errors(source);
        let ast = ast.expect("should parse valid TOON");

        let tokens = collect_semantic_tokens(&ast);

        // Should find 2 Property tokens: "name" and "age"
        let properties: Vec<_> =
            tokens.iter().filter(|t| t.token_type == ToonTokenType::Property).collect();

        assert_eq!(
            properties.len(),
            2,
            "Expected 2 property tokens (name, age), found {}",
            properties.len()
        );

        // Verify first property token
        assert_eq!(properties[0].line, 0, "name should be on line 0");
        assert_eq!(properties[0].start_col, 0, "name should start at column 0");
        assert_eq!(properties[0].length, 4, "name is 4 characters");
    }

    // Test semantic tokens for string values
    ///
    /// Validates that string literals are correctly classified as String tokens
    /// with READONLY modifier (immutable literals).
    #[test]
    fn test_semantic_tokens_string_values() {
        let source = "name: Alice\nmessage: Hello World";
        let (ast, _) = parse_with_errors(source);
        let ast = ast.expect("should parse valid TOON");

        let tokens = collect_semantic_tokens(&ast);

        // Should find 2 String tokens: "Alice" and "Hello World"
        let strings: Vec<_> =
            tokens.iter().filter(|t| t.token_type == ToonTokenType::String).collect();

        assert_eq!(
            strings.len(),
            2,
            "Expected 2 string tokens (Alice, Hello World), found {}",
            strings.len()
        );

        // Verify first string
        assert_eq!(strings[0].line, 0, "Alice should be on line 0");
        assert_eq!(strings[0].length, 5, "Alice is 5 characters");
        assert!(
            strings[0].has_modifier(ToonTokenModifier::READONLY),
            "String literals should be marked READONLY"
        );

        // Verify second string
        assert_eq!(strings[1].line, 1, "Hello World should be on line 1");
        assert_eq!(strings[1].length, 11, "Hello World is 11 characters");
        assert!(
            strings[1].has_modifier(ToonTokenModifier::READONLY),
            "String literals should be marked READONLY"
        );
    }

    // Test semantic tokens for number values
    ///
    /// Validates that numeric literals (integer, float, negative) are correctly
    /// classified as Number tokens with READONLY modifier.
    #[test]
    fn test_semantic_tokens_number_values() {
        let source = "count: 42\nprice: 19.99\nnegative: -5";
        let (ast, _) = parse_with_errors(source);
        let ast = ast.expect("should parse valid TOON");

        let tokens = collect_semantic_tokens(&ast);

        // Should find 3 Number tokens: 42, 19.99, -5
        let numbers: Vec<_> =
            tokens.iter().filter(|t| t.token_type == ToonTokenType::Number).collect();

        assert_eq!(
            numbers.len(),
            3,
            "Expected 3 number tokens (42, 19.99, -5), found {}",
            numbers.len()
        );

        // Verify integer
        assert_eq!(numbers[0].line, 0, "42 should be on line 0");
        assert_eq!(numbers[0].length, 2, "42 is 2 characters");
        assert!(
            numbers[0].has_modifier(ToonTokenModifier::READONLY),
            "Number literals should be marked READONLY"
        );

        // Verify float
        assert_eq!(numbers[1].line, 1, "19.99 should be on line 1");
        assert_eq!(numbers[1].length, 5, "19.99 is 5 characters");

        // Verify negative
        assert_eq!(numbers[2].line, 2, "-5 should be on line 2");
        assert_eq!(numbers[2].length, 2, "-5 is 2 characters");
    }

    // Test semantic tokens for boolean and null keywords
    ///
    /// Validates that true, false, and null are correctly classified as
    /// Keyword tokens with READONLY modifier.
    #[test]
    fn test_semantic_tokens_boolean_null() {
        let source = "active: true\ndeleted: false\ndata: null";
        let (ast, _) = parse_with_errors(source);
        let ast = ast.expect("should parse valid TOON");

        let tokens = collect_semantic_tokens(&ast);

        // Should find 3 Keyword tokens: true, false, null
        let keywords: Vec<_> =
            tokens.iter().filter(|t| t.token_type == ToonTokenType::Keyword).collect();

        assert_eq!(
            keywords.len(),
            3,
            "Expected 3 keyword tokens (true, false, null), found {}",
            keywords.len()
        );

        // Verify true keyword
        assert_eq!(keywords[0].line, 0, "true should be on line 0");
        assert_eq!(keywords[0].length, 4, "true is 4 characters");
        assert!(
            keywords[0].has_modifier(ToonTokenModifier::READONLY),
            "Keywords should be marked READONLY"
        );

        // Verify false keyword
        assert_eq!(keywords[1].line, 1, "false should be on line 1");
        assert_eq!(keywords[1].length, 5, "false is 5 characters");
        assert!(
            keywords[1].has_modifier(ToonTokenModifier::READONLY),
            "Keywords should be marked READONLY"
        );

        // Verify null keyword
        assert_eq!(keywords[2].line, 2, "null should be on line 2");
        assert_eq!(keywords[2].length, 4, "null is 4 characters");
        assert!(
            keywords[2].has_modifier(ToonTokenModifier::READONLY),
            "Keywords should be marked READONLY"
        );
    }

    // Test semantic tokens for nested objects and arrays
    ///
    /// Validates that tokens are correctly collected from nested structures,
    /// including keys at different indentation levels and values within arrays.
    #[test]
    fn test_semantic_tokens_nested() {
        let source = "user:\n  name: Bob\n  tags:\n    - admin\n    - user";
        let (ast, _) = parse_with_errors(source);
        let ast = ast.expect("should parse valid TOON");

        let tokens = collect_semantic_tokens(&ast);

        // Should find 3 property tokens: "user", "name", "tags"
        let properties: Vec<_> =
            tokens.iter().filter(|t| t.token_type == ToonTokenType::Property).collect();

        assert_eq!(
            properties.len(),
            3,
            "Expected 3 property tokens (user, name, tags), found {}",
            properties.len()
        );

        // Should find 3 string tokens: "Bob", "admin", "user"
        let strings: Vec<_> =
            tokens.iter().filter(|t| t.token_type == ToonTokenType::String).collect();

        assert_eq!(
            strings.len(),
            3,
            "Expected 3 string tokens (Bob, admin, user), found {}",
            strings.len()
        );

        // Verify nested property positions
        assert_eq!(properties[0].line, 0, "user should be on line 0");
        assert_eq!(properties[1].line, 1, "name should be on line 1");
        assert_eq!(properties[1].start_col, 2, "name should be indented 2 spaces");
        assert_eq!(properties[2].line, 2, "tags should be on line 2");
        assert_eq!(properties[2].start_col, 2, "tags should be indented 2 spaces");
    }

    // Test encode_tokens produces correct relative positions
    ///
    /// Validates the LSP delta-encoding algorithm:
    /// - delta_line, delta_start, length, token_type, token_modifiers_bitset
    /// - Same line: delta_line=0, delta_start=offset from previous
    /// - New line: delta_line=difference, delta_start=absolute (reset)
    #[test]
    fn test_encode_tokens_relative_positions() {
        let source = "foo: bar\nbaz: 123";
        let (ast, _) = parse_with_errors(source);
        let ast = ast.expect("should parse valid TOON");

        let tokens = collect_semantic_tokens(&ast);
        let encoded = encode_tokens(&tokens, source);

        // Should have at least 4 tokens: "foo", "bar", "baz", "123"
        assert!(encoded.len() >= 4, "Should have at least 4 tokens (foo, bar, baz, 123)");

        // First token: "foo" at line 0, col 0
        assert_eq!(encoded[0].delta_line, 0, "First token delta_line should be 0");
        assert_eq!(encoded[0].delta_start, 0, "First token delta_start should be 0");

        // Second token on same line: "bar" at col 5
        if encoded.len() >= 2 {
            assert_eq!(encoded[1].delta_line, 0, "Same line token delta_line should be 0");
            assert!(
                encoded[1].delta_start > 0,
                "Same line token delta_start should be offset from previous"
            );
        }

        // Third token on new line: "baz" at line 1, col 0
        if encoded.len() >= 3 {
            assert_eq!(encoded[2].delta_line, 1, "New line token delta_line should be 1");
            assert_eq!(
                encoded[2].delta_start, 0,
                "New line token delta_start resets to absolute position"
            );
        }
    }

    // Test semantic tokens handles documents with parse errors
    ///
    /// Validates graceful degradation - even with parse errors, should
    /// return tokens for successfully parsed portions of the document.
    /// Uses unclosed bracket to force parse error.
    #[test]
    fn test_semantic_tokens_with_parse_errors() {
        // Use unclosed array bracket to force parse error
        let source = "valid: ok\narray: [\nmore: stuff";
        let (ast, errors) = parse_with_errors(source);

        // If we got an AST despite errors, should still tokenize valid parts
        // This tests graceful degradation
        if let Some(ast) = ast {
            let _tokens = collect_semantic_tokens(&ast);

            // Should tokenize without panicking on parse errors
            // (No assertion needed - test passes if no panic occurs)
        } else {
            // If parser returned no AST due to errors, that's also acceptable
            // The test validates we don't panic on error cases
            assert!(!errors.is_empty(), "If no AST returned, should have parse errors");
        }
    }

    // Test semantic tokens handles empty documents
    ///
    /// Validates edge case: empty document should produce empty token list
    /// without panicking or returning errors.
    #[test]
    fn test_semantic_tokens_empty_document() {
        let source = "";
        let (ast, _) = parse_with_errors(source);
        let ast = ast.expect("Empty document should parse successfully");

        let tokens = collect_semantic_tokens(&ast);

        assert!(
            tokens.is_empty(),
            "Empty document should produce no tokens, found {}",
            tokens.len()
        );
    }

    /// Test semantic tokens handles whitespace-only documents
    ///
    /// Validates edge case: document with only whitespace/comments should
    /// produce empty token list.
    #[test]
    fn test_semantic_tokens_whitespace_only() {
        let source = "   \n  \n\t\n";
        let (ast, _) = parse_with_errors(source);
        let ast = ast.expect("Whitespace-only document should parse");

        let tokens = collect_semantic_tokens(&ast);

        assert!(
            tokens.is_empty(),
            "Whitespace-only document should produce no tokens, found {}",
            tokens.len()
        );
    }

    /// Encoding out-of-order tokens must not panic (u32 delta underflow) and
    /// must still yield a valid, monotonically decodable delta stream. The LSP
    /// spec requires ascending order; `encode_tokens` sorts defensively.
    #[test]
    fn test_encode_tokens_out_of_order_does_not_panic() {
        let source = "aaa: 1\nbbb: 2\nccc: 3";
        // Deliberately reversed / interleaved order.
        let tokens = vec![
            SemanticToken {
                line: 2,
                start_col: 0,
                length: 3,
                token_type: ToonTokenType::Property,
                modifiers: ToonTokenModifier::empty(),
            },
            SemanticToken {
                line: 0,
                start_col: 0,
                length: 3,
                token_type: ToonTokenType::Property,
                modifiers: ToonTokenModifier::empty(),
            },
            SemanticToken {
                line: 1,
                start_col: 0,
                length: 3,
                token_type: ToonTokenType::Property,
                modifiers: ToonTokenModifier::empty(),
            },
        ];

        let encoded = encode_tokens(&tokens, source);

        assert_eq!(encoded.len(), 3);
        // After sorting: line 0 (delta 0), line 1 (delta 1), line 2 (delta 1).
        assert_eq!(encoded[0].delta_line, 0);
        assert_eq!(encoded[1].delta_line, 1);
        assert_eq!(encoded[2].delta_line, 1);
        // Decoded absolute lines must be strictly ascending; no underflow wrap.
        let mut abs = 0u32;
        for t in &encoded {
            abs += t.delta_line;
        }
        assert_eq!(abs, 2, "absolute line of last token should be 2");
    }

    /// Two tokens on the same line, supplied out of column order, must produce a
    /// non-wrapping (saturating) column delta rather than a panic.
    #[test]
    fn test_encode_tokens_same_line_reversed_columns() {
        let source = "aa bbbb cc";
        let tokens = vec![
            SemanticToken {
                line: 0,
                start_col: 8,
                length: 2,
                token_type: ToonTokenType::String,
                modifiers: ToonTokenModifier::empty(),
            },
            SemanticToken {
                line: 0,
                start_col: 0,
                length: 2,
                token_type: ToonTokenType::String,
                modifiers: ToonTokenModifier::empty(),
            },
        ];

        let encoded = encode_tokens(&tokens, source);
        assert_eq!(encoded.len(), 2);
        // Sorted: col 0 first (delta 0), then col 8 (delta 8).
        assert_eq!(encoded[0].delta_start, 0);
        assert_eq!(encoded[1].delta_line, 0);
        assert_eq!(encoded[1].delta_start, 8);
    }
}