forked from google/gxui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_selection_list.go
79 lines (66 loc) · 1.79 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
76
77
78
79
// 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/google/gxui/interval"
type TextSelectionList []TextSelection
func (l TextSelectionList) Transform(from int, transform func(i int) int) TextSelectionList {
res := TextSelectionList{}
for _, s := range l {
start := s.start
end := s.end
if start >= from {
start = transform(start)
}
if end >= from {
end = transform(end)
}
interval.Merge(&res, TextSelection{start, end, s.caretAtStart})
}
return res
}
func (l TextSelectionList) TransformCarets(from int, transform func(i int) int) TextSelectionList {
res := TextSelectionList{}
for _, s := range l {
if s.caretAtStart && s.start >= from {
s.start = transform(s.start)
} else if s.end >= from {
s.end = transform(s.end)
}
if s.start > s.end {
tmp := s.start
s.start = s.end
s.end = tmp
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(len int) {
*l = (*l)[:len]
}
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) GetInterval(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) {
l[index].caretAtStart = i.(TextSelection).caretAtStart
}