diff --git a/widget/editor.go b/widget/editor.go index c904c313..9e2c0c2e 100644 --- a/widget/editor.go +++ b/widget/editor.go @@ -913,11 +913,12 @@ func (e *Editor) replace(start, end int, s string) { if e.SingleLine { s = strings.ReplaceAll(s, "\n", " ") } + e.makeValid() if start > end { start, end = end, start } - startPos := e.seek(e.caret.start, start) - endPos := e.seek(e.caret.end, end) + startPos := e.closestPosition(combinedPos{runes: start}) + endPos := e.closestPosition(combinedPos{runes: end}) e.rr.deleteRunes(startPos.ofs, endPos.runes-startPos.runes) e.rr.prepend(startPos.ofs, s) newEnd := startPos.runes + utf8.RuneCountInString(s) @@ -929,30 +930,13 @@ func (e *Editor) replace(start, end int, s string) { diff := newEnd - endPos.runes pos.runes = pos.runes + diff } - return e.seek(startPos, pos.runes) + return e.closestPosition(combinedPos{runes: pos.runes}) } e.caret.start = adjust(e.caret.start) e.caret.end = adjust(e.caret.end) e.invalidate() } -// seek returns the byte offset for an absolute rune offset. The provided hint -// is a position potentially close. -func (e *Editor) seek(hint combinedPos, runes int) combinedPos { - pos := hint - for pos.runes > runes && pos.ofs > 0 { - _, s := e.rr.runeBefore(pos.ofs) - pos.ofs -= s - pos.runes-- - } - for pos.runes < runes && pos.ofs < e.rr.len() { - _, s := e.rr.runeAt(pos.ofs) - pos.ofs += s - pos.runes++ - } - return pos -} - func (e *Editor) movePages(pages int, selAct selectionAction) { e.makeValid() x := e.caret.start.x + e.caret.xoff diff --git a/widget/editor_test.go b/widget/editor_test.go index 333adf85..842e5102 100644 --- a/widget/editor_test.go +++ b/widget/editor_test.go @@ -163,11 +163,6 @@ func TestEditorCaretConsistency(t *testing.T) { return fmt.Errorf("caret (%d,%d) pos %s, want (%d,%d) pos %s", gotLine, gotCol, gotCoords, want.lineCol.Y, want.lineCol.X, wantCoords) } - seekCaret := e.seek(combinedPos{}, e.caret.start.runes) - if seekCaret.runes != e.caret.start.runes || seekCaret.ofs != e.caret.start.ofs { - return fmt.Errorf("caret ofs %d, runes %d, expected ofs %d runes %d", - e.caret.start.ofs, e.caret.start.runes, seekCaret.ofs, seekCaret.runes) - } return nil } if err := consistent(); err != nil {