aetna-core 0.3.3

Aetna — backend-agnostic UI library core
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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
//! Library-level text selection model.
//!
//! Selection is a single, application-owned [`Selection`] value that
//! identifies *which* keyed text-bearing element holds the active
//! selection and *where* in that element's text the anchor and head
//! sit. The library enforces the single-selection invariant by
//! emitting `SelectionChanged` events; the app folds them into its
//! `Selection` field the same way it folds `apply_event` results into
//! a [`crate::widgets::text_input::TextSelection`] today.
//!
//! # Model
//!
//! - [`Selection`] — the slot, holds an `Option<SelectionRange>`.
//! - [`SelectionRange`] — anchor + head, both [`SelectionPoint`].
//! - [`SelectionPoint`] — `(key, byte)`. The key references the same
//!   widget-key form that `focus_order` already uses; the byte indexes
//!   into that element's text content.
//!
//! When `anchor.key == head.key` the selection lives entirely inside
//! one leaf — equivalent to a [`crate::widgets::text_input::TextSelection`]
//! over that leaf's text. When they differ, the selection spans
//! multiple leaves in document order.
//!
//! # Key requirement
//!
//! Selectable leaves must carry an explicit `.key(...)` — same
//! convention as focusable widgets. Without a key the leaf is silently
//! excluded from `selection_order` because nothing could survive a
//! tree rebuild as a stable identity. See [`crate::tree::El::selectable`].

use std::ops::Range;

use crate::tree::{El, Kind};
use crate::widgets::text_input::TextSelection;

/// The application's single selection slot. `None` means nothing is
/// selected. The library emits `SelectionChanged` events that fold
/// into this; widgets read it back to draw highlight bands and route
/// editing operations.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Selection {
    pub range: Option<SelectionRange>,
}

/// A non-empty selection range. `anchor` is where the user started
/// (pointer-down); `head` is where they ended up (pointer current /
/// last move). The pair may be in tree order or reversed.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SelectionRange {
    pub anchor: SelectionPoint,
    pub head: SelectionPoint,
}

/// A point inside a selectable leaf's text content. `key` is the
/// widget key that owns the leaf; `byte` is a byte offset into that
/// leaf's text (clamped to a UTF-8 char boundary by anything that
/// reads or writes it).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SelectionPoint {
    pub key: String,
    pub byte: usize,
}

impl SelectionPoint {
    pub fn new(key: impl Into<String>, byte: usize) -> Self {
        Self {
            key: key.into(),
            byte,
        }
    }
}

/// Source-backed copy/hit-test payload for a selectable rich-text
/// node.
///
/// `visible` is the logical text users point at while selecting;
/// `source` is what copy should return. `spans` maps byte ranges in
/// `visible` to byte ranges in `source`. A plain text leaf has one
/// identity span. Markdown can instead map rendered words to their
/// original markdown source, and atomic embeds such as math can map a
/// one-byte object slot to the full `$...$` / `$$...$$` source.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct SelectionSource {
    pub source: String,
    pub visible: String,
    pub spans: Vec<SelectionSourceSpan>,
    pub full_selection_group: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SelectionSourceSpan {
    pub visible: Range<usize>,
    pub source: Range<usize>,
    pub source_full: Range<usize>,
    pub atomic: bool,
}

impl SelectionSource {
    pub fn new(source: impl Into<String>, visible: impl Into<String>) -> Self {
        Self {
            source: source.into(),
            visible: visible.into(),
            spans: Vec::new(),
            full_selection_group: None,
        }
    }

    pub fn identity(text: impl Into<String>) -> Self {
        let text = text.into();
        let len = text.len();
        Self {
            source: text.clone(),
            visible: text,
            spans: vec![SelectionSourceSpan {
                visible: 0..len,
                source: 0..len,
                source_full: 0..len,
                atomic: false,
            }],
            full_selection_group: None,
        }
    }

    pub fn full_selection_group(mut self, group: impl Into<String>) -> Self {
        self.full_selection_group = Some(group.into());
        self
    }

    pub fn push_span(&mut self, visible: Range<usize>, source: Range<usize>, atomic: bool) {
        self.push_span_with_full_source(visible, source.clone(), source, atomic);
    }

    pub fn push_span_with_full_source(
        &mut self,
        visible: Range<usize>,
        source: Range<usize>,
        source_full: Range<usize>,
        atomic: bool,
    ) {
        if visible.start <= visible.end
            && visible.end <= self.visible.len()
            && source.start <= source.end
            && source.end <= self.source.len()
            && source_full.start <= source_full.end
            && source_full.end <= self.source.len()
        {
            self.spans.push(SelectionSourceSpan {
                visible,
                source,
                source_full,
                atomic,
            });
        }
    }

