widget: replace Editor.seek

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2022-02-05 21:41:19 +01:00
parent c071750ed9
commit 3ce403f851
2 changed files with 4 additions and 25 deletions
+4 -20
View File
@@ -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
-5
View File
@@ -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 {