eggsact 1.2.7

Deterministic MCP and in-process utility tools for coding agents
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
use serde::{Deserialize, Serialize};

use crate::text::primitives::byte_offset_to_char_index;
use unicode_general_category::get_general_category;
use unicode_segmentation::UnicodeSegmentation;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TextPositionResult {
    pub valid: bool,
    pub byte_offset: Option<usize>,
    pub codepoint_index: Option<usize>,
    pub utf16_offset: Option<usize>,
    pub line: Option<usize>,
    pub column: Option<usize>,
    pub line_base: usize,
    pub column_base: usize,
    #[serde(rename = "char")]
    pub char: Option<String>,
    pub codepoint: Option<String>,
    pub name: Option<String>,
    pub line_text_preview: Option<String>,
    pub error: Option<String>,
    pub summary: String,
}

fn utf16_offset_to_codepoint_index(text: &str, utf16_offset: usize) -> usize {
    let mut utf16_count = 0;
    for (i, char) in text.chars().enumerate() {
        let cp = char as u32;
        if cp <= 0xFFFF {
            utf16_count += 1;
        } else {
            utf16_count += 2;
        }
        if utf16_count > utf16_offset {
            return i;
        }
        if utf16_count == utf16_offset {
            return i + 1;
        }
    }
    text.chars().count()
}

fn codepoint_index_to_utf16_offset(text: &str, codepoint_index: usize) -> usize {
    let mut utf16_offset = 0;
    for (i, char) in text.chars().enumerate() {
        if i >= codepoint_index {
            break;
        }
        let cp = char as u32;
        if cp <= 0xFFFF {
            utf16_offset += 1;
        } else {
            utf16_offset += 2;
        }
    }
    utf16_offset
}

fn split_lines_keepends(text: &str) -> Vec<&str> {
    let mut lines: Vec<&str> = Vec::new();
    if text.is_empty() {
        return lines;
    }

    let mut start = 0usize;
    let mut i = 0usize;
    let chars: Vec<(usize, char)> = text.char_indices().collect();
    while i < chars.len() {
        let (byte_idx, ch) = chars[i];
        let ch_len = ch.len_utf8();

        if crate::text::primitives::is_line_break(ch) && ch != '\r' {
            lines.push(&text[start..byte_idx + ch_len]);
            start = byte_idx + ch_len;
        } else if ch == '\r' {
            if i + 1 < chars.len() && chars[i + 1].1 == '\n' {
                let end = chars[i + 1].0 + chars[i + 1].1.len_utf8();
                lines.push(&text[start..end]);
                start = end;
                i += 1;
            } else {
                lines.push(&text[start..byte_idx + ch_len]);
                start = byte_idx + ch_len;
            }
        }

        i += 1;
    }

    if start < text.len()
        || text
            .chars()
            .last()
            .is_some_and(crate::text::primitives::is_line_break)
    {
        lines.push(&text[start..]);
    }

    lines
}

fn trim_line_ending(line: &str) -> &str {
    line.trim_end_matches(crate::text::primitives::is_line_break)
}

fn get_line_col(text: &str, codepoint_index: usize) -> (Vec<&str>, usize, usize) {
    let lines = split_lines_keepends(text);
    let chars: Vec<char> = text.chars().collect();
    let mut line_num = 0usize;
    let mut current_col = 0usize;

    for i in 0..chars.len() {
        if i == codepoint_index {
            return (lines, line_num, current_col);
        }

        match chars[i] {
            '\n' | '\x0b' | '\x0c' | '\x1c' | '\x1d' | '\x1e' | '\u{0085}' | '\u{2028}'
            | '\u{2029}' => {
                line_num += 1;
                current_col = 0;
            }
            '\r' => {
                if i + 1 < chars.len() && chars[i + 1] == '\n' {
                    continue;
                }
                line_num += 1;
                current_col = 0;
            }
            _ => {
                current_col += 1;
            }
        }
    }

    if codepoint_index >= chars.len() {
        return (lines, line_num, current_col);
    }

    (lines, line_num, current_col)
}

fn is_valid_byte_offset(text: &str, offset: usize) -> bool {
    // `text` is guaranteed valid UTF-8; a byte offset is valid iff it lies
    // on a char boundary (including EOF). This correctly rejects overlong
    // encodings, surrogates, and continuation bytes without hand-rolled
    // validation (see B6).
    text.is_char_boundary(offset)
}

fn codepoint_index_to_byte_offset(text: &str, codepoint_index: usize) -> usize {
    text.char_indices()
        .nth(codepoint_index)
        .map(|(byte_idx, _)| byte_idx)
        .unwrap_or_else(|| text.len())
}

