From bc7d95b4a30d5f15c36d42a6119ed0bf182b9065 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 18 Aug 2026 17:03:39 +0200 Subject: [PATCH] Demonstrate that a wrapped selection is reported by segment The lines a selection covers are asked for by view line, which counts the segments a wrapping view breaks a line into, and then used to index the content, whose lines are unwrapped. The two agree only for a view whose content doesn't wrap. Co-authored-by: Claude Opus 5 (1M context) --- pkg/gocui/view_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/pkg/gocui/view_test.go b/pkg/gocui/view_test.go index 3ffd56e7c..adbfb952b 100644 --- a/pkg/gocui/view_test.go +++ b/pkg/gocui/view_test.go @@ -820,3 +820,32 @@ func TestApplySelTextColor(t *testing.T) { }) } } + +// A view that wraps draws one line of its content as several view lines, and the +// cursor and the range anchor count those. What is asked about a selection is +// which lines of the content it covers, so those are what it has to be reported +// in. +func TestSelectedLinesOfWrappedContent(t *testing.T) { + v := NewView("name", 0, 0, 11, 10, OutputNormal) // InnerWidth 10 + v.Wrap = true + v.Highlight = true + + // "a line that wraps" takes two view lines, so the four lines of content are + // drawn as five: "one", "two", "a line th", "at wraps", "four". + v.writeString("one\ntwo\na line that wraps\nfour\n") + assert.Equal(t, 5, v.ViewLinesHeight()) + + // The cursor on the wrapped line's second half is on that line. + v.FocusPoint(0, 3, false) + /* EXPECTED: + assert.Equal(t, "a line that wraps", v.SelectedLine()) + ACTUAL: */ + assert.Equal(t, "four", v.SelectedLine()) + + // A range over both halves of the wrapped line covers one line of content. + v.SetRangeSelectStart(2) + /* EXPECTED: + assert.Equal(t, []string{"a line that wraps"}, v.SelectedLines()) + ACTUAL: */ + assert.Equal(t, []string{"a line that wraps", "four"}, v.SelectedLines()) +}