    pub fn visible_len(&self) -> usize {
        self.visible.len()
    }

    pub fn source_slice_for_visible(&self, a: usize, b: usize) -> Option<&str> {
        let (a, b) = (a.min(b), a.max(b));
        if a == 0 && b >= self.visible.len() && !self.source.is_empty() {
            return Some(&self.source);
        }
        let a = clamp_to_char_boundary(&self.visible, a.min(self.visible.len()));
        let b = clamp_to_char_boundary(&self.visible, b.min(self.visible.len()));
        let lo = self.source_offset_for_visible(a, Bias::Start)?;
        let hi = self.source_offset_for_visible(b, Bias::End)?;
        let (lo, hi) = (lo.min(hi), lo.max(hi));
        let lo = clamp_to_char_boundary(&self.source, lo.min(self.source.len()));
        let hi = clamp_to_char_boundary(&self.source, hi.min(self.source.len()));
        (lo < hi).then(|| &self.source[lo..hi])
    }

    pub fn source_text_for_visible(&self, a: usize, b: usize) -> Option<String> {
        let (a, b) = (a.min(b), a.max(b));
        if a == 0 && b >= self.visible.len() && !self.source.is_empty() {
            return Some(self.source.clone());
        }
        let a = clamp_to_char_boundary(&self.visible, a.min(self.visible.len()));
        let b = clamp_to_char_boundary(&self.visible, b.min(self.visible.len()));
        if a >= b {
            return None;
        }
        if self.spans.is_empty() {
            return self.source_slice_for_visible(a, b).map(str::to_string);
        }

        let mut out = String::new();
        for span in &self.spans {
            let start = a.max(span.visible.start);
            let end = b.min(span.visible.end);
            if start >= end {
                continue;
            }
            if span.atomic || (start == span.visible.start && end == span.visible.end) {
                out.push_str(&self.source[span.source_full.clone()]);
                continue;
            }
            let lo = source_offset_in_span(span, start, Bias::Start)?;
            let hi = source_offset_in_span(span, end, Bias::End)?;
            let (lo, hi) = (lo.min(hi), lo.max(hi));
            let lo = clamp_to_char_boundary(&self.source, lo.min(self.source.len()));
            let hi = clamp_to_char_boundary(&self.source, hi.min(self.source.len()));
            if lo < hi {
                out.push_str(&self.source[lo..hi]);
            }
        }
        if out.is_empty() { None } else { Some(out) }
    }

    fn full_group_for_visible(&self, start: usize, end: usize) -> Option<&str> {
        (start == 0 && end >= self.visible.len())
            .then_some(self.full_selection_group.as_deref())
            .flatten()
    }

    fn source_offset_for_visible(&self, byte: usize, bias: Bias) -> Option<usize> {
        if self.spans.is_empty() {
            return Some(byte.min(self.source.len()));
        }
        for span in &self.spans {
            if byte < span.visible.start || byte > span.visible.end {
                continue;
            }
            if byte == span.visible.end && byte != span.visible.start && matches!(bias, Bias::Start)
            {
                continue;
            }
            if span.atomic {
                return Some(match bias {
                    Bias::Start => span.source.start,
                    Bias::End => span.source.end,
                });
            }
            let visible_len = span.visible.end.saturating_sub(span.visible.start);
            let source_len = span.source.end.saturating_sub(span.source.start);
            if visible_len == 0 {
                return Some(match bias {
                    Bias::Start => span.source.start,
                    Bias::End => span.source.end,
                });
            }
            let offset = byte.saturating_sub(span.visible.start).min(visible_len);
            let mapped = if source_len == visible_len {
                span.source.start + offset
            } else {
                span.source.start
                    + ((offset as f32 / visible_len as f32) * source_len as f32) as usize
            };
            return Some(mapped.min(span.source.end));
        }
        let first = self.spans.first()?;
        if byte <= first.visible.start {
            return Some(first.source.start);
        }
        let last = self.spans.last()?;
        if byte >= last.visible.end {
            return Some(last.source.end);
        }
        self.spans
            .windows(2)
            .find(|pair| byte > pair[0].visible.end && byte < pair[1].visible.start)
            .map(|pair| match bias {
                Bias::Start => pair[0].source.end,
                Bias::End => pair[1].source.start,
            })
    }
}

