diff --git a/textarea/textarea.go b/textarea/textarea.go index f0c0ca54b..70a50a8d9 100644 --- a/textarea/textarea.go +++ b/textarea/textarea.go @@ -482,6 +482,7 @@ func (m *Model) SetValue(s string) { m.Reset() m.InsertString(s) m.recalculateHeight() + m.repositionView() } // InsertString inserts a string at the cursor position. diff --git a/textarea/textarea_test.go b/textarea/textarea_test.go index 41d51f744..8efb81c86 100644 --- a/textarea/textarea_test.go +++ b/textarea/textarea_test.go @@ -143,6 +143,22 @@ func TestSetValue(t *testing.T) { } } +func TestSetValueViewportReposition(t *testing.T) { + ta := newTextArea() + ta.SetHeight(3) + ta.SetWidth(40) + + // Insert more lines than the viewport height so the cursor ends below the visible area. + lines := strings.Join([]string{"line1", "line2", "line3", "line4", "line5"}, "\n") + ta.SetValue(lines) + + // Cursor must be visible: ScrollYOffset must be high enough that the last row is in view. + if ta.ScrollYOffset()+ta.Height()-1 < ta.LineCount()-1 { + t.Fatalf("cursor out of view after SetValue: yOffset=%d height=%d lines=%d", + ta.ScrollYOffset(), ta.Height(), ta.LineCount()) + } +} + func TestInsertString(t *testing.T) { textarea := newTextArea()