]> Cypherpunks repositories - gostls13.git/commitdiff
go/printer/gofmt: remove special case for multi-line raw strings
authorRobert Griesemer <gri@golang.org>
Wed, 30 Mar 2011 01:30:59 +0000 (18:30 -0700)
committerRobert Griesemer <gri@golang.org>
Wed, 30 Mar 2011 01:30:59 +0000 (18:30 -0700)
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
src/cmd/gofix/netdial.go
src/cmd/gofix/procattr.go
src/pkg/ebnf/ebnf_test.go
src/pkg/go/printer/printer.go
src/pkg/go/printer/testdata/expressions.golden
src/pkg/go/printer/testdata/expressions.input
src/pkg/go/printer/testdata/expressions.raw

index 659a259267f12e337bb53b8c6c7584db555d760b..79eea08c6f37ac42019146099da82d032c2fd4ef 100644 (file)
@@ -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
index e9196f066973f50fa4c406b9f5d3f84b40bf54e3..d1531b647e367342756f77e275590e32768b70af 100644 (file)
@@ -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
 `,
index 32425b06270c8b94de1b3c1244d1ea073de7736b..80b75d1d48a307a779c80e88b949cc7bf3cdc43d 100644 (file)
@@ -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
 `,
index 69ad5fed1cf6aa05c4c2bcc79fcf69ddc4326e30..e77cf64adfaa380e96a8c58c2ec88e790d923ad4 100644 (file)
@@ -15,31 +15,26 @@ var fset = token.NewFileSet()
 
 
 var grammars = []string{
-`Program = .
-`,
-
-`Program = foo .
-foo = "foo" .
-`,
-
-`Program = "a" | "b" "c" .
-`,
-
-`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 = .`,
+
+       `Program = foo .
+        foo = "foo" .`,
+
+       `Program = "a" | "b" "c" .`,
+
+       `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" .`,
 }
 
 
index 2fbec862917eab7bf3f6da7429d4e0c04679d916..697a83fa8665cf0dc24a386f9e798f9d4fd89cb3 100644 (file)
@@ -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
index 788b9cd2226bf8e95973cf269c328286b970f32e..c1a7e970b45e426b80dd98146215377f9181c5e5 100644 (file)
@@ -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
+               `...........
 ...........
 ....●●●....
 ....●●●....
index 2c2ebce040305e63637b0d2ff6a22dd2c8ccae0c..b87381198e470c58bfbae4a19a7c981be8573b7d 100644 (file)
@@ -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
                `...........
 ...........
 ....●●●....
index 0d22779c61765dffcf36441c3a8f14c2c5b0b168..735cd943e69d729fc2eb5f3908b94d4b3f1c7cfa 100644 (file)
@@ -239,7 +239,8 @@ func _() {
        _ = `foo
                bar`
        _ = `three spaces before the end of the line starting here:   
-they must not be removed`\f}
+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
+               `...........
 ...........
 ....●●●....
 ....●●●....