fn source_offset_in_span(span: &SelectionSourceSpan, byte: usize, bias: Bias) -> Option<usize> {
    if span.atomic {
        return Some(match bias {
            Bias::Start => span.source_full.start,
            Bias::End => span.source_full.end,
        });
    }
    let visible_len = span.visible.end.saturating_sub(span.visible.start);
    let source_len = span.source.end.saturating_sub(span.source.start);
    if visible_len == 0 {
        return Some(match bias {
            Bias::Start => span.source.start,
            Bias::End => span.source.end,
        });
    }
    let offset = byte.saturating_sub(span.visible.start).min(visible_len);
    let mapped = if source_len == visible_len {
        span.source.start + offset
    } else {
        span.source.start + ((offset as f32 / visible_len as f32) * source_len as f32) as usize
    };
    Some(mapped.min(span.source.end))
}

#[derive(Clone, Copy)]
enum Bias {
    Start,
    End,
}

impl Selection {
    /// A collapsed caret at `(key, byte)`. Convenience for tests and
    /// app-side initialization.
    pub fn caret(key: impl Into<String>, byte: usize) -> Self {
        let pt = SelectionPoint::new(key, byte);
        Self {
            range: Some(SelectionRange {
                anchor: pt.clone(),
                head: pt,
            }),
        }
    }

    /// True when there is no active selection.
    pub fn is_empty(&self) -> bool {
        self.range.is_none()
    }

    /// True when the selection lives entirely inside `key` — both
    /// anchor and head reference it. False for cross-element
    /// selections and for the empty selection.
    pub fn is_within(&self, key: &str) -> bool {
        match &self.range {
            Some(r) => r.anchor.key == key && r.head.key == key,
            None => false,
        }
    }

    /// True when `key` is the anchor's key (the originating leaf).
    pub fn anchored_at(&self, key: &str) -> bool {
        self.range.as_ref().is_some_and(|r| r.anchor.key == key)
    }

    /// View the selection through one leaf's lens: returns
    /// `Some(TextSelection)` only when the selection lives entirely
    /// inside `key`. Cross-element selections return `None` here —
    /// callers that need a per-leaf slice for a spanned leaf should
    /// instead consult the document-order range.
    pub fn within(&self, key: &str) -> Option<TextSelection> {
        let r = self.range.as_ref()?;
        if r.anchor.key == key && r.head.key == key {
            Some(TextSelection {
                anchor: r.anchor.byte,
                head: r.head.byte,
            })
        } else {
            None
        }
    }

    /// Replace this selection's slice for `key` from a freshly
    /// produced [`TextSelection`]. Used by editable widgets after
    /// folding an event: take the slice via [`Self::within`], let the
    /// widget mutate it, and write it back. No-op when the selection
    /// isn't currently within `key`.
    pub fn set_within(&mut self, key: &str, sel: TextSelection) {
        let Some(r) = self.range.as_mut() else { return };
        if r.anchor.key == key && r.head.key == key {
            r.anchor.byte = sel.anchor;
            r.head.byte = sel.head;
        }
    }

    /// Clear the selection.
    pub fn clear(&mut self) {
        self.range = None;
    }
}

/// Compute the byte range within `key`'s text that should be
/// highlighted, given the current `selection` and the document-order
/// list of selectable leaves. Returns `None` when `key` isn't part
/// of the selection range.
///
/// The painter calls this for each selectable leaf to decide whether
/// (and where) to draw a highlight band:
///
/// - Single-leaf selection: returns the `(lo, hi)` byte range when
///   `key` matches both endpoints.
/// - Anchor leaf (in cross-leaf): returns `(anchor.byte, text_len)`
///   for the leaf where the drag started.
/// - Head leaf (in cross-leaf): returns `(0, head.byte)` for the
///   leaf where the drag currently ends.
/// - Middle leaf: returns `(0, text_len)` — fully selected.
///
/// Anchor / head are normalized to document order using
/// `order` (keys in tree order, e.g. `selection_order` from
/// [`crate::state::UiState::selection_order`]).
pub fn slice_for_leaf(
    selection: &Selection,
    order: &[crate::event::UiTarget],
    key: &str,
    text_len: usize,
) -> Option<(usize, usize)> {
    let r = selection.range.as_ref()?;
    if r.anchor.key == r.head.key {
        if r.anchor.key != key {
            return None;
        }
        let (lo, hi) = (
            r.anchor.byte.min(r.head.byte).min(text_len),
            r.anchor.byte.max(r.head.byte).min(text_len),
        );
        return (lo < hi).then_some((lo, hi));
    }
    let pos = |k: &str| order.iter().position(|t| t.key == k);
    let (a_idx, h_idx, key_idx) = (pos(&r.anchor.key)?, pos(&r.head.key)?, pos(key)?);
    let (lo_idx, lo_byte, hi_idx, hi_byte) = if a_idx <= h_idx {
        (a_idx, r.anchor.byte, h_idx, r.head.byte)
    } else {
        (h_idx, r.head.byte, a_idx, r.anchor.byte)
    };
    if key_idx < lo_idx || key_idx > hi_idx {
        return None;
    }
    let lo = if key_idx == lo_idx {
        lo_byte.min(text_len)
    } else {
        0
    };
    let hi = if key_idx == hi_idx {
        hi_byte.min(text_len)
    } else {
        text_len
    };
    (lo < hi).then_some((lo, hi))
}

