func (l TextSelectionList) Transform(transform SelectionTransform) TextSelectionList { res := TextSelectionList{} for _, s := range l { interval.Merge(&res, transform(s)) } return res }
func (t *TextBoxController) updateSelectionsForEdits(edits []TextBoxEdit) { min := 0 max := len(t.text) selections := TextSelectionList{} for _, selection := range t.selections { for _, e := range edits { start := e.At if e.Delta < 0 { start -= e.Delta } delta := e.Delta if selection.start >= start { selection.start += delta } if selection.end >= start { selection.end += delta } } if selection.end < selection.start { selection.end = selection.start } selection.start = math.Clamp(selection.start, min, max) selection.end = math.Clamp(selection.end, min, max) selection = selection.Store() interval.Merge(&selections, selection) } t.selections = selections }
func (t *TextBoxController) AddCarets(transform SelectionTransform) { t.storeCaretLocationsNextEdit = true up := t.selections.Transform(transform) for _, s := range up { interval.Merge(&t.selections, s) } t.onSelectionChanged.Fire() }
func TestTextSelectionMergeExtendEnd(t *testing.T) { s1 := CreateTextSelection(6, 9, true) s2 := CreateTextSelection(8, 15, false) l := TextSelectionList{s1} interval.Merge(&l, s2) test.AssertEquals(t, TextSelectionList{ CreateTextSelection(6, 15, false), }, l) }
func TestTextSelectionMergeDuplicate0Len(t *testing.T) { s1 := CreateTextSelection(2, 2, false) s2 := CreateTextSelection(2, 2, true) l := TextSelectionList{s1} interval.Merge(&l, s2) test.AssertEquals(t, TextSelectionList{ CreateTextSelection(2, 2, true), }, l) }
func TestTextSelectionMergeEncompass(t *testing.T) { s1 := CreateTextSelection(6, 9, false) s2 := CreateTextSelection(5, 10, true) l := TextSelectionList{s1} interval.Merge(&l, s2) test.AssertEquals(t, TextSelectionList{ CreateTextSelection(5, 10, true), }, l) }
func TestTextSelectionMergeBeforeStart(t *testing.T) { s1 := CreateTextSelection(6, 9, true) s2 := CreateTextSelection(2, 6, false) l := TextSelectionList{s1} interval.Merge(&l, s2) test.AssertEquals(t, TextSelectionList{ CreateTextSelection(2, 6, false), CreateTextSelection(6, 9, true), }, l) }
func (l TextSelectionList) TransformCarets(transform SelectionTransform) TextSelectionList { res := TextSelectionList{} for _, s := range l { moved := transform(s) if s.caretAtStart { s.start = moved.start s.storedStart = moved.storedStart } else { s.end = moved.end s.storedEnd = moved.storedEnd } if s.start > s.end { s.start, s.end = s.end, s.start s.caretAtStart = !s.caretAtStart } interval.Merge(&res, s) } return res }
func TestTextSelectionMergeOne(t *testing.T) { s := CreateTextSelection(5, 10, true) l := TextSelectionList{} interval.Merge(&l, s) test.AssertEquals(t, TextSelectionList{s}, l) }
func (t *TextBoxController) AddSelection(s TextSelection) { t.storeCaretLocationsNextEdit = true interval.Merge(&t.selections, s) t.onSelectionChanged.Fire() }