]> Cypherpunks repositories - gostls13.git/commitdiff
fix a comment formatting bug:
authorRobert Griesemer <gri@golang.org>
Fri, 6 Nov 2009 01:02:29 +0000 (17:02 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 6 Nov 2009 01:02:29 +0000 (17:02 -0800)
- this ensures better comment formatting in template.go and codec_test.go
- it affects only 2 files of all files that have been gofmt'ed already,
  see separate CL (it fixes the same issue in those files)

R=rsc
http://go/go-review/1023002

src/pkg/go/printer/printer.go
src/pkg/go/printer/testdata/comments.golden
src/pkg/go/printer/testdata/comments.input

index fc3cc70d45bd1bec49fcd7306e79b25aa15c91f5..36e5d62bca9a2e4dd0f29a07ac3cbf3c6fdcb73d 100644 (file)
@@ -43,6 +43,7 @@ var (
        htab            = []byte{'\t'};
        htabs           = [...]byte{'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t'};
        newlines        = [...]byte{'\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n'};    // more than maxNewlines
+       formfeeds       = [...]byte{'\f', '\f', '\f', '\f', '\f', '\f', '\f', '\f'};    // more than maxNewlines
 
        esc_quot        = strings.Bytes("&#34;");       // shorter than "&quot;"
        esc_apos        = strings.Bytes("&#39;");       // shorter than "&apos;"
@@ -203,6 +204,16 @@ func (p *printer) writeNewlines(n int) {
 }
 
 
+func (p *printer) writeFormfeeds(n int) {
+       if n > 0 {
+               if n > maxNewlines {
+                       n = maxNewlines;
+               }
+               p.write(formfeeds[0:n]);
+       }
+}
+
+
 func (p *printer) writeTaggedItem(data []byte, tag HtmlTag) {
        // write start tag, if any
        // (no html-escaping and no p.pos update for tags - use write0)
@@ -332,7 +343,10 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, isFirst, isKeywor
                        }
                        p.writeWhitespace(j);
                }
-               p.writeNewlines(pos.Line - p.last.Line);
+               // use formfeeds to break columns before a comment;
+               // this is analogous to using formfeeds to separate
+               // individual lines of /*-style comments
+               p.writeFormfeeds(pos.Line - p.last.Line);
        }
 }
 
@@ -535,7 +549,7 @@ func (p *printer) writeComment(comment *ast.Comment) {
 
        // write comment lines, separated by formfeed,
        // without a line break after the last line
-       linebreak := []byte{byte(formfeed)};
+       linebreak := formfeeds[0:1];
        pos := comment.Pos();
        for i, line := range lines {
                if i > 0 {
index 7bed90bb10a5e4872203c2e8fed95c4920867c17..94a4d8da678d292a8a7d1d2ed23d58ea41fc34a5 100644 (file)
@@ -118,6 +118,9 @@ func typeswitch(x interface{}) {
        }
 
        switch v0, ok := x.(int); x.(type) {
+       case byte:      // this comment should be on the same line as the keyword
+               // this comment should be normally indented
+               _ = 0;
        case bool, int, float:
                // this comment should be indented
        case string:
index b562241671cbb158b4022f608edb89bbcfbac862..7e954c9a24a15462feb888cc1644f3d436318174 100644 (file)
@@ -118,6 +118,9 @@ func typeswitch(x interface{}) {
        }
 
        switch v0, ok := x.(int); x.(type) {
+       case byte:  // this comment should be on the same line as the keyword
+               // this comment should be normally indented
+               _ = 0;
        case bool, int, float:
                // this comment should be indented
        case string: