~eliasnaur/gio

ref: abd6e8f9cdd4 gio/widget/buffer.go -rw-r--r-- 3.6 KiB
abd6e8f9pierre app: support changing Window options at runtime 7 days ago
                                                                                
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
// SPDX-License-Identifier: Unlicense OR MIT

package widget

import (
	"io"
	"strings"
	"unicode/utf8"
)

// editBuffer implements a gap buffer for text editing.
type editBuffer struct {
	// pos is the byte position for Read and ReadRune.
	pos int

	// The gap start and end in bytes.
	gapstart, gapend int
	text             []byte

	// changed tracks whether the buffer content
	// has changed since the last call to Changed.
	changed bool
}

const minSpace = 5

func (e *editBuffer) Changed() bool {
	c := e.changed
	e.changed = false
	return c
}

func (e *editBuffer) deleteRunes(caret, runes int) int {
	e.moveGap(caret, 0)
	for ; runes < 0 && e.gapstart > 0; runes++ {
		_, s := utf8.DecodeLastRune(e.text[:e.gapstart])
		e.gapstart -= s
		caret -= s
		e.changed = e.changed || s > 0
	}
	for ; runes > 0 && e.gapend < len(e.text); runes-- {
		_, s := utf8.DecodeRune(e.text[e.gapend:])
		e.gapend += s
		e.changed = e.changed || s > 0
	}
	return caret
}

// moveGap moves the gap to the caret position. After returning,
// the gap is guaranteed to be at least space bytes long.
func (e *editBuffer) moveGap(caret, space int) {
	if e.gapLen() < space {
		if space < minSpace {
			space = minSpace
		}
		txt := make([]byte, e.len()+space)
		// Expand to capacity.
		txt = txt[:cap(txt)]
		gaplen := len(txt) - e.len()
		if caret > e.gapstart {
			copy(txt, e.text[:e.gapstart])
			copy(txt[caret+gaplen:], e.text[caret:])
			copy(txt[e.gapstart:], e.text[e.gapend:caret+e.gapLen()])
		} else {
			copy(txt, e.text[:caret])
			copy(txt[e.gapstart+gaplen:], e.text[e.gapend:])
			copy(txt[caret+gaplen:], e.text[caret:e.gapstart])
		}
		e.text = txt
		e.gapstart = caret
		e.gapend = e.gapstart + gaplen
	} else {
		if caret > e.gapstart {
			copy(e.text[e.gapstart:], e.text[e.gapend:caret+e.gapLen()])
		} else {
			copy(e.text[caret+e.gapLen():], e.text[caret:e.gapstart])
		}
		l := e.gapLen()
		e.gapstart = caret
		e.gapend = e.gapstart + l
	}
}

func (e *editBuffer) len() int {
	return len(e.text) - e.gapLen()
}

func (e *editBuffer) gapLen() int {
	return e.gapend - e.gapstart
}

func (e *editBuffer) Reset() {
	e.Seek(0, io.SeekStart)
}

// Seek implements io.Seeker
func (e *editBuffer) Seek(offset int64, whence int) (ret int64, err error) {
	switch whence {
	case io.SeekStart:
		e.pos = int(offset)
	case io.SeekCurrent:
		e.pos += int(offset)
	case io.SeekEnd:
		e.pos = e.len() - int(offset)
	}
	if e.pos < 0 {
		e.pos = 0
	} else if e.pos > e.len() {
		e.pos = e.len()
	}
	return int64(e.pos), nil
}

func (e *editBuffer) Read(p []byte) (int, error) {
	if e.pos == e.len() {
		return 0, io.EOF
	}
	var total int
	if e.pos < e.gapstart {
		n := copy(p, e.text[e.pos:e.gapstart])
		p = p[n:]
		total += n
		e.pos += n
	}
	if e.pos >= e.gapstart {
		n := copy(p, e.text[e.pos+e.gapLen():])
		total += n
		e.pos += n
	}
	if e.pos > e.len() {
		panic("hey!")
	}
	return total, nil
}

func (e *editBuffer) ReadRune() (rune, int, error) {
	if e.pos == e.len() {
		return 0, 0, io.EOF
	}
	r, s := e.runeAt(e.pos)
	e.pos += s
	return r, s, nil
}

func (e *editBuffer) String() string {
	var b strings.Builder
	b.Grow(e.len())
	b.Write(e.text[:e.gapstart])
	b.Write(e.text[e.gapend:])
	return b.String()
}

func (e *editBuffer) prepend(caret int, s string) {
	e.moveGap(caret, len(s))
	copy(e.text[caret:], s)
	e.gapstart += len(s)
	e.changed = e.changed || len(s) > 0
}

func (e *editBuffer) runeBefore(idx int) (rune, int) {
	if idx > e.gapstart {
		idx += e.gapLen()
	}
	return utf8.DecodeLastRune(e.text[:idx])
}

func (e *editBuffer) runeAt(idx int) (rune, int) {
	if idx >= e.gapstart {
		idx += e.gapLen()
	}
	return utf8.DecodeRune(e.text[idx:])
}