fn line_column_to_codepoint_index(
    text: &str,
    line: usize,
    column: usize,
    line_base: usize,
    column_base: usize,
) -> Option<usize> {
    let lines = split_lines_keepends(text);
    let line_index = line.checked_sub(line_base)?;
    if line_index >= lines.len() {
        return None;
    }

    let col_index = column.checked_sub(column_base)?;
    let line_text_raw = lines[line_index];
    let trimmed_len = trim_line_ending(line_text_raw).chars().count();
    if col_index > trimmed_len {
        return None;
    }

    let mut codepoint_index = 0usize;
    for line_text in &lines[..line_index] {
        codepoint_index += line_text.chars().count();
    }

    Some(codepoint_index + col_index)
}

#[allow(clippy::too_many_arguments)]
pub fn text_position(
    text: &str,
    byte_offset: Option<usize>,
    codepoint_index: Option<usize>,
    line: Option<usize>,
    column: Option<usize>,
    utf16_offset: Option<usize>,
    line_base: usize,
    column_base: usize,
) -> TextPositionResult {
    let mut mode_parts = Vec::new();
    if byte_offset.is_some() {
        mode_parts.push("byte_offset");
    }
    if codepoint_index.is_some() {
        mode_parts.push("codepoint_index");
    }
    if line.is_some() || column.is_some() {
        mode_parts.push("line+column");
    }
    if utf16_offset.is_some() {
        mode_parts.push("utf16_offset");
    }

    if mode_parts.len() != 1 {
        return TextPositionResult {
            valid: false,
            byte_offset: None,
            codepoint_index: None,
            utf16_offset: None,
            line: None,
            column: None,
            line_base,
            column_base,
            char: None,
            codepoint: None,
            name: None,
            line_text_preview: None,
            error: Some(
                "Exactly one locator mode must be provided: byte_offset, codepoint_index, line+column, or utf16_offset".to_string(),
            ),
            summary: "Invalid: multiple or no locator modes provided".to_string(),
        };
    }

    if text.is_empty() {
        if byte_offset.is_some() && byte_offset != Some(0) {
            return TextPositionResult {
                valid: false,
                byte_offset: None,
                codepoint_index: None,
                utf16_offset: None,
                line: None,
                column: None,
                line_base,
                column_base,
                char: None,
                codepoint: None,
                name: None,
                line_text_preview: None,
                error: Some("Byte offset 0 is the only valid position for empty text".to_string()),
                summary: "Invalid position for empty text".to_string(),
            };
        }
        return TextPositionResult {
            valid: true,
            byte_offset: Some(0),
            codepoint_index: Some(0),
            utf16_offset: Some(0),
            line: Some(line_base),
            column: Some(column_base),
            line_base,
            column_base,
            char: Some(String::new()),
            codepoint: None,
            name: None,
            line_text_preview: Some(String::new()),
            error: None,
            summary: "Empty text at start position".to_string(),
        };
    }

    let total_codepoints = text.chars().count();
    let effective_codepoint_index: usize;

    if let Some(bo) = byte_offset {
        if bo > text.len() {
            return TextPositionResult {
                valid: false,
                byte_offset: None,
                codepoint_index: None,
                utf16_offset: None,
                line: None,
                column: None,
                line_base,
                column_base,
                char: None,
                codepoint: None,
                name: None,
                line_text_preview: None,
                error: Some("Byte offset exceeds text length".to_string()),
                summary: "Invalid byte offset: beyond text end".to_string(),
            };
        }
        if !is_valid_byte_offset(text, bo) {
            return TextPositionResult {
                valid: false,
                byte_offset: None,
                codepoint_index: None,
                utf16_offset: None,
                line: None,
                column: None,
                line_base,
                column_base,
                char: None,
                codepoint: None,
                name: None,
                line_text_preview: None,
                error: Some("Byte offset falls inside multibyte character".to_string()),
                summary: "Invalid byte offset: inside multibyte character".to_string(),
            };
        }
        effective_codepoint_index = byte_offset_to_char_index(text, bo).unwrap_or(total_codepoints);
    } else if let Some(cp) = codepoint_index {
        if cp > total_codepoints {
            return TextPositionResult {
                valid: false,
                byte_offset: None,
                codepoint_index: None,
                utf16_offset: None,
                line: None,
                column: None,
                line_base,
                column_base,
                char: None,
                codepoint: None,
                name: None,
                line_text_preview: None,
                error: Some("Codepoint index out of bounds".to_string()),
                summary: "Invalid codepoint_index: out of bounds".to_string(),
            };
        }
        effective_codepoint_index = cp;
    } else if let Some(u16) = utf16_offset {
        let cp_idx = utf16_offset_to_codepoint_index(text, u16);
        if cp_idx > total_codepoints {
            return TextPositionResult {
                valid: false,
                byte_offset: None,
                codepoint_index: None,
                utf16_offset: None,
                line: None,
                column: None,
                line_base,
                column_base,
                char: None,
                codepoint: None,
                name: None,
                line_text_preview: None,
                error: Some("UTF-16 offset exceeds text length".to_string()),
                summary: "Invalid utf16_offset: beyond text end".to_string(),
            };
        }
        effective_codepoint_index = cp_idx;
    } else {
        let ln = line.unwrap_or(1);
        let col = column.unwrap_or(1);

        let lines_keepends = split_lines_keepends(text);
        let line_count = lines_keepends.len();
        if ln < line_base || ln >= line_base + line_count {
            let max_line = line_base + line_count.saturating_sub(1);
            if ln < line_base {
                return TextPositionResult {
                    valid: false,
                    byte_offset: None,
                    codepoint_index: None,
                    utf16_offset: None,
                    line: None,
                    column: None,
                    line_base,
                    column_base,
                    char: None,
                    codepoint: None,
                    name: None,
                    line_text_preview: None,
                    error: Some(format!("Line {ln} is less than minimum line {line_base}")),
                    summary: "Invalid line: below valid range".to_string(),
                };
            }
            return TextPositionResult {
                valid: false,
                byte_offset: None,
                codepoint_index: None,
                utf16_offset: None,
                line: None,
                column: None,
                line_base,
                column_base,
                char: None,
                codepoint: None,
                name: None,
                line_text_preview: None,
                error: Some(format!("Line {ln} exceeds maximum line {max_line}")),
                summary: "Invalid line: beyond text end".to_string(),
            };
        }

        let lines = lines_keepends;

        if col < column_base {
            return TextPositionResult {
                valid: false,
                byte_offset: None,
                codepoint_index: None,
                utf16_offset: None,
                line: None,
                column: None,
                line_base,
                column_base,
                char: None,
                codepoint: None,
                name: None,
                line_text_preview: None,
                error: Some(format!(
                    "Column {col} is less than minimum column {column_base}"
                )),
                summary: "Invalid column: below valid range".to_string(),
            };
        }

        let line_index = ln.saturating_sub(line_base);
        let col_index = col.saturating_sub(column_base);

        if line_index < lines.len() {
            let line_text_raw = lines[line_index];
            let trimmed_len = trim_line_ending(line_text_raw).chars().count();

            if col_index > trimmed_len {
                return TextPositionResult {
                    valid: false,
                    byte_offset: None,
                    codepoint_index: None,
                    utf16_offset: None,
                    line: None,
                    column: None,
                    line_base,
                    column_base,
                    char: None,
                    codepoint: None,
                    name: None,
                    line_text_preview: None,
                    error: Some(format!("Column {col} exceeds line length {trimmed_len}")),
                    summary: "Invalid column: beyond line length".to_string(),
                };
            }
        }

        effective_codepoint_index =
            line_column_to_codepoint_index(text, ln, col, line_base, column_base)
                .unwrap_or(total_codepoints);
    }

    let (lines, line_num, col) = get_line_col(text, effective_codepoint_index);

    let line_1based = line_num + line_base;
    let col_1based = col + column_base;

    let char_at_pos = text.chars().nth(effective_codepoint_index);

    let (char_val, codepoint_str, name_val) = if let Some(ch) = char_at_pos {
        let cp_str = format!("U+{:04X}", ch as u32);
        let name_val = unicodedata_name(ch);
        (Some(ch.to_string()), Some(cp_str), Some(name_val))
    } else {
        (None, None, None)
    };

    let line_preview = if line_num < lines.len() {
        Some(lines[line_num].trim_end_matches(['\r', '\n']).to_string())
    } else {
        None
    };

    let byte_offset_result = codepoint_index_to_byte_offset(text, effective_codepoint_index);
    let utf16_result = codepoint_index_to_utf16_offset(text, effective_codepoint_index);

    TextPositionResult {
        valid: true,
        byte_offset: Some(byte_offset_result),
        codepoint_index: Some(effective_codepoint_index),
        utf16_offset: Some(utf16_result),
        line: Some(line_1based),
        column: Some(col_1based),
        line_base,
        column_base,
        char: char_val,
        codepoint: codepoint_str,
        name: name_val,
        line_text_preview: line_preview,
        error: None,
        summary: format!("Line {line_1based}, column {col_1based}"),
    }
}