/// Walk `tree` and return the substring covered by `selection`.
/// Returns `None` for an empty selection or when the selection
/// references a key with no matching keyed text leaf in the tree.
///
/// For single-leaf selections (the only kind P1a renders) the
/// returned string is `value[lo..hi]` for that leaf. Cross-leaf
/// selections walk in tree order: anchor leaf from anchor.byte to
/// end, every leaf strictly between anchor and head fully, head leaf
/// up to head.byte. Joined with `\n` between leaves.
pub fn selected_text(tree: &El, selection: &Selection) -> Option<String> {
    let r = selection.range.as_ref()?;
    if r.anchor.key == r.head.key {
        if let Some(source) = find_keyed_selection_source(tree, &r.anchor.key) {
            let lo = r.anchor.byte.min(r.head.byte);
            let hi = r.anchor.byte.max(r.head.byte);
            return source.source_text_for_visible(lo, hi);
        }
        let value = find_keyed_text(tree, &r.anchor.key)?;
        let lo = r.anchor.byte.min(r.head.byte).min(value.len());
        let hi = r.anchor.byte.max(r.head.byte).min(value.len());
        if lo >= hi {
            return None;
        }
        return Some(value[lo..hi].to_string());
    }
    // Cross-leaf walk in tree order.
    let mut leaves: Vec<(String, LeafSelectionText)> = Vec::new();
    collect_keyed_selection_leaves(tree, &mut leaves);
    let anchor_idx = leaves.iter().position(|(k, _)| *k == r.anchor.key)?;
    let head_idx = leaves.iter().position(|(k, _)| *k == r.head.key)?;
    let (lo_idx, lo_byte, hi_idx, hi_byte) = if anchor_idx <= head_idx {
        (anchor_idx, r.anchor.byte, head_idx, r.head.byte)
    } else {
        (head_idx, r.head.byte, anchor_idx, r.anchor.byte)
    };
    let mut out = String::new();
    let mut last_group: Option<String> = None;
    for (i, (_, value)) in leaves
        .iter()
        .enumerate()
        .skip(lo_idx)
        .take(hi_idx - lo_idx + 1)
    {
        let start = if i == lo_idx {
            lo_byte.min(value.visible_len())
        } else {
            0
        };
        let end = if i == hi_idx {
            hi_byte.min(value.visible_len())
        } else {
            value.visible_len()
        };
        if start >= end {
            continue;
        }
        let Some(slice) = value.source_text_for_visible(start, end) else {
            continue;
        };
        let group = value.full_group_for_visible(start, end).map(str::to_string);
        if group.is_some() && group == last_group {
            continue;
        }
        if !out.is_empty() {
            out.push('\n');
        }
        out.push_str(&slice);
        last_group = group;
    }
    if out.is_empty() { None } else { Some(out) }
}

pub(crate) fn find_keyed_text(node: &El, key: &str) -> Option<String> {
    if node.key.as_deref() == Some(key) {
        if let Some(source) = &node.selection_source {
            return Some(source.visible.clone());
        }
        if matches!(node.kind, Kind::Text | Kind::Heading)
            && let Some(t) = &node.text
        {
            return Some(t.clone());
        }
        let mut out = String::new();
        collect_text_content(node, &mut out);
        if !out.is_empty() {
            return Some(out);
        }
    }
    node.children.iter().find_map(|c| find_keyed_text(c, key))
}

pub(crate) fn find_keyed_selection_source(node: &El, key: &str) -> Option<SelectionSource> {
    if node.key.as_deref() == Some(key)
        && let Some(source) = &node.selection_source
    {
        return Some(source.clone());
    }
    node.children
        .iter()
        .find_map(|c| find_keyed_selection_source(c, key))
}

