forked from joejulian/gio
widget: take deleted runes into account when applying Editor.MaxLen
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+2
-2
@@ -1262,11 +1262,12 @@ func (e *Editor) replace(start, end int, s string, addHistory bool) int {
|
|||||||
startPos := e.closestPosition(combinedPos{runes: start})
|
startPos := e.closestPosition(combinedPos{runes: start})
|
||||||
endPos := e.closestPosition(combinedPos{runes: end})
|
endPos := e.closestPosition(combinedPos{runes: end})
|
||||||
startOff := e.runeOffset(startPos.runes)
|
startOff := e.runeOffset(startPos.runes)
|
||||||
|
replaceSize := endPos.runes - startPos.runes
|
||||||
el := e.Len()
|
el := e.Len()
|
||||||
var sc int
|
var sc int
|
||||||
idx := 0
|
idx := 0
|
||||||
for idx < len(s) {
|
for idx < len(s) {
|
||||||
if e.MaxLen > 0 && el+sc >= e.MaxLen {
|
if e.MaxLen > 0 && el-replaceSize+sc >= e.MaxLen {
|
||||||
s = s[:idx]
|
s = s[:idx]
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -1279,7 +1280,6 @@ func (e *Editor) replace(start, end int, s string, addHistory bool) int {
|
|||||||
sc++
|
sc++
|
||||||
}
|
}
|
||||||
newEnd := startPos.runes + sc
|
newEnd := startPos.runes + sc
|
||||||
replaceSize := endPos.runes - startPos.runes
|
|
||||||
|
|
||||||
if addHistory {
|
if addHistory {
|
||||||
e.rr.Seek(int64(startOff), 0)
|
e.rr.Seek(int64(startOff), 0)
|
||||||
|
|||||||
@@ -1019,7 +1019,7 @@ func TestEditor_MaxLen(t *testing.T) {
|
|||||||
Ops: new(op.Ops),
|
Ops: new(op.Ops),
|
||||||
Constraints: layout.Exact(image.Pt(100, 100)),
|
Constraints: layout.Exact(image.Pt(100, 100)),
|
||||||
Queue: newQueue(
|
Queue: newQueue(
|
||||||
key.EditEvent{Range: key.Range{Start: 0, End: 0}, Text: "1234"},
|
key.EditEvent{Range: key.Range{Start: 0, End: 2}, Text: "1234"},
|
||||||
key.SelectionEvent{Start: 4, End: 4},
|
key.SelectionEvent{Start: 4, End: 4},
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
@@ -1031,7 +1031,7 @@ func TestEditor_MaxLen(t *testing.T) {
|
|||||||
if got, want := e.Text(), "12345678"; got != want {
|
if got, want := e.Text(), "12345678"; got != want {
|
||||||
t.Errorf("editor failed to cap EditEvent")
|
t.Errorf("editor failed to cap EditEvent")
|
||||||
}
|
}
|
||||||
if start, end := e.Selection(); start != 1 || end != 1 {
|
if start, end := e.Selection(); start != 3 || end != 3 {
|
||||||
t.Errorf("editor failed to adjust SelectionEvent")
|
t.Errorf("editor failed to adjust SelectionEvent")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user