jarq 0.9.0

An interactive jq-like JSON query tool with a TUI
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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
use nom::{
    IResult, Parser,
    branch::alt,
    bytes::complete::{tag, take},
    character::complete::{anychar, char, digit1, multispace1, one_of, satisfy},
    combinator::{opt, recognize},
    multi::many0,
    sequence::{pair, preceded},
};
use nom_locate::LocatedSpan;

type Span<'a> = LocatedSpan<&'a str>;

/// Navigation token parser module - uses nom for robust tokenization
mod nav_tokens {
    use super::*;

    /// Skip whitespace, returns the position after whitespace (no nav stop)
    pub fn whitespace(input: Span<'_>) -> IResult<Span<'_>, ()> {
        let (rest, _) = multispace1(input)?;
        Ok((rest, ()))
    }

    /// Parse a string literal with proper escape handling
    pub fn string_literal(input: Span<'_>) -> IResult<Span<'_>, ()> {
        let (rest, _) = recognize(pair(
            char('"'),
            pair(
                many0(alt((
                    // Escape sequence: backslash followed by any char
                    recognize(preceded(char('\\'), anychar)),
                    // Regular char: anything except quote or backslash
                    recognize(satisfy(|c| c != '"' && c != '\\')),
                ))),
                char('"'),
            ),
        ))
        .parse(input)?;
        Ok((rest, ()))
    }

    /// Parse an identifier (alphabetic/underscore start, alphanumeric/underscore continue)
    pub fn identifier(input: Span<'_>) -> IResult<Span<'_>, Span<'_>> {
        recognize(pair(
            satisfy(|c| c.is_alphabetic() || c == '_'),
            many0(satisfy(|c| c.is_alphanumeric() || c == '_')),
        ))
        .parse(input)
    }

    /// Parse a number (integer or float with optional exponent)
    pub fn number(input: Span<'_>) -> IResult<Span<'_>, ()> {
        let (rest, _) = recognize(pair(
            // Integer part: optional minus, then digits
            pair(opt(char('-')), digit1),
            // Optional: decimal and/or exponent
            opt(alt((
                // Decimal with optional exponent: .123e45
                recognize(pair(preceded(char('.'), digit1), opt(exponent))),
                // Exponent only: e45
                exponent,
            ))),
        ))
        .parse(input)?;
        Ok((rest, ()))
    }

    /// Parse exponent part of a number (e.g., e10, E-5)
    fn exponent(input: Span<'_>) -> IResult<Span<'_>, Span<'_>> {
        recognize(pair(one_of("eE"), pair(opt(one_of("+-")), digit1))).parse(input)
    }

    /// Two-char operators that need position after
    pub fn two_char_op(input: Span<'_>) -> IResult<Span<'_>, ()> {
        let (rest, _) =
            alt((tag("=="), tag("!="), tag("<="), tag(">="), tag("//"))).parse(input)?;
        Ok((rest, ()))
    }

    /// Single-char tokens that get a position after
    pub fn single_char_token(input: Span<'_>) -> IResult<Span<'_>, char> {
        alt((
            char('.'),
            char('('),
            char(')'),
            char('['),
            char(']'),
            char('{'),
            char('}'),
            char('<'),
            char('>'),
            char('+'),
            char('-'),
            char('*'),
            char('/'),
            char(','),
            char(':'),
            char(';'),
            char('?'),
        ))
        .parse(input)
    }

    /// Pipe gets position before AND after
    pub fn pipe(input: Span<'_>) -> IResult<Span<'_>, ()> {
        let (rest, _) = char('|').parse(input)?;
        Ok((rest, ()))
    }

    /// Skip one character (for unknown tokens)
    pub fn skip_one(input: Span<'_>) -> IResult<Span<'_>, ()> {
        let (rest, _) = take(1usize).parse(input)?;
        Ok((rest, ()))
    }
}

/// A text buffer with cursor position management for filter editing.
#[derive(Debug, Clone)]
pub struct TextBuffer {
    text: String,
    cursor: usize, // char position (not byte position)
}

impl TextBuffer {
    pub fn new(initial: &str) -> Self {
        let cursor = initial.chars().count();
        Self {
            text: initial.to_string(),
            cursor,
        }
    }

    pub fn text(&self) -> &str {
        &self.text
    }

    #[cfg(test)]
    pub fn cursor(&self) -> usize {
        self.cursor
    }

    #[cfg(test)]
    pub fn set_cursor(&mut self, pos: usize) {
        self.cursor = pos.min(self.len());
    }

    pub fn len(&self) -> usize {
        self.text.chars().count()
    }

    /// Insert a character at the current cursor position.
    pub fn insert(&mut self, c: char) {
        let byte_pos = self.cursor_byte_pos();
        self.text.insert(byte_pos, c);
        self.cursor += 1;
    }

    /// Delete the character before the cursor. Returns true if a character was deleted.
    pub fn backspace(&mut self) -> bool {
        if self.cursor > 0 {
            self.cursor -= 1;
            let byte_pos = self.cursor_byte_pos();
            self.text.remove(byte_pos);
            true
        } else {
            false
        }
    }

    /// Delete the character at the cursor. Returns true if a character was deleted.
    pub fn delete(&mut self) -> bool {
        if self.cursor < self.len() {
            let byte_pos = self.cursor_byte_pos();
            self.text.remove(byte_pos);
            true
        } else {
            false
        }
    }

    /// Move cursor one character left.
    pub fn move_left(&mut self) {
        self.cursor = self.cursor.saturating_sub(1);
    }

    /// Move cursor one character right.
    pub fn move_right(&mut self) {
        self.cursor = (self.cursor + 1).min(self.len());
    }

    /// Move cursor to start of text.
    pub fn move_to_start(&mut self) {
        self.cursor = 0;
    }

    /// Move cursor to end of text.
    pub fn move_to_end(&mut self) {
        self.cursor = self.len();
    }

    /// Convert char position to byte index in the underlying string.
    pub fn cursor_byte_pos(&self) -> usize {
        self.char_to_byte_index(self.cursor)
    }

    fn char_to_byte_index(&self, char_index: usize) -> usize {
        self.text
            .char_indices()
            .nth(char_index)
            .map(|(i, _)| i)
            .unwrap_or(self.text.len())
    }

    /// Delete the word before the cursor (Ctrl+W behavior).
    /// Treats `.` and spaces as word boundaries.
    pub fn delete_word_back(&mut self) -> bool {
        if self.cursor == 0 {
            return false;
        }

        let chars: Vec<char> = self.text.chars().collect();
        let mut new_cursor = self.cursor;

        // Skip trailing whitespace/dots
        while new_cursor > 0 && (chars[new_cursor - 1] == '.' || chars[new_cursor - 1] == ' ') {
            new_cursor -= 1;
        }

        // Delete back to previous dot, space, or start
        while new_cursor > 0 && chars[new_cursor - 1] != '.' && chars[new_cursor - 1] != ' ' {
            new_cursor -= 1;
        }

        if new_cursor < self.cursor {
            let start_byte = self.char_to_byte_index(new_cursor);
            let end_byte = self.char_to_byte_index(self.cursor);
            self.text.replace_range(start_byte..end_byte, "");
            self.cursor = new_cursor;
            true
        } else {
            false
        }
    }

    /// Get valid cursor positions for word-based navigation.
    /// Positions are after `.`, after `]`, or after identifiers.
    pub fn navigation_positions(&self) -> Vec<usize> {
        Self::compute_navigation_positions(&self.text)
    }

    fn compute_navigation_positions(filter_text: &str) -> Vec<usize> {
        use nav_tokens::*;

        let mut byte_positions = vec![0usize];
        let mut input = Span::new(filter_text);
        let len = filter_text.len();

        while !input.is_empty() {
            // Skip whitespace (no position added)
            if let Ok((rest, _)) = whitespace(input) {
                input = rest;
                continue;
            }

            // String literal - position after closing quote
            if let Ok((rest, _)) = string_literal(input) {
                byte_positions.push(rest.location_offset());
                input = rest;
                continue;
            }

            // Pipe - position before AND after
            if let Ok((rest, _)) = pipe(input) {
                byte_positions.push(input.location_offset()); // before
                byte_positions.push(rest.location_offset()); // after
                input = rest;
                continue;
            }

            // Two-char operators (==, !=, <=, >=, //)
            if let Ok((rest, _)) = two_char_op(input) {
                byte_positions.push(rest.location_offset());
                input = rest;
                continue;
            }

            // Identifier - position after
            if let Ok((rest, _)) = identifier(input) {
                byte_positions.push(rest.location_offset());
                input = rest;
                continue;
            }

            // Number - position after
            if let Ok((rest, _)) = number(input) {
                byte_positions.push(rest.location_offset());
                input = rest;
                continue;
            }

            // Single-char tokens - position after
            if let Ok((rest, _)) = single_char_token(input) {
                byte_positions.push(rest.location_offset());
                input = rest;
                continue;
            }

            // Unknown character - skip it
            if let Ok((rest, _)) = skip_one(input) {
                input = rest;
            }
        }

        // Always include end position
        if len > 0 && !byte_positions.contains(&len) {
            byte_positions.push(len);
        }

        // Convert byte positions to char positions for Unicode correctness
        let char_positions: Vec<usize> = byte_positions
            .into_iter()
            .map(|byte_pos| filter_text[..byte_pos].chars().count())
            .collect();

        let mut positions = char_positions;
        positions.sort();
        positions.dedup();
        positions
    }

    /// Jump cursor to the previous word boundary.
    pub fn jump_word_back(&mut self) {
        if self.cursor == 0 {
            return;
        }

        let positions = self.navigation_positions();

        // Find largest position less than current cursor
        let mut target = 0;
        for &pos in &positions {
            if pos < self.cursor {
                target = pos;
            }
        }

        self.cursor = target;
    }

    /// Jump cursor to the next word boundary.
    pub fn jump_word_forward(&mut self) {
        let len = self.len();
        if self.cursor >= len {
            return;
        }

        let positions = self.navigation_positions();

        // Find smallest position greater than current cursor
        for &pos in &positions {
            if pos > self.cursor {
                self.cursor = pos;
                return;
            }
        }

        self.cursor = len;
    }
}

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

    #[test]
    fn test_new() {
        let buf = TextBuffer::new("hello");
        assert_eq!(buf.text(), "hello");
        assert_eq!(buf.cursor(), 5);
        assert_eq!(buf.len(), 5);
    }

    #[test]
    fn test_insert() {
        let mut buf = TextBuffer::new(".");
        buf.insert('f');
        assert_eq!(buf.text(), ".f");
        buf.insert('o');
        buf.insert('o');
        assert_eq!(buf.text(), ".foo");
        assert_eq!(buf.cursor(), 4);
    }

    #[test]
    fn test_backspace() {
        let mut buf = TextBuffer::new(".foo");
        assert!(buf.backspace());
        assert_eq!(buf.text(), ".fo");
        assert_eq!(buf.cursor(), 3);
    }

    #[test]
    fn test_backspace_at_start() {
        let mut buf = TextBuffer::new(".");
        buf.set_cursor(0);
        assert!(!buf.backspace());
        assert_eq!(buf.text(), ".");
    }

    #[test]
    fn test_delete() {
        let mut buf = TextBuffer::new(".foo");
        buf.set_cursor(1);
        assert!(buf.delete());
        assert_eq!(buf.text(), ".oo");
        assert_eq!(buf.cursor(), 1);
    }

    #[test]
    fn test_delete_at_end() {
        let mut buf = TextBuffer::new(".foo");
        assert!(!buf.delete());
        assert_eq!(buf.text(), ".foo");
    }

    #[test]
    fn test_move_left_right() {
        let mut buf = TextBuffer::new(".foo");
        assert_eq!(buf.cursor(), 4);
        buf.move_left();
        assert_eq!(buf.cursor(), 3);
        buf.move_right();
        assert_eq!(buf.cursor(), 4);
        buf.move_right(); // Should not go past end
        assert_eq!(buf.cursor(), 4);
    }

    #[test]
    fn test_move_to_start_end() {
        let mut buf = TextBuffer::new(".foo");
        buf.move_to_start();
        assert_eq!(buf.cursor(), 0);
        buf.move_to_end();
        assert_eq!(buf.cursor(), 4);
    }

    #[test]
    fn test_delete_word_back() {
        let mut buf = TextBuffer::new(".foo.bar");
        assert!(buf.delete_word_back());
        assert_eq!(buf.text(), ".foo.");
        assert_eq!(buf.cursor(), 5);
    }

    #[test]
    fn test_delete_word_back_at_start() {
        let mut buf = TextBuffer::new(".");
        buf.set_cursor(0);
        assert!(!buf.delete_word_back());
        assert_eq!(buf.text(), ".");
    }

    #[test]
    fn test_navigation_positions_identity() {
        let buf = TextBuffer::new(".");
        assert_eq!(buf.navigation_positions(), vec![0, 1]);
    }

    #[test]
    fn test_navigation_positions_field() {
        let buf = TextBuffer::new(".foo");
        assert_eq!(buf.navigation_positions(), vec![0, 1, 4]);
    }

    #[test]
    fn test_navigation_positions_iterate() {
        // .[]
        // Positions: 0=start, 1=after ., 2=after [, 3=after ]
        let buf = TextBuffer::new(".[]");
        assert_eq!(buf.navigation_positions(), vec![0, 1, 2, 3]);
    }

    #[test]
    fn test_navigation_positions_multiple_iterate() {
        // .[][][]
        // Positions: 0, 1 (after .), 2 (after [), 3 (after ]), 4 (after [), 5 (after ]), 6 (after [), 7 (after ])
        let buf = TextBuffer::new(".[][][]");
        assert_eq!(buf.navigation_positions(), vec![0, 1, 2, 3, 4, 5, 6, 7]);
    }

    #[test]
    fn test_navigation_positions_index() {
        // .[0]
        // Positions: 0, 1 (after .), 2 (after [), 3 (after 0), 4 (after ])
        let buf = TextBuffer::new(".[0]");
        assert_eq!(buf.navigation_positions(), vec![0, 1, 2, 3, 4]);
    }

    #[test]
    fn test_navigation_positions_field_chain() {
        // .foo.bar
        // Positions: 0, 1 (after .), 4 (after foo), 5 (after second .), 8 (after bar)
        let buf = TextBuffer::new(".foo.bar");
        assert_eq!(buf.navigation_positions(), vec![0, 1, 4, 5, 8]);
    }

    #[test]
    fn test_navigation_positions_mixed() {
        // .foo[0].bar
        // Positions: 0, 1 (after .), 4 (after foo), 5 (after [), 6 (after 0), 7 (after ]), 8 (after .), 11 (after bar)
        let buf = TextBuffer::new(".foo[0].bar");
        assert_eq!(buf.navigation_positions(), vec![0, 1, 4, 5, 6, 7, 8, 11]);
    }

    #[test]
    fn test_navigation_positions_quoted_field() {
        // .["foo"]
        // Positions: 0, 1 (after .), 2 (after [), 7 (after "foo"), 8 (after ])
        let buf = TextBuffer::new(".[\"foo\"]");
        assert_eq!(buf.navigation_positions(), vec![0, 1, 2, 7, 8]);
    }

    #[test]
    fn test_jump_word_back() {
        // .foo.bar has positions: 0, 1, 4, 5, 8
        let mut buf = TextBuffer::new(".foo.bar");
        buf.jump_word_back(); // 8 -> 5
        assert_eq!(buf.cursor(), 5);
        buf.jump_word_back(); // 5 -> 4
        assert_eq!(buf.cursor(), 4);
        buf.jump_word_back(); // 4 -> 1
        assert_eq!(buf.cursor(), 1);
        buf.jump_word_back(); // 1 -> 0
        assert_eq!(buf.cursor(), 0);
    }

    #[test]
    fn test_jump_word_forward() {
        // .foo.bar has positions: 0, 1, 4, 5, 8
        let mut buf = TextBuffer::new(".foo.bar");
        buf.set_cursor(0);
        buf.jump_word_forward(); // 0 -> 1
        assert_eq!(buf.cursor(), 1);
        buf.jump_word_forward(); // 1 -> 4
        assert_eq!(buf.cursor(), 4);
        buf.jump_word_forward(); // 4 -> 5
        assert_eq!(buf.cursor(), 5);
        buf.jump_word_forward(); // 5 -> 8
        assert_eq!(buf.cursor(), 8);
    }

    #[test]
    fn test_unicode_handling() {
        let mut buf = TextBuffer::new(".héllo");
        assert_eq!(buf.len(), 6);
        assert_eq!(buf.cursor(), 6);
        buf.move_left();
        assert_eq!(buf.cursor(), 5);
        buf.insert('!');
        assert_eq!(buf.text(), ".héll!o");
    }

    #[test]
    fn test_navigation_positions_with_pipes() {
        // ". | sort | reverse"
        // 0123456789...
        // Positions: 0, 1 (after .), 2 (before |), 3 (after |), 8 (after sort), 9 (before |), 10 (after |), 18 (after reverse)
        let buf = TextBuffer::new(". | sort | reverse");
        let positions = buf.navigation_positions();
        assert!(positions.contains(&0)); // start
        assert!(positions.contains(&1)); // after .
        assert!(positions.contains(&2)); // before first |
        assert!(positions.contains(&3)); // after first |
        assert!(positions.contains(&8)); // after sort
        assert!(positions.contains(&9)); // before second |
        assert!(positions.contains(&10)); // after second |
        assert!(positions.contains(&18)); // end (after reverse)
    }

    #[test]
    fn test_jump_word_back_with_pipes() {
        // ". | sort | reverse" positions: 0, 1, 2, 3, 8, 9, 10, 18
        let mut buf = TextBuffer::new(". | sort | reverse");
        // cursor at end (18)
        buf.jump_word_back(); // 18 -> 10
        assert_eq!(buf.cursor(), 10); // after second |
        buf.jump_word_back(); // 10 -> 9
        assert_eq!(buf.cursor(), 9); // before second |
        buf.jump_word_back(); // 9 -> 8
        assert_eq!(buf.cursor(), 8); // after sort
        buf.jump_word_back(); // 8 -> 3
        assert_eq!(buf.cursor(), 3); // after first |
        buf.jump_word_back(); // 3 -> 2
        assert_eq!(buf.cursor(), 2); // before first |
        buf.jump_word_back(); // 2 -> 1
        assert_eq!(buf.cursor(), 1); // after .
        buf.jump_word_back(); // 1 -> 0
        assert_eq!(buf.cursor(), 0); // start
    }

    #[test]
    fn test_navigation_positions_escaped_backslash_in_string() {
        // Key ending with backslash: .["key\\"] | keys
        // The \\" is escaped backslash, followed by real closing quote
        // .["key\\"] | keys
        // 0123456789012345678
        //           1111111
        // Positions: 0, 1 (after .), 2 (after [), 9 (after "key\\"), 10 (after ]), 11 (before |), 12 (after |), 17 (after keys)
        let buf = TextBuffer::new(r#".["key\\"] | keys"#);
        let positions = buf.navigation_positions();
        assert!(positions.contains(&0)); // start
        assert!(positions.contains(&1)); // after .
        assert!(positions.contains(&2)); // after [
        assert!(positions.contains(&9)); // after "key\\"
        assert!(positions.contains(&10)); // after ]
        assert!(positions.contains(&11)); // before |
        assert!(positions.contains(&12)); // after |
        assert!(positions.contains(&17)); // end (after keys)
    }

    #[test]
    fn test_expression_nav_select_with_comparison() {
        // [.items[] | select(.count < 20)]
        // 01234567890123456789012345678901
        //           1111111111222222222233
        let buf = TextBuffer::new("[.items[] | select(.count < 20)]");
        let positions = buf.navigation_positions();

        // Check key positions for expression-aware navigation
        assert!(positions.contains(&0)); // start
        assert!(positions.contains(&1)); // after [
        assert!(positions.contains(&2)); // after .
        assert!(positions.contains(&7)); // after items
        assert!(positions.contains(&8)); // after [
        assert!(positions.contains(&9)); // after ]
        assert!(positions.contains(&10)); // before |
        assert!(positions.contains(&11)); // after |
        assert!(positions.contains(&18)); // after select
        assert!(positions.contains(&19)); // after (
        assert!(positions.contains(&20)); // after .
        assert!(positions.contains(&25)); // after count
        assert!(positions.contains(&27)); // after <
        assert!(positions.contains(&30)); // after 20
        assert!(positions.contains(&31)); // after )
        assert!(positions.contains(&32)); // after ]
    }

    #[test]
    fn test_expression_nav_operators() {
        // Test comparison and arithmetic operators
        // .x + .y - .z * 2 / 3 // 1
        // 0123456789012345678901234
        //           1111111111222222
        let buf = TextBuffer::new(".x + .y - .z * 2 / 3 // 1");
        let positions = buf.navigation_positions();

        assert!(positions.contains(&0)); // start
        assert!(positions.contains(&1)); // after .
        assert!(positions.contains(&2)); // after x
        assert!(positions.contains(&4)); // after +
        assert!(positions.contains(&6)); // after .
        assert!(positions.contains(&7)); // after y
        assert!(positions.contains(&9)); // after -
        assert!(positions.contains(&11)); // after .
        assert!(positions.contains(&12)); // after z
        assert!(positions.contains(&14)); // after *
        assert!(positions.contains(&16)); // after 2
        assert!(positions.contains(&18)); // after /
        assert!(positions.contains(&20)); // after 3
        assert!(positions.contains(&23)); // after //
        assert!(positions.contains(&25)); // after 1
    }

    #[test]
    fn test_expression_nav_nested_parens() {
        // Test nested parentheses
        let buf = TextBuffer::new("(((.x)))");
        let positions = buf.navigation_positions();

        assert!(positions.contains(&0)); // start
        assert!(positions.contains(&1)); // after first (
        assert!(positions.contains(&2)); // after second (
        assert!(positions.contains(&3)); // after third (
        assert!(positions.contains(&4)); // after .
        assert!(positions.contains(&5)); // after x
        assert!(positions.contains(&6)); // after first )
        assert!(positions.contains(&7)); // after second )
        assert!(positions.contains(&8)); // after third )
    }

    #[test]
    fn test_expression_nav_string_literals() {
        // Test that string contents are skipped
        // .foo | select(.bar == "test.value") | .baz
        // 012345678901234567890123456789012345678901
        //           11111111112222222222333333333344
        let buf = TextBuffer::new(r#".foo | select(.bar == "test.value") | .baz"#);
        let positions = buf.navigation_positions();

        // Should not stop inside "test.value"
        assert!(positions.contains(&0)); // start
        assert!(positions.contains(&1)); // after .
        assert!(positions.contains(&4)); // after foo
        assert!(positions.contains(&5)); // before |
        assert!(positions.contains(&6)); // after |
        assert!(positions.contains(&13)); // after select
        assert!(positions.contains(&14)); // after (
        assert!(positions.contains(&15)); // after .
        assert!(positions.contains(&18)); // after bar
        assert!(positions.contains(&21)); // after ==
        assert!(positions.contains(&34)); // after "test.value"
        assert!(positions.contains(&35)); // after )
        assert!(positions.contains(&36)); // before |
        assert!(positions.contains(&37)); // after |
        assert!(positions.contains(&39)); // after .
        assert!(positions.contains(&42)); // after baz
    }

    #[test]
    fn test_expression_nav_keywords() {
        // Test keyword navigation
        // if .x > 0 then .y else .z end
        // 01234567890123456789012345678
        //           1111111111222222222
        let buf = TextBuffer::new("if .x > 0 then .y else .z end");
        let positions = buf.navigation_positions();

        assert!(positions.contains(&0)); // start
        assert!(positions.contains(&2)); // after if
        assert!(positions.contains(&4)); // after .
        assert!(positions.contains(&5)); // after x
        assert!(positions.contains(&7)); // after >
        assert!(positions.contains(&9)); // after 0
        assert!(positions.contains(&14)); // after then
        assert!(positions.contains(&16)); // after .
        assert!(positions.contains(&17)); // after y
        assert!(positions.contains(&22)); // after else
        assert!(positions.contains(&24)); // after .
        assert!(positions.contains(&25)); // after z
        assert!(positions.contains(&29)); // after end
    }

    #[test]
    fn test_expression_nav_logical_operators() {
        // Test and/or/not keywords
        // .x > 0 and .y < 10 or not .z
        // 0123456789012345678901234567
        //           11111111112222222222
        let buf = TextBuffer::new(".x > 0 and .y < 10 or not .z");
        let positions = buf.navigation_positions();

        assert!(positions.contains(&0)); // start
        assert!(positions.contains(&1)); // after .
        assert!(positions.contains(&2)); // after x
        assert!(positions.contains(&4)); // after >
        assert!(positions.contains(&6)); // after 0
        assert!(positions.contains(&10)); // after and
        assert!(positions.contains(&12)); // after .
        assert!(positions.contains(&13)); // after y
        assert!(positions.contains(&15)); // after <
        assert!(positions.contains(&18)); // after 10
        assert!(positions.contains(&21)); // after or
        assert!(positions.contains(&25)); // after not
        assert!(positions.contains(&27)); // after .
        assert!(positions.contains(&28)); // after z
    }

    #[test]
    fn test_expression_nav_string_with_escaped_quote() {
        // String with escaped quote inside: .x == "foo\"bar"
        // 0123456789012345678
        //           11111111
        let buf = TextBuffer::new(r#".x == "foo\"bar""#);
        let positions = buf.navigation_positions();

        assert!(positions.contains(&0)); // start
        assert!(positions.contains(&1)); // after .
        assert!(positions.contains(&2)); // after x
        assert!(positions.contains(&5)); // after ==
        assert!(positions.contains(&16)); // after "foo\"bar"
        // Should NOT stop inside the string
        assert!(!positions.contains(&7)); // inside string
        assert!(!positions.contains(&10)); // at escaped quote
    }

    #[test]
    fn test_expression_nav_string_with_keywords() {
        // String containing keywords that should be ignored: .x == "and or if"
        // 01234567890123456789
        //           1111111111
        let buf = TextBuffer::new(r#".x == "and or if""#);
        let positions = buf.navigation_positions();

        assert!(positions.contains(&0)); // start
        assert!(positions.contains(&1)); // after .
        assert!(positions.contains(&2)); // after x
        assert!(positions.contains(&5)); // after ==
        assert!(positions.contains(&17)); // after "and or if"
        // Should NOT stop at keywords inside the string
        assert!(!positions.contains(&7)); // at "and"
        assert!(!positions.contains(&11)); // at "or"
        assert!(!positions.contains(&14)); // at "if"
    }

    #[test]
    fn test_expression_nav_string_with_operators() {
        // String containing operators: .x == "> | ."
        // 0123456789012345
        //           11111
        let buf = TextBuffer::new(r#".x == "> | .""#);
        let positions = buf.navigation_positions();

        assert!(positions.contains(&0)); // start
        assert!(positions.contains(&1)); // after .
        assert!(positions.contains(&2)); // after x
        assert!(positions.contains(&5)); // after ==
        assert!(positions.contains(&13)); // after "> | ."
        // Should NOT stop at operators inside the string
        assert!(!positions.contains(&7)); // at ">"
        assert!(!positions.contains(&9)); // at "|"
        assert!(!positions.contains(&11)); // at "."
    }

    #[test]
    fn test_expression_nav_complex_filter() {
        // A more complex filter example
        // [.data[] | select(.val > 100 and .ok == true)] | sort_by(.val) | reverse
        // 0123456789012345678901234567890123456789012345678901234567890123456789012345
        //           1111111111222222222233333333334444444444555555555566666666667777777
        let buf = TextBuffer::new(
            "[.data[] | select(.val > 100 and .ok == true)] | sort_by(.val) | reverse",
        );
        let positions = buf.navigation_positions();

        // Key navigation points
        assert!(positions.contains(&0)); // start
        assert!(positions.contains(&1)); // after [
        assert!(positions.contains(&2)); // after .
        assert!(positions.contains(&6)); // after data
        assert!(positions.contains(&7)); // after [
        assert!(positions.contains(&8)); // after ]
        assert!(positions.contains(&9)); // before |
        assert!(positions.contains(&10)); // after |
        assert!(positions.contains(&17)); // after select
        assert!(positions.contains(&18)); // after (
        assert!(positions.contains(&19)); // after .
        assert!(positions.contains(&22)); // after val
        assert!(positions.contains(&24)); // after >
        assert!(positions.contains(&28)); // after 100
        assert!(positions.contains(&32)); // after and
        assert!(positions.contains(&34)); // after .
        assert!(positions.contains(&36)); // after ok
        assert!(positions.contains(&39)); // after ==
        assert!(positions.contains(&44)); // after true
        assert!(positions.contains(&45)); // after )
        assert!(positions.contains(&46)); // after ]
        assert!(positions.contains(&47)); // before |
        assert!(positions.contains(&48)); // after |
        assert!(positions.contains(&56)); // after sort_by
        assert!(positions.contains(&57)); // after (
        assert!(positions.contains(&58)); // after .
        assert!(positions.contains(&61)); // after val
        assert!(positions.contains(&62)); // after )
        assert!(positions.contains(&63)); // before |
        assert!(positions.contains(&64)); // after |
        assert!(positions.contains(&72)); // after reverse
    }
}