From 1afc37fa7eb5bcc48a72646357a497973b5a2c1e Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 29 Mar 2011 18:30:59 -0700 Subject: [PATCH] go/printer/gofmt: remove special case for multi-line raw strings As a special case, multi-line raw strings (i.e., strings in `` quotes) were not indented if they were the only token on a line. This heuristic was meant to improve formatting for multi-line raw strings where sub- sequent lines are not indented at the level of the surrounding code. Multiple people have complained about this. Removing the heuristic again because it makes the formatting more regular, easier to under- stand, and simplifies the implementation. - manual changes to ebnf/ebnf_test.go for readability - gofmt -w src misc Fixes #1643. R=r, rsc CC=golang-dev https://golang.org/cl/4307045 --- src/cmd/gofix/httpserver.go | 2 +- src/cmd/gofix/netdial.go | 6 +- src/cmd/gofix/procattr.go | 2 +- src/pkg/ebnf/ebnf_test.go | 37 +++++------ src/pkg/go/printer/printer.go | 66 ++++--------------- .../go/printer/testdata/expressions.golden | 18 ++--- src/pkg/go/printer/testdata/expressions.input | 8 +-- src/pkg/go/printer/testdata/expressions.raw | 21 +++--- 8 files changed, 57 insertions(+), 103 deletions(-) diff --git a/src/cmd/gofix/httpserver.go b/src/cmd/gofix/httpserver.go index 659a259267f..79eea08c6f3 100644 --- a/src/cmd/gofix/httpserver.go +++ b/src/cmd/gofix/httpserver.go @@ -12,7 +12,7 @@ import ( var httpserverFix = fix{ "httpserver", httpserver, -`Adapt http server methods and functions to changes + `Adapt http server methods and functions to changes made to the http ResponseWriter interface. http://codereview.appspot.com/4245064 Hijacker diff --git a/src/cmd/gofix/netdial.go b/src/cmd/gofix/netdial.go index e9196f06697..d1531b647e3 100644 --- a/src/cmd/gofix/netdial.go +++ b/src/cmd/gofix/netdial.go @@ -11,7 +11,7 @@ import ( var netdialFix = fix{ "netdial", netdial, -`Adapt 3-argument calls of net.Dial to use 2-argument form. + `Adapt 3-argument calls of net.Dial to use 2-argument form. http://codereview.appspot.com/4244055 `, @@ -20,7 +20,7 @@ http://codereview.appspot.com/4244055 var tlsdialFix = fix{ "tlsdial", tlsdial, -`Adapt 4-argument calls of tls.Dial to use 3-argument form. + `Adapt 4-argument calls of tls.Dial to use 3-argument form. http://codereview.appspot.com/4244055 `, @@ -29,7 +29,7 @@ http://codereview.appspot.com/4244055 var netlookupFix = fix{ "netlookup", netlookup, -`Adapt 3-result calls to net.LookupHost to use 2-result form. + `Adapt 3-result calls to net.LookupHost to use 2-result form. http://codereview.appspot.com/4244055 `, diff --git a/src/cmd/gofix/procattr.go b/src/cmd/gofix/procattr.go index 32425b06270..80b75d1d48a 100644 --- a/src/cmd/gofix/procattr.go +++ b/src/cmd/gofix/procattr.go @@ -12,7 +12,7 @@ import ( var procattrFix = fix{ "procattr", procattr, -`Adapt calls to os.StartProcess to use new ProcAttr type. + `Adapt calls to os.StartProcess to use new ProcAttr type. http://codereview.appspot.com/4253052 `, diff --git a/src/pkg/ebnf/ebnf_test.go b/src/pkg/ebnf/ebnf_test.go index 69ad5fed1cf..e77cf64adfa 100644 --- a/src/pkg/ebnf/ebnf_test.go +++ b/src/pkg/ebnf/ebnf_test.go @@ -15,31 +15,26 @@ var fset = token.NewFileSet() var grammars = []string{ -`Program = . -`, + `Program = .`, -`Program = foo . -foo = "foo" . -`, + `Program = foo . + foo = "foo" .`, -`Program = "a" | "b" "c" . -`, + `Program = "a" | "b" "c" .`, -`Program = "a" ... "z" . -`, + `Program = "a" ... "z" .`, -`Program = Song . - Song = { Note } . - Note = Do | (Re | Mi | Fa | So | La) | Ti . - Do = "c" . - Re = "d" . - Mi = "e" . - Fa = "f" . - So = "g" . - La = "a" . - Ti = ti . - ti = "b" . -`, + `Program = Song . + Song = { Note } . + Note = Do | (Re | Mi | Fa | So | La) | Ti . + Do = "c" . + Re = "d" . + Mi = "e" . + Fa = "f" . + So = "g" . + La = "a" . + Ti = ti . + ti = "b" .`, } diff --git a/src/pkg/go/printer/printer.go b/src/pkg/go/printer/printer.go index 2fbec862917..697a83fa866 100644 --- a/src/pkg/go/printer/printer.go +++ b/src/pkg/go/printer/printer.go @@ -14,7 +14,6 @@ import ( "os" "path/filepath" "runtime" - "strings" "tabwriter" ) @@ -35,13 +34,6 @@ const ( ) -const ( - esc2 = '\xfe' // an escape byte that cannot occur in regular UTF-8 - _ = 1 / (esc2 - tabwriter.Escape) // cause compiler error if esc2 == tabwriter.Escape - esc2str = "\xfe" -) - - var ( esc = []byte{tabwriter.Escape} htab = []byte{'\t'} @@ -775,21 +767,7 @@ func (p *printer) print(args ...interface{}) { data = x.Name tok = token.IDENT case *ast.BasicLit: - // If we have a raw string that spans multiple lines and - // the opening quote (`) is on a line preceded only by - // indentation, we don't want to write that indentation - // because the following lines of the raw string are not - // indented. It's easiest to correct the output at the end - // via the trimmer (because of the complex handling of - // white space). - // Mark multi-line raw strings by replacing the opening - // quote with esc2 and have the trimmer take care of fixing - // it up. - if x.Value[0] == '`' && strings.Index(x.Value, "\n") > 0 { - data = p.escape(esc2str + x.Value[1:]) - } else { - data = p.escape(x.Value) - } + data = p.escape(x.Value) tok = x.Kind case token.Token: s := x.String() @@ -871,10 +849,9 @@ func (p *printer) flush(next token.Position, tok token.Token) (droppedFF bool) { // through unchanged. // type trimmer struct { - output io.Writer - state int - space bytes.Buffer - hasText bool + output io.Writer + state int + space bytes.Buffer } @@ -882,15 +859,11 @@ type trimmer struct { // It can be in one of the following states: const ( inSpace = iota // inside space - atEscape // inside space and the last char was an opening tabwriter.Escape inEscape // inside text bracketed by tabwriter.Escapes inText // inside text ) -var backquote = []byte{'`'} - - // Design note: It is tempting to eliminate extra blanks occurring in // whitespace in this function as it could simplify some // of the blanks logic in the node printing functions. @@ -899,9 +872,8 @@ var backquote = []byte{'`'} func (p *trimmer) Write(data []byte) (n int, err os.Error) { // invariants: - // p.state == inSpace, atEscape: + // p.state == inSpace: // p.space is unwritten - // p.hasText indicates if there is any text on this line // p.state == inEscape, inText: // data[m:n] is unwritten m := 0 @@ -918,32 +890,20 @@ func (p *trimmer) Write(data []byte) (n int, err os.Error) { case '\n', '\f': p.space.Reset() // discard trailing space _, err = p.output.Write(newlines[0:1]) // write newline - p.hasText = false case tabwriter.Escape: - p.state = atEscape + _, err = p.output.Write(p.space.Bytes()) + p.state = inEscape + m = n + 1 // +1: skip tabwriter.Escape default: _, err = p.output.Write(p.space.Bytes()) p.state = inText m = n } - case atEscape: - // discard indentation if we have a multi-line raw string - // (see printer.print for details) - if b != esc2 || p.hasText { - _, err = p.output.Write(p.space.Bytes()) - } - p.state = inEscape - m = n - if b == esc2 { - _, err = p.output.Write(backquote) // convert back - m++ - } case inEscape: if b == tabwriter.Escape { _, err = p.output.Write(data[m:n]) p.state = inSpace p.space.Reset() - p.hasText = true } case inText: switch b { @@ -952,19 +912,18 @@ func (p *trimmer) Write(data []byte) (n int, err os.Error) { p.state = inSpace p.space.Reset() p.space.WriteByte(b) // WriteByte returns no errors - p.hasText = true case '\n', '\f': _, err = p.output.Write(data[m:n]) p.state = inSpace p.space.Reset() _, err = p.output.Write(newlines[0:1]) // write newline - p.hasText = false case tabwriter.Escape: _, err = p.output.Write(data[m:n]) - p.state = atEscape - p.space.Reset() - p.hasText = true + p.state = inEscape + m = n + 1 // +1: skip tabwriter.Escape } + default: + panic("unreachable") } if err != nil { return @@ -977,7 +936,6 @@ func (p *trimmer) Write(data []byte) (n int, err os.Error) { _, err = p.output.Write(data[m:n]) p.state = inSpace p.space.Reset() - p.hasText = true } return diff --git a/src/pkg/go/printer/testdata/expressions.golden b/src/pkg/go/printer/testdata/expressions.golden index 788b9cd2226..c1a7e970b45 100644 --- a/src/pkg/go/printer/testdata/expressions.golden +++ b/src/pkg/go/printer/testdata/expressions.golden @@ -253,8 +253,8 @@ bar` var _ = `` var _ = `foo` var _ = - // the next line should not be indented -`foo + // the next line should remain indented + `foo bar` var _ = // comment @@ -262,8 +262,8 @@ bar` var _ = // comment `foo` var _ = // comment - // the next line should not be indented -`foo + // the next line should remain indented + `foo bar` var _ = /* comment */ `` @@ -276,12 +276,12 @@ bar` var _ = /* comment */ `foo` var _ = /* comment */ - // the next line should not be indented -`foo + // the next line should remain indented + `foo bar` var board = []int( -`........... + `........... ........... ....●●●.... ....●●●.... @@ -296,8 +296,8 @@ bar` var state = S{ "foo", - // the next line should not be indented -`........... + // the next line should remain indented + `........... ........... ....●●●.... ....●●●.... diff --git a/src/pkg/go/printer/testdata/expressions.input b/src/pkg/go/printer/testdata/expressions.input index 2c2ebce0403..b87381198e4 100644 --- a/src/pkg/go/printer/testdata/expressions.input +++ b/src/pkg/go/printer/testdata/expressions.input @@ -256,7 +256,7 @@ var _ = var _ = `foo` var _ = - // the next line should not be indented + // the next line should remain indented `foo bar` @@ -266,7 +266,7 @@ bar` var _ = // comment `foo` var _ = // comment - // the next line should not be indented + // the next line should remain indented `foo bar` @@ -282,7 +282,7 @@ bar` var _ = /* comment */ `foo` var _ = /* comment */ - // the next line should not be indented + // the next line should remain indented `foo bar` @@ -304,7 +304,7 @@ var board = []int( var state = S{ "foo", - // the next line should not be indented + // the next line should remain indented `........... ........... ....●●●.... diff --git a/src/pkg/go/printer/testdata/expressions.raw b/src/pkg/go/printer/testdata/expressions.raw index 0d22779c617..735cd943e69 100644 --- a/src/pkg/go/printer/testdata/expressions.raw +++ b/src/pkg/go/printer/testdata/expressions.raw @@ -239,7 +239,8 @@ func _() { _ = `foo bar` _ = `three spaces before the end of the line starting here: -they must not be removed` } +they must not be removed` +} func _() { @@ -252,8 +253,8 @@ bar` var _ = `` var _ = `foo` var _ = - // the next line should not be indented -`foo + // the next line should remain indented + `foo bar` var _ = // comment @@ -261,8 +262,8 @@ bar` var _ = // comment `foo` var _ = // comment - // the next line should not be indented -`foo + // the next line should remain indented + `foo bar` var _ = /* comment */ `` @@ -275,12 +276,12 @@ bar` var _ = /* comment */ `foo` var _ = /* comment */ - // the next line should not be indented -`foo + // the next line should remain indented + `foo bar` var board = []int( -`........... + `........... ........... ....●●●.... ....●●●.... @@ -295,8 +296,8 @@ bar` var state = S{ "foo", - // the next line should not be indented -`........... + // the next line should remain indented + `........... ........... ....●●●.... ....●●●....