widget: ensure buffer.Read does nothing to read zero bytes

This commit ensures that the edit buffer used by widget.Editor
does not get EOF when trying to read zero bytes from the
underlying buffer, which eliminates a panic when calling
Editor.SelectedText().

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
This commit is contained in:
Chris Waldon
2022-02-09 10:08:05 -05:00
committed by Elias Naur
parent e32925d6a3
commit b28307baeb
2 changed files with 6 additions and 0 deletions
+3
View File
@@ -113,6 +113,9 @@ func (e *editBuffer) Seek(offset int64, whence int) (ret int64, err error) {
}
func (e *editBuffer) Read(p []byte) (int, error) {
if len(p) == 0 {
return 0, nil
}
if e.pos == e.len() {
return 0, io.EOF
}
+3
View File
@@ -121,6 +121,9 @@ func assertCaret(t *testing.T, e *Editor, line, col, bytes int) {
if bytes != caretBytes {
t.Errorf("caret at buffer position %d, expected %d", caretBytes, bytes)
}
// Ensure that SelectedText() does not panic no matter what the
// editor's state is.
_ = e.SelectedText()
}
type editMutation int