fn collect_text_content(node: &El, out: &mut String) {
    if matches!(node.kind, Kind::Text | Kind::Heading)
        && let Some(t) = &node.text
    {
        out.push_str(t);
    }
    for c in &node.children {
        collect_text_content(c, out);
    }
}

enum LeafSelectionText {
    Source(SelectionSource),
    Text(String),
}

impl LeafSelectionText {
    fn visible_len(&self) -> usize {
        match self {
            LeafSelectionText::Source(source) => source.visible_len(),
            LeafSelectionText::Text(text) => text.len(),
        }
    }

    fn source_text_for_visible(&self, start: usize, end: usize) -> Option<String> {
        match self {
            LeafSelectionText::Source(source) => source.source_text_for_visible(start, end),
            LeafSelectionText::Text(text) => {
                let start = start.min(text.len());
                let end = end.min(text.len());
                (start < end).then(|| text[start..end].to_string())
            }
        }
    }

    fn full_group_for_visible(&self, start: usize, end: usize) -> Option<&str> {
        match self {
            LeafSelectionText::Source(source) => source.full_group_for_visible(start, end),
            LeafSelectionText::Text(_) => None,
        }
    }
}

fn collect_keyed_selection_leaves(node: &El, out: &mut Vec<(String, LeafSelectionText)>) {
    if let (Some(k), Some(source)) = (&node.key, &node.selection_source) {
        out.push((k.clone(), LeafSelectionText::Source(source.clone())));
        return;
    }
    if matches!(node.kind, Kind::Text | Kind::Heading)
        && let (Some(k), Some(t)) = (&node.key, &node.text)
    {
        out.push((k.clone(), LeafSelectionText::Text(t.clone())));
    }
    for c in &node.children {
        collect_keyed_selection_leaves(c, out);
    }
}

/// Word range containing `byte`, returned as `(lo, hi)` byte offsets
/// into `text`. A *word* is a maximal run of `is_word_char` scalars
/// (alphanumeric, `_`, or `'`); when `byte` lands on a non-word
/// character the result is just that single codepoint, matching the
/// browser convention where double-clicking a punctuation mark
/// selects only that mark rather than the surrounding whitespace.
/// Used for double-click word selection.
///
/// `byte` is clamped to a UTF-8 char boundary; positions inside a
/// multi-byte codepoint snap to the previous boundary. An empty
/// `text` returns `(0, 0)`.
pub fn word_range_at(text: &str, byte: usize) -> (usize, usize) {
    if text.is_empty() {
        return (0, 0);
    }
    let byte = clamp_to_char_boundary(text, byte.min(text.len()));
    // At the very end of the text, point at the previous codepoint so
    // double-click after the last word still selects that word rather
    // than collapsing to (len, len).
    let probe = if byte == text.len() {
        prev_char_boundary(text, byte)
    } else {
        byte
    };
    let probe_char = text[probe..].chars().next().unwrap_or(' ');
    if !is_word_char(probe_char) {
        // Non-word char → select just this codepoint. Avoids the
        // awkward "comma + space" double-select that grouping would
        // produce.
        return (probe, probe + probe_char.len_utf8());
    }

    // Word char → expand left and right through the run.
    let mut lo = probe;
    while lo > 0 {
        let p = prev_char_boundary(text, lo);
        let ch = text[p..].chars().next().unwrap();
        if !is_word_char(ch) {
            break;
        }
        lo = p;
    }
    let mut hi = probe;
    while hi < text.len() {
        let ch = text[hi..].chars().next().unwrap();
        if !is_word_char(ch) {
            break;
        }
        hi += ch.len_utf8();
    }
    (lo, hi)
}

/// Line range containing `byte`, returned as `(lo, hi)` byte offsets
/// into `text`. The range excludes the trailing `\n` so the matching
/// substring renders the visible line. An empty text returns `(0, 0)`.
/// Used for triple-click line selection.
pub fn line_range_at(text: &str, byte: usize) -> (usize, usize) {
    let byte = byte.min(text.len());
    let lo = text[..byte].rfind('\n').map(|i| i + 1).unwrap_or(0);
    let hi = text[byte..]
        .find('\n')
        .map(|i| byte + i)
        .unwrap_or(text.len());
    (lo, hi)
}

fn is_word_char(c: char) -> bool {
    c.is_alphanumeric() || c == '_' || c == '\''
}

