widget: extract seeking logic from Editor.closestPosition

We'd like to re-use the Editor.closestPosition seeking for
segmentIterator.Next; this change extracts the state-less logic
into functions.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2022-02-16 11:39:41 +01:00
parent 2df3db361f
commit b7341672e3
2 changed files with 114 additions and 67 deletions
+3 -3
View File
@@ -159,7 +159,7 @@ func assertCaret(t *testing.T, e *Editor, line, col, bytes int) {
if gotLine != line || gotCol != col {
t.Errorf("caret at (%d, %d), expected (%d, %d)", gotLine, gotCol, line, col)
}
caretBytes := e.closestPosition(combinedPos{runes: e.caret.start}).ofs
caretBytes := e.runeOffset(e.caret.start)
if bytes != caretBytes {
t.Errorf("caret at buffer position %d, expected %d", caretBytes, bytes)
}
@@ -291,7 +291,7 @@ func TestEditorMoveWord(t *testing.T) {
e := setup(tt.Text)
e.MoveCaret(tt.Start, tt.Start)
e.moveWord(tt.Skip, selectionClear)
caretBytes := e.closestPosition(combinedPos{runes: e.caret.start}).ofs
caretBytes := e.runeOffset(e.caret.start)
if caretBytes != tt.Want {
t.Fatalf("[%d] moveWord: bad caret position: got %d, want %d", ii, caretBytes, tt.Want)
}
@@ -485,7 +485,7 @@ func TestEditorDeleteWord(t *testing.T) {
e.MoveCaret(tt.Start, tt.Start)
e.MoveCaret(0, tt.Selection)
e.deleteWord(tt.Delete)
caretBytes := e.closestPosition(combinedPos{runes: e.caret.start}).ofs
caretBytes := e.runeOffset(e.caret.start)
if caretBytes != tt.Want {
t.Fatalf("[%d] deleteWord: bad caret position: got %d, want %d", ii, caretBytes, tt.Want)
}