mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 09:55:40 +00:00
widget: replace Editor.seek
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+4
-20
@@ -913,11 +913,12 @@ func (e *Editor) replace(start, end int, s string) {
|
|||||||
if e.SingleLine {
|
if e.SingleLine {
|
||||||
s = strings.ReplaceAll(s, "\n", " ")
|
s = strings.ReplaceAll(s, "\n", " ")
|
||||||
}
|
}
|
||||||
|
e.makeValid()
|
||||||
if start > end {
|
if start > end {
|
||||||
start, end = end, start
|
start, end = end, start
|
||||||
}
|
}
|
||||||
startPos := e.seek(e.caret.start, start)
|
startPos := e.closestPosition(combinedPos{runes: start})
|
||||||
endPos := e.seek(e.caret.end, end)
|
endPos := e.closestPosition(combinedPos{runes: end})
|
||||||
e.rr.deleteRunes(startPos.ofs, endPos.runes-startPos.runes)
|
e.rr.deleteRunes(startPos.ofs, endPos.runes-startPos.runes)
|
||||||
e.rr.prepend(startPos.ofs, s)
|
e.rr.prepend(startPos.ofs, s)
|
||||||
newEnd := startPos.runes + utf8.RuneCountInString(s)
|
newEnd := startPos.runes + utf8.RuneCountInString(s)
|
||||||
@@ -929,30 +930,13 @@ func (e *Editor) replace(start, end int, s string) {
|
|||||||
diff := newEnd - endPos.runes
|
diff := newEnd - endPos.runes
|
||||||
pos.runes = pos.runes + diff
|
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.start = adjust(e.caret.start)
|
||||||
e.caret.end = adjust(e.caret.end)
|
e.caret.end = adjust(e.caret.end)
|
||||||
e.invalidate()
|
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) {
|
func (e *Editor) movePages(pages int, selAct selectionAction) {
|
||||||
e.makeValid()
|
e.makeValid()
|
||||||
x := e.caret.start.x + e.caret.xoff
|
x := e.caret.start.x + e.caret.xoff
|
||||||
|
|||||||
@@ -163,11 +163,6 @@ func TestEditorCaretConsistency(t *testing.T) {
|
|||||||
return fmt.Errorf("caret (%d,%d) pos %s, want (%d,%d) pos %s",
|
return fmt.Errorf("caret (%d,%d) pos %s, want (%d,%d) pos %s",
|
||||||
gotLine, gotCol, gotCoords, want.lineCol.Y, want.lineCol.X, wantCoords)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
if err := consistent(); err != nil {
|
if err := consistent(); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user