From 5a3aae4bf7410fd28fe4a92551f2a3c5ae58003a Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Mon, 28 Mar 2011 18:48:52 -0700 Subject: [PATCH] go/printer, gofmt: rely on existing line breaks when formatting expression lists No impact on existing sources. Fixes #1632. R=rsc CC=golang-dev https://golang.org/cl/4271083 --- src/pkg/go/printer/nodes.go | 16 ++-------------- src/pkg/go/printer/testdata/expressions.golden | 10 ++++++++++ src/pkg/go/printer/testdata/expressions.input | 10 ++++++++++ src/pkg/go/printer/testdata/expressions.raw | 10 ++++++++++ 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/pkg/go/printer/nodes.go b/src/pkg/go/printer/nodes.go index 8f0d74ca63..2f12038e52 100644 --- a/src/pkg/go/printer/nodes.go +++ b/src/pkg/go/printer/nodes.go @@ -160,19 +160,7 @@ func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exp // the first linebreak is always a formfeed since this section must not // depend on any previous formatting prevBreak := -1 // index of last expression that was followed by a linebreak - linebreakMin := 1 - if mode&periodSep != 0 { - // Make fragments like - // - // a.Bar(1, - // 2).Foo - // - // format correctly (a linebreak shouldn't be added before Foo) when - // doing period-separated expr lists by setting minimum linebreak to 0 - // lines for them. - linebreakMin = 0 - } - if prev.IsValid() && prev.Line < line && p.linebreak(line, linebreakMin, ws, true) { + if prev.IsValid() && prev.Line < line && p.linebreak(line, 0, ws, true) { ws = ignore *multiLine = true prevBreak = 0 @@ -237,7 +225,7 @@ func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exp // lines are broken using newlines so comments remain aligned // unless forceFF is set or there are multiple expressions on // the same line in which case formfeed is used - if p.linebreak(line, linebreakMin, ws, useFF || prevBreak+1 < i) { + if p.linebreak(line, 0, ws, useFF || prevBreak+1 < i) { ws = ignore *multiLine = true prevBreak = i diff --git a/src/pkg/go/printer/testdata/expressions.golden b/src/pkg/go/printer/testdata/expressions.golden index 314d3213c7..788b9cd222 100644 --- a/src/pkg/go/printer/testdata/expressions.golden +++ b/src/pkg/go/printer/testdata/expressions.golden @@ -619,3 +619,13 @@ func _() { b.(T). c } + + +// Don't introduce extra newlines in strangely formatted expression lists. +func f() { + // os.Open parameters should remain on two lines + if writer, err = os.Open(outfile, s.O_WRONLY|os.O_CREATE| + os.O_TRUNC,0666); err != nil { + log.Fatal(err) + } +} diff --git a/src/pkg/go/printer/testdata/expressions.input b/src/pkg/go/printer/testdata/expressions.input index cac22af431..2c2ebce040 100644 --- a/src/pkg/go/printer/testdata/expressions.input +++ b/src/pkg/go/printer/testdata/expressions.input @@ -625,3 +625,13 @@ baz() (T). c } + + +// Don't introduce extra newlines in strangely formatted expression lists. +func f() { + // os.Open parameters should remain on two lines + if writer, err = os.Open(outfile, s.O_WRONLY|os.O_CREATE| + os.O_TRUNC, 0666); err != nil { + log.Fatal(err) + } +} diff --git a/src/pkg/go/printer/testdata/expressions.raw b/src/pkg/go/printer/testdata/expressions.raw index f22ceeb476..0d22779c61 100644 --- a/src/pkg/go/printer/testdata/expressions.raw +++ b/src/pkg/go/printer/testdata/expressions.raw @@ -618,3 +618,13 @@ func _() { b.(T). c } + + +// Don't introduce extra newlines in strangely formatted expression lists. +func f() { + // os.Open parameters should remain on two lines + if writer, err = os.Open(outfile, s.O_WRONLY|os.O_CREATE| + os.O_TRUNC,0666); err != nil { + log.Fatal(err) + } +}