Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions textarea/textarea.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions textarea/textarea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down