From d46324fe783b67fb18cb68c0fcea0d586ad1c43a Mon Sep 17 00:00:00 2001 From: Arpad Ryszka Date: Tue, 10 Nov 2020 01:33:46 +0100 Subject: [PATCH] handle multiple non-wrapped long nodes following each other --- fprint.go | 25 ++++++++++++++++++++----- sprint_test.go | 11 ++++++++--- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/fprint.go b/fprint.go index d06d78d..f1f37da 100644 --- a/fprint.go +++ b/fprint.go @@ -115,23 +115,38 @@ func wrapNode(t, c0, c1 int, n node) node { n.wrap = true cc0, cc1 := c0, c1 - for i, p := range n.parts { + lastWrapperIndex := -1 + var trackBack bool + for i := 0; i < len(n.parts); i++ { + p := n.parts[i] switch part := p.(type) { case node: part = wrapNode(t, cc0, cc1, part) n.parts[i] = part if part.wrap { - cc0 -= part.wrapLen.last - cc1 -= part.wrapLen.last + // This is an approximation: sometimes part.fullWrap.first is applied here, + // but usually those are the same. + cc0 -= part.wrapLen.first + cc1 -= part.wrapLen.first } else { cc0 -= part.len cc1 -= part.len } + + if !trackBack && cc1 < 0 { + cc0 = 0 + cc1 = 0 + i = lastWrapperIndex + trackBack = true + } case wrapper: - if len(part.items) > 0 { - cc0, cc1 = c0, c1 + if len(part.items) == 0 { + continue } + cc0, cc1 = c0, c1 + trackBack = false + lastWrapperIndex = i switch part.mode { case line: c0, c1 = c0-t, c1-t diff --git a/sprint_test.go b/sprint_test.go index b1ae138..dbb2a22 100644 --- a/sprint_test.go +++ b/sprint_test.go @@ -910,11 +910,13 @@ func TestSortedMap(t *testing.T) { } func TestBytes(t *testing.T) { - const expectNotWrapped = `[]{00 01 02 03 04 05 06 07 08 09 0a 0b}` - const expectWrapped = `[]{ + const ( + expectNotWrapped = `[]{00 01 02 03 04 05 06 07 08 09 0a 0b}` + expectWrapped = `[]{ 00 01 02 03 04 05 06 07 08 09 0a 0b }` + ) b := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} t.Run("not wrapped", func(t *testing.T) { @@ -940,7 +942,10 @@ func TestBytes(t *testing.T) { } func TestNonWrapperNodes(t *testing.T) { - const expect = `map[struct{foo int; bar int}]struct{ + const expect = `map[struct{ + foo int + bar int +}]struct{ foo int bar int }{}`