fn unicodedata_name(ch: char) -> String {
    unicode_names2::name(ch)
        .map(|name| name.to_string())
        .unwrap_or_else(|| "<unknown>".to_string())
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TextWindowPosition {
    pub kind: String,
    pub value: Option<usize>,
    pub byte_offset: Option<usize>,
    pub codepoint_index: Option<usize>,
    pub grapheme_index: Option<usize>,
    pub line: Option<usize>,
    pub column: Option<usize>,
    pub line_base: Option<usize>,
    pub column_base: Option<usize>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PositionInfo {
    pub byte_offset: usize,
    pub codepoint_index: usize,
    pub grapheme_index: usize,
    pub line: usize,
    pub column: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LineInfo {
    pub line: usize,
    pub text: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AtCodepointInfo {
    #[serde(rename = "char")]
    pub char: char,
    pub codepoint: String,
    pub name: String,
    pub category: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TextWindowResult {
    pub position: PositionInfo,
    pub line_text: String,
    pub line_visible_repr: String,
    pub before: Vec<LineInfo>,
    pub after: Vec<LineInfo>,
    pub newline_style: String,
    pub at_codepoint: Option<AtCodepointInfo>,
    pub warnings: Vec<String>,
}

pub fn text_window(
    text: &str,
    position: &TextWindowPosition,
    context_lines: usize,
    include_visible_repr: bool,
) -> TextWindowResult {
    let mut warnings = Vec::new();

    let kind = &position.kind;
    let line_base = position.line_base.unwrap_or(1);
    let column_base = position.column_base.unwrap_or(1);
    let total_codepoints = text.chars().count();
    let graphemes: Vec<(usize, &str)> = text.grapheme_indices(true).collect();

    let mut codepoint_index: Option<usize> = None;

    match kind.as_str() {
        "byte_offset" => {
            let bo = position.value.or(position.byte_offset).unwrap_or(0);
            if bo > text.len() || !text.is_char_boundary(bo) {
                warnings.push(format!(
                    "byte_offset {} out of range or not on char boundary (clamped to EOF)",
                    bo
                ));
                codepoint_index = Some(total_codepoints);
            } else {
                codepoint_index =
                    Some(byte_offset_to_char_index(text, bo).unwrap_or(total_codepoints));
            }
        }
        "codepoint_index" => {
            let cp = position.value.or(position.codepoint_index).unwrap_or(0);
            if cp <= total_codepoints {
                codepoint_index = Some(cp);
            } else {
                warnings.push(format!(
                    "codepoint_index {} beyond total codepoints {} (clamped to EOF)",
                    cp, total_codepoints
                ));
            }
        }
        "grapheme_index" => {
            let gi = position.value.or(position.grapheme_index).unwrap_or(0);
            if gi < graphemes.len() {
                let byte_idx = graphemes[gi].0;
                codepoint_index =
                    Some(byte_offset_to_char_index(text, byte_idx).unwrap_or(total_codepoints));
            } else {
                warnings.push(format!(
                    "grapheme_index {} beyond total graphemes {} (clamped to EOF)",
                    gi,
                    graphemes.len()
                ));
                codepoint_index = Some(total_codepoints);
            }
        }
        "line_column" => {
            let ln = position.line.or(position.value).unwrap_or(1);
            let col = position.column.unwrap_or(1);
            let resolved = line_column_to_codepoint_index(text, ln, col, line_base, column_base);
            if resolved.is_none() {
                let lines = split_lines_keepends(text);
                let line_index = ln.checked_sub(line_base);
                if line_index.is_none_or(|idx| idx >= lines.len()) {
                    warnings.push(format!(
                        "line_column out of range: line {} beyond total lines {} (clamped to EOF)",
                        ln,
                        lines.len()
                    ));
                } else {
                    let line_text_raw = lines[line_index.unwrap()];
                    let trimmed_len = line_text_raw.trim_end_matches(['\r', '\n']).chars().count();
                    let col_index = col.checked_sub(column_base);
                    if col_index.is_none_or(|idx| idx > trimmed_len) {
                        warnings.push(format!(
                            "line_column out of range: column {} beyond line {} length {} (clamped to EOF)",
                            col, ln, trimmed_len
                        ));
                    } else {
                        warnings.push(format!(
                            "line_column out of range: line {} column {} invalid (clamped to EOF)",
                            ln, col
                        ));
                    }
                }
            }
            codepoint_index = resolved;
        }
        _ => {
            codepoint_index = Some(0);
        }
    }

    let cp_idx = codepoint_index.unwrap_or(total_codepoints);
    let (line_num, column_num) = get_line_col_for_cp(text, cp_idx, line_base, column_base);

    let byte_offset = codepoint_index_to_byte_offset(text, cp_idx);

    let mut grapheme_index = 0;
    for (start_byte, _) in &graphemes {
        let start_cp = byte_offset_to_char_index(text, *start_byte).unwrap_or(total_codepoints);
        if start_cp < cp_idx {
            grapheme_index += 1;
        } else {
            break;
        }
    }

    let lines_all = split_lines_keepends(text);
    let line_index = line_num.saturating_sub(line_base);
    let line_text = if line_index < lines_all.len() {
        trim_line_ending(lines_all[line_index]).to_string()
    } else {
        String::new()
    };

    let line_visible_repr = if include_visible_repr {
        line_text.replace('\t', "→").replace(' ', "·")
    } else {
        String::new()
    };

    let newline_style = detect_newline_style(text);

    let before: Vec<LineInfo> = (0..context_lines)
        .filter_map(|offset| {
            let ln = line_num.saturating_sub(line_base + 1 + offset);
            if ln < lines_all.len() {
                Some(LineInfo {
                    line: ln + line_base,
                    text: trim_line_ending(lines_all[ln]).to_string(),
                })
            } else {
                None
            }
        })
        .collect();
    let before: Vec<LineInfo> = before.into_iter().rev().collect();

    let after: Vec<LineInfo> = (0..context_lines)
        .filter_map(|offset| {
            let ln = line_num.saturating_sub(line_base) + 1 + offset;
            if ln < lines_all.len() {
                Some(LineInfo {
                    line: ln + line_base,
                    text: trim_line_ending(lines_all[ln]).to_string(),
                })
            } else {
                None
            }
        })
        .collect();

    let at_codepoint = if cp_idx < total_codepoints {
        let ch = text
            .chars()
            .nth(cp_idx)
            .expect("invariant: cp_idx < total_codepoints guarantees a char at cp_idx");
        let codepoint_str = format!("U+{:04X}", ch as u32);
        let category = get_unicode_category(ch);
        let name_val = unicodedata_name(ch);
        Some(AtCodepointInfo {
            char: ch,
            codepoint: codepoint_str,
            name: name_val,
            category,
        })
    } else {
        None
    };

    if byte_offset < text.len() {
        let b = text.as_bytes()[byte_offset];
        if (0x80..0xC0).contains(&b) {
            warnings.push(
                "Position falls in middle of multi-byte sequence (byte is continuation byte)"
                    .to_string(),
            );
        }
    }

    TextWindowResult {
        position: PositionInfo {
            byte_offset,
            codepoint_index: cp_idx,
            grapheme_index,
            line: line_num,
            column: column_num,
        },
        line_text,
        line_visible_repr,
        before,
        after,
        newline_style,
        at_codepoint,
        warnings,
    }
}

fn get_line_col_for_cp(
    text: &str,
    codepoint_index: usize,
    line_base: usize,
    column_base: usize,
) -> (usize, usize) {
    let mut current_line = line_base;
    let mut current_col = column_base;
    let chars: Vec<char> = text.chars().collect();

    for i in 0..chars.len() {
        if i == codepoint_index {
            return (current_line, current_col);
        }

        match chars[i] {
            '\n' | '\x0b' | '\x0c' | '\x1c' | '\x1d' | '\x1e' | '\u{0085}' | '\u{2028}'
            | '\u{2029}' => {
                current_line += 1;
                current_col = column_base;
            }
            '\r' => {
                if i + 1 < chars.len() && chars[i + 1] == '\n' {
                    continue;
                }
                current_line += 1;
                current_col = column_base;
            }
            _ => {
                current_col += 1;
            }
        }
    }

    (current_line, current_col)
}

fn detect_newline_style(text: &str) -> String {
    crate::text::primitives::detect_newline_style(text).to_string()
}

fn get_unicode_category(ch: char) -> String {
    get_general_category(ch).abbreviation().to_string()
}

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

    #[test]
    fn test_text_position_empty() {
        let result = text_position("", Some(0), None, None, None, None, 1, 1);
        assert!(result.valid);
        assert_eq!(result.byte_offset, Some(0));
    }

    #[test]
    fn test_text_position_simple() {
        let result = text_position("hello", None, Some(0), None, None, None, 1, 1);
        assert!(result.valid);
        assert_eq!(result.line, Some(1));
        assert_eq!(result.column, Some(1));
    }

    #[test]
    fn test_text_position_multiline() {
        let text = "line1\nline2\nline3";
        let result = text_position(text, None, Some(7), None, None, None, 1, 1);
        assert!(result.valid);
        assert_eq!(result.line, Some(2));
        assert_eq!(result.column, Some(2));
    }
}