forked from google/gxui
/
text_selection_list.go
75 lines (62 loc) · 1.73 KB
/
text_selection_list.go
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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gxui
import "github.com/nelsam/gxui/interval"
type TextSelectionList []TextSelection
func (l TextSelectionList) Transform(transform SelectionTransform) TextSelectionList {
res := TextSelectionList{}
for _, s := range l {
interval.Merge(&res, transform(s))
}
return res
}
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 (l TextSelectionList) Len() int {
return len(l)
}
func (l TextSelectionList) Cap() int {
return cap(l)
}
func (l *TextSelectionList) SetLen(length int) {
*l = (*l)[:length]
}
func (l *TextSelectionList) GrowTo(length, capacity int) {
old := *l
*l = make(TextSelectionList, length, capacity)
copy(*l, old)
}
func (l TextSelectionList) Copy(to, from, count int) {
copy(l[to:to+count], l[from:from+count])
}
func (l TextSelectionList) Interval(index int) (start, end uint64) {
return l[index].Span()
}
func (l TextSelectionList) SetInterval(index int, start, end uint64) {
l[index].start = int(start)
l[index].end = int(end)
}
func (l TextSelectionList) MergeData(index int, i interval.Node) {
sel := i.(TextSelection)
l[index].caretAtStart = sel.caretAtStart
l[index].storedStart = sel.storedStart
l[index].storedEnd = sel.storedEnd
}