]> Cypherpunks repositories - gostls13.git/commitdiff
go/printer, gofmt: trim trailing whitespace in comments
authorRobert Griesemer <gri@golang.org>
Tue, 30 Oct 2012 20:09:47 +0000 (13:09 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 30 Oct 2012 20:09:47 +0000 (13:09 -0700)
Also: updated go fix testcases to pass tests.

Fixes #4310.

R=r
CC=golang-dev
https://golang.org/cl/6810055

src/cmd/fix/testdata/reflect.encoder.go.in
src/cmd/fix/testdata/reflect.encoder.go.out
src/cmd/fix/testdata/reflect.export.go.in
src/cmd/fix/testdata/reflect.export.go.out
src/cmd/fix/testdata/reflect.print.go.in
src/cmd/fix/testdata/reflect.print.go.out
src/pkg/go/printer/printer.go
src/pkg/go/printer/testdata/comments.golden
src/pkg/go/printer/testdata/comments.input

index 0202d79ac90b06171f1b32a724c8f2689e1e24af..702f6dc068f996a6cc83c162701a9288d50850a0 100644 (file)
@@ -120,7 +120,7 @@ func (enc *Encoder) sendActualType(w io.Writer, state *encoderState, ut *userTyp
        return true
 }
 
-// sendType sends the type info to the other side, if necessary. 
+// sendType sends the type info to the other side, if necessary.
 func (enc *Encoder) sendType(w io.Writer, state *encoderState, origt reflect.Type) (sent bool) {
        ut := userType(origt)
        if ut.isGobEncoder {
index 925d39301e6e1d3089a97c65e4058f4f7250b05c..f1a7b98f16c80d5e5a3a8b39167d6a172336cdd6 100644 (file)
@@ -120,7 +120,7 @@ func (enc *Encoder) sendActualType(w io.Writer, state *encoderState, ut *userTyp
        return true
 }
 
-// sendType sends the type info to the other side, if necessary. 
+// sendType sends the type info to the other side, if necessary.
 func (enc *Encoder) sendType(w io.Writer, state *encoderState, origt reflect.Type) (sent bool) {
        ut := userType(origt)
        if ut.isGobEncoder {
index ce7940b2984fe021f04df6a4f954c712c11c8def..722387ac58b1154ac21105e9eec1b0cf8b1bc623 100644 (file)
@@ -162,7 +162,7 @@ func (client *expClient) run() {
                                // all messages <=N have been seen by the recipient.  We check anyway.
                                expLog("sequence out of order:", client.ackNum, hdr.SeqNum)
                        }
-                       if client.ackNum < hdr.SeqNum { // If there has been an error, don't back up the count. 
+                       if client.ackNum < hdr.SeqNum { // If there has been an error, don't back up the count.
                                client.ackNum = hdr.SeqNum
                        }
                        client.mu.Unlock()
index 7bd73c5e7fcad86ac8a5e4e78b5c26c482a30d69..d1324f346561ae410b9d67f48471ca8b1e6ccf38 100644 (file)
@@ -162,7 +162,7 @@ func (client *expClient) run() {
                                // all messages <=N have been seen by the recipient.  We check anyway.
                                expLog("sequence out of order:", client.ackNum, hdr.SeqNum)
                        }
-                       if client.ackNum < hdr.SeqNum { // If there has been an error, don't back up the count. 
+                       if client.ackNum < hdr.SeqNum { // If there has been an error, don't back up the count.
                                client.ackNum = hdr.SeqNum
                        }
                        client.mu.Unlock()
index 6c9b8e4f9aec51e0fbb031114c3345518f809471..14cf2b215c3444c609ea965394792c623abd74aa 100644 (file)
@@ -182,7 +182,7 @@ func Sprintf(format string, a ...interface{}) string {
        return s
 }
 
-// Errorf formats according to a format specifier and returns the string 
+// Errorf formats according to a format specifier and returns the string
 // converted to an os.ErrorString, which satisfies the os.Error interface.
 func Errorf(format string, a ...interface{}) os.Error {
        return os.NewError(Sprintf(format, a...))
index b475a2ae172f8e949335a565167b3a47d336a9d8..e4e4c73687a291cd1890ecc39c269b4304ce999c 100644 (file)
@@ -182,7 +182,7 @@ func Sprintf(format string, a ...interface{}) string {
        return s
 }
 
-// Errorf formats according to a format specifier and returns the string 
+// Errorf formats according to a format specifier and returns the string
 // converted to an os.ErrorString, which satisfies the os.Error interface.
 func Errorf(format string, a ...interface{}) os.Error {
        return os.NewError(Sprintf(format, a...))
index 516c37161c13959b3fa433880e99202ce4640dc1..030bc2387efff83ecf24d5a655bd4556e05098ac 100644 (file)
@@ -14,6 +14,7 @@ import (
        "strconv"
        "strings"
        "text/tabwriter"
+       "unicode"
 )
 
 const (
@@ -405,6 +406,7 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, prev, comment *as
 // Split comment text into lines
 // (using strings.Split(text, "\n") is significantly slower for
 // this specific purpose, as measured with: go test -bench=Print)
+//
 func split(text string) []string {
        // count lines (comment text never ends in a newline)
        n := 1
@@ -432,6 +434,7 @@ func split(text string) []string {
 
 // Returns true if s contains only white space
 // (only tabs and blanks can appear in the printer's context).
+//
 func isBlank(s string) bool {
        for i := 0; i < len(s); i++ {
                if s[i] > ' ' {
@@ -441,6 +444,7 @@ func isBlank(s string) bool {
        return true
 }
 
+// commonPrefix returns the common prefix of a and b.
 func commonPrefix(a, b string) string {
        i := 0
        for i < len(a) && i < len(b) && a[i] == b[i] && (a[i] <= ' ' || a[i] == '*') {
@@ -449,6 +453,11 @@ func commonPrefix(a, b string) string {
        return a[0:i]
 }
 
+// trimRight returns s with trailing whitespace removed.
+func trimRight(s string) string {
+       return strings.TrimRightFunc(s, unicode.IsSpace)
+}
+
 // stripCommonPrefix removes a common prefix from /*-style comment lines (unless no
 // comment line is indented, all but the first line have some form of space prefix).
 // The prefix is computed using heuristics such that is is likely that the comment
@@ -611,7 +620,7 @@ func (p *printer) writeComment(comment *ast.Comment) {
 
        // shortcut common case of //-style comments
        if text[1] == '/' {
-               p.writeString(pos, text, true)
+               p.writeString(pos, trimRight(text), true)
                return
        }
 
@@ -641,7 +650,7 @@ func (p *printer) writeComment(comment *ast.Comment) {
                        pos = p.pos
                }
                if len(line) > 0 {
-                       p.writeString(pos, line, true)
+                       p.writeString(pos, trimRight(line), true)
                }
        }
 }
@@ -1159,7 +1168,7 @@ func (p *trimmer) Write(data []byte) (n int, err error) {
 // ----------------------------------------------------------------------------
 // Public interface
 
-// A Mode value is a set of flags (or 0). They control printing. 
+// A Mode value is a set of flags (or 0). They control printing.
 type Mode uint
 
 const (
index d9aa2d82f7d8510392ca2effb38f32909f3a8fa0..610a42a68bda5bf1b9ea7258404c9a11b5d114a7 100644 (file)
@@ -529,7 +529,7 @@ func _() {
 }
 
 func _() {
-       var a = []int{1, 2}// jasldf 
+       var a = []int{1, 2}// jasldf
 
        _ = a
 }
@@ -626,4 +626,13 @@ func _() {
        var lflag bool          // -l                   - disable line directives
 }
 
+// Trailing white space in comments should be trimmed
+func _() {
+       // This comment has 4 blanks following that should be trimmed:
+       /* Each line of this comment has blanks or tabs following that should be trimmed:
+          line 2:
+          line 3:
+       */
+}
+
 /* This comment is the last entry in this file. It must be printed and should be followed by a newline */
index 6084b3fe4500666a6d6380cedeb7ad43a3752892..d121dd4be77d706162dad3db38132247a85d3aef 100644 (file)
@@ -534,7 +534,7 @@ func _() {
 }
 
 func _() {
-       var a = []int{1, 2, // jasldf 
+       var a = []int{1, 2, // jasldf
        }
        _ = a
 }
@@ -630,5 +630,13 @@ var        vflag           string                          // -v [y.output]        - y.output file
 var    lflag           bool                            // -l                   - disable line directives
 }
 
+// Trailing white space in comments should be trimmed
+func _() {
+// This comment has 4 blanks following that should be trimmed:    
+/* Each line of this comment has blanks or tabs following that should be trimmed:      
+   line 2:    
+   line 3:                     
+*/
+}
 
 /* This comment is the last entry in this file. It must be printed and should be followed by a newline */