fn clamp_to_char_boundary(text: &str, byte: usize) -> usize {
    let mut b = byte;
    while b > 0 && !text.is_char_boundary(b) {
        b -= 1;
    }
    b
}

fn prev_char_boundary(text: &str, byte: usize) -> usize {
    let mut b = byte.saturating_sub(1);
    while b > 0 && !text.is_char_boundary(b) {
        b -= 1;
    }
    b
}

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

    #[test]
    fn empty_selection_has_no_views() {
        let sel = Selection::default();
        assert!(sel.is_empty());
        assert!(!sel.is_within("name"));
        assert!(sel.within("name").is_none());
    }

    #[test]
    fn caret_constructor_is_within_its_key() {
        let sel = Selection::caret("name", 3);
        assert!(!sel.is_empty());
        assert!(sel.is_within("name"));
        assert!(!sel.is_within("email"));
        let view = sel.within("name").expect("within name");
        assert_eq!(view, TextSelection::caret(3));
    }

    #[test]
    fn within_returns_none_for_cross_element_selection() {
        let sel = Selection {
            range: Some(SelectionRange {
                anchor: SelectionPoint::new("para_a", 0),
                head: SelectionPoint::new("para_b", 5),
            }),
        };
        // Cross-element: neither lens reveals the full selection.
        assert!(sel.within("para_a").is_none());
        assert!(sel.within("para_b").is_none());
        // But the originating-leaf check still works.
        assert!(sel.anchored_at("para_a"));
        assert!(!sel.anchored_at("para_b"));
    }

    #[test]
    fn set_within_writes_back_a_modified_slice() {
        let mut sel = Selection::caret("name", 0);
        let mut view = sel.within("name").expect("caret");
        view.head = 5; // simulate widget editing the slice
        sel.set_within("name", view);
        let view_back = sel.within("name").expect("still within name");
        assert_eq!(view_back, TextSelection::range(0, 5));
    }

    #[test]
    fn set_within_is_a_noop_when_selection_is_not_in_key() {
        let mut sel = Selection::caret("name", 0);
        sel.set_within("email", TextSelection::range(0, 9));
        // Selection unchanged.
        assert_eq!(sel.within("name"), Some(TextSelection::caret(0)));
        assert!(sel.within("email").is_none());
    }

    #[test]
    fn selected_text_returns_single_leaf_substring() {
        let tree = crate::widgets::text::text("Hello, world!").key("p");
        let sel = Selection {
            range: Some(SelectionRange {
                anchor: SelectionPoint::new("p", 7),
                head: SelectionPoint::new("p", 12),
            }),
        };
        assert_eq!(selected_text(&tree, &sel).as_deref(), Some("world"));
    }

    #[test]
    fn selected_text_reads_text_inside_keyed_composite_widget() {
        let sel = Selection {
            range: Some(SelectionRange {
                anchor: SelectionPoint::new("name", 1),
                head: SelectionPoint::new("name", 4),
            }),
        };
        let tree = crate::widgets::text_input::text_input("hello", &sel, "name");
        assert_eq!(selected_text(&tree, &sel).as_deref(), Some("ell"));
    }

    #[test]
    fn selected_text_walks_tree_order_for_cross_leaf_selection() {
        let tree = crate::column([
            crate::widgets::text::text("alpha").key("a"),
            crate::widgets::text::text("bravo").key("b"),
            crate::widgets::text::text("charlie").key("c"),
        ]);
        // Anchor inside "alpha" at byte 2, head inside "charlie" at
        // byte 4 — should yield "pha\nbravo\nchar" (joined by newline
        // between leaves; full middle leaf included).
        let sel = Selection {
            range: Some(SelectionRange {
                anchor: SelectionPoint::new("a", 2),
                head: SelectionPoint::new("c", 4),
            }),
        };
        assert_eq!(
            selected_text(&tree, &sel).as_deref(),
            Some("pha\nbravo\nchar")
        );
    }

    #[test]
    fn selected_text_uses_source_payload_for_single_leaf() {
        let mut source = SelectionSource::new("This is **bold**.", "This is bold.");
        source.push_span(0..8, 0..8, false);
        source.push_span_with_full_source(8..12, 10..14, 8..16, false);
        source.push_span(12..13, 16..17, false);
        let tree = crate::text_runs([crate::text("This is "), crate::text("bold").bold()])
            .key("md:p")
            .selectable()
            .selection_source(source);

        let inner_only = Selection {
            range: Some(SelectionRange {
                anchor: SelectionPoint::new("md:p", 8),
                head: SelectionPoint::new("md:p", 12),
            }),
        };
        assert_eq!(
            selected_text(&tree, &inner_only).as_deref(),
            Some("**bold**")
        );

        let partial_inner = Selection {
            range: Some(SelectionRange {
                anchor: SelectionPoint::new("md:p", 9),
                head: SelectionPoint::new("md:p", 11),
            }),
        };
        assert_eq!(selected_text(&tree, &partial_inner).as_deref(), Some("ol"));

        let through_styled_span = Selection {
            range: Some(SelectionRange {
                anchor: SelectionPoint::new("md:p", 0),
                head: SelectionPoint::new("md:p", 12),
            }),
        };
        assert_eq!(
            selected_text(&tree, &through_styled_span).as_deref(),
            Some("This is **bold**")
        );

        let whole = Selection {
            range: Some(SelectionRange {
                anchor: SelectionPoint::new("md:p", 0),
                head: SelectionPoint::new("md:p", 13),
            }),
        };
        assert_eq!(
            selected_text(&tree, &whole).as_deref(),
            Some("This is **bold**.")
        );
    }

    #[test]
    fn selected_text_dedupes_adjacent_full_source_group_leaves() {
        let mut first = SelectionSource::new("| **Ada** | dev |", "Ada");
        first.push_span_with_full_source(0..3, 4..7, 0..17, false);
        let first = first.full_selection_group("row:0");

        let mut second = SelectionSource::new("| **Ada** | dev |", "dev");
        second.push_span_with_full_source(0..3, 12..15, 0..17, false);
        let second = second.full_selection_group("row:0");

        let tree = crate::row([
            crate::text("Ada")
                .key("a")
                .selectable()
                .selection_source(first),
            crate::text("dev")
                .key("b")
                .selectable()
                .selection_source(second),
        ]);
        let sel = Selection {
            range: Some(SelectionRange {
                anchor: SelectionPoint::new("a", 0),
                head: SelectionPoint::new("b", 3),
            }),
        };

        assert_eq!(
            selected_text(&tree, &sel).as_deref(),
            Some("| **Ada** | dev |")
        );
    }

    #[test]
    fn slice_for_leaf_single_leaf() {
        let order = order_for(&["a", "b", "c"]);
        let sel = Selection {
            range: Some(SelectionRange {
                anchor: SelectionPoint::new("b", 2),
                head: SelectionPoint::new("b", 5),
            }),
        };
        assert_eq!(slice_for_leaf(&sel, &order, "b", 10), Some((2, 5)));
        assert_eq!(slice_for_leaf(&sel, &order, "a", 10), None);
        assert_eq!(slice_for_leaf(&sel, &order, "c", 10), None);
    }

    #[test]
    fn slice_for_leaf_cross_leaf_anchor_to_head_in_doc_order() {
        // anchor = a@2, head = c@4: spans a, b, c.
        let order = order_for(&["a", "b", "c"]);
        let sel = Selection {
            range: Some(SelectionRange {
                anchor: SelectionPoint::new("a", 2),
                head: SelectionPoint::new("c", 4),
            }),
        };
        assert_eq!(
            slice_for_leaf(&sel, &order, "a", 10),
            Some((2, 10)),
            "anchor leaf: from anchor.byte to text_len"
        );
        assert_eq!(
            slice_for_leaf(&sel, &order, "b", 8),
            Some((0, 8)),
            "middle leaf: fully selected"
        );
        assert_eq!(
            slice_for_leaf(&sel, &order, "c", 10),
            Some((0, 4)),
            "head leaf: from 0 to head.byte"
        );
    }

    #[test]
    fn slice_for_leaf_cross_leaf_reversed_drag() {
        // anchor in c (later), head in a (earlier) — order shouldn't
        // matter; the slice is the same as forward drag.
        let order = order_for(&["a", "b", "c"]);
        let sel = Selection {
            range: Some(SelectionRange {
                anchor: SelectionPoint::new("c", 3),
                head: SelectionPoint::new("a", 1),
            }),
        };
        // Forward in doc order: a@1..end, b full, c 0..3.
        assert_eq!(slice_for_leaf(&sel, &order, "a", 5), Some((1, 5)));
        assert_eq!(slice_for_leaf(&sel, &order, "b", 6), Some((0, 6)));
        assert_eq!(slice_for_leaf(&sel, &order, "c", 9), Some((0, 3)));
    }

    #[test]
    fn slice_for_leaf_returns_none_for_leaves_outside_range() {
        // 5-leaf order; selection covers only b..d.
        let order = order_for(&["a", "b", "c", "d", "e"]);
        let sel = Selection {
            range: Some(SelectionRange {
                anchor: SelectionPoint::new("b", 0),
                head: SelectionPoint::new("d", 0),
            }),
        };
        assert_eq!(slice_for_leaf(&sel, &order, "a", 10), None);
        assert_eq!(slice_for_leaf(&sel, &order, "e", 10), None);
        // Boundary leaves with collapsed endpoints: anchor at b@0
        // means b's slice is (0, len). head at d@0 means d's slice is
        // (0, 0) which collapses → None.
        assert_eq!(slice_for_leaf(&sel, &order, "b", 4), Some((0, 4)));
        assert_eq!(slice_for_leaf(&sel, &order, "c", 7), Some((0, 7)));
        assert_eq!(slice_for_leaf(&sel, &order, "d", 5), None);
    }

    fn order_for(keys: &[&str]) -> Vec<crate::event::UiTarget> {
        keys.iter()
            .map(|k| crate::event::UiTarget {
                key: (*k).to_string(),
                node_id: format!("root.{k}"),
                rect: crate::tree::Rect::new(0.0, 0.0, 0.0, 0.0),
                tooltip: None,
                scroll_offset_y: 0.0,
            })
            .collect()
    }

    #[test]
    fn selected_text_returns_none_for_empty_or_unknown_keys() {
        let tree = crate::widgets::text::text("hi").key("p");
        assert!(selected_text(&tree, &Selection::default()).is_none());
        let unknown = Selection::caret("missing", 0);
        assert!(selected_text(&tree, &unknown).is_none());
    }

    #[test]
    fn word_range_at_picks_run_around_byte() {
        let text = "Hello, world!";
        // Byte 0 in "Hello" → whole word.
        assert_eq!(word_range_at(text, 0), (0, 5));
        // Byte 3 (inside "Hello") → whole word.
        assert_eq!(word_range_at(text, 3), (0, 5));
        // Byte 5 (the comma) → run of non-word chars (just ",").
        assert_eq!(word_range_at(text, 5), (5, 6));
        // Byte 6 (the space) → run of non-word chars (just " ").
        assert_eq!(word_range_at(text, 6), (6, 7));
        // Byte 7 (start of "world") → "world".
        assert_eq!(word_range_at(text, 7), (7, 12));
        // Byte 12 ("!") → "!".
        assert_eq!(word_range_at(text, 12), (12, 13));
    }

    #[test]
    fn word_range_at_treats_apostrophe_and_underscore_as_word_chars() {
        // Contractions stay one word.
        assert_eq!(word_range_at("don't stop", 2), (0, 5));
        // Identifier-style.
        assert_eq!(word_range_at("foo_bar baz", 4), (0, 7));
    }

    #[test]
    fn word_range_at_handles_end_of_text_and_empty() {
        let text = "hello";
        // Byte at len → snaps back into the trailing word.
        assert_eq!(word_range_at(text, 5), (0, 5));
        // Empty text → (0, 0).
        assert_eq!(word_range_at("", 0), (0, 0));
    }

    #[test]
    fn word_range_at_clamps_off_utf8_boundary() {
        // 'é' is two bytes; byte=1 sits inside the codepoint and snaps
        // back to byte 0, then expands into the run of non-ASCII word chars.
        let text = "café";
        let (lo, hi) = word_range_at(text, 1);
        assert_eq!((lo, hi), (0, text.len()));
    }

    #[test]
    fn line_range_at_returns_line_around_byte() {
        let text = "first\nsecond line\nthird";
        // First line: bytes 0..5 ("first"), \n at byte 5.
        assert_eq!(line_range_at(text, 0), (0, 5));
        assert_eq!(line_range_at(text, 3), (0, 5));
        assert_eq!(line_range_at(text, 5), (0, 5));
        // Second line: bytes 6..17 ("second line"), \n at byte 17.
        assert_eq!(line_range_at(text, 6), (6, 17));
        assert_eq!(line_range_at(text, 12), (6, 17));
        assert_eq!(line_range_at(text, 17), (6, 17));
        // Third (final, no trailing \n) line: bytes 18..23.
        assert_eq!(line_range_at(text, 18), (18, 23));
        assert_eq!(line_range_at(text, 23), (18, 23));
    }

    #[test]
    fn line_range_at_handles_empty_and_single_line() {
        assert_eq!(line_range_at("", 0), (0, 0));
        assert_eq!(line_range_at("just one line", 4), (0, 13));
    }
}