diff options
-rw-r--r-- | src/pkg/go/printer/printer.go | 18 | ||||
-rw-r--r-- | src/pkg/go/printer/testdata/comments.golden | 3 | ||||
-rw-r--r-- | src/pkg/go/printer/testdata/comments.input | 3 |
3 files changed, 22 insertions, 2 deletions
diff --git a/src/pkg/go/printer/printer.go b/src/pkg/go/printer/printer.go index fc3cc70d4..36e5d62bc 100644 --- a/src/pkg/go/printer/printer.go +++ b/src/pkg/go/printer/printer.go @@ -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("""); // shorter than """ esc_apos = strings.Bytes("'"); // shorter than "'" @@ -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 { diff --git a/src/pkg/go/printer/testdata/comments.golden b/src/pkg/go/printer/testdata/comments.golden index 7bed90bb1..94a4d8da6 100644 --- a/src/pkg/go/printer/testdata/comments.golden +++ b/src/pkg/go/printer/testdata/comments.golden @@ -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: diff --git a/src/pkg/go/printer/testdata/comments.input b/src/pkg/go/printer/testdata/comments.input index b56224167..7e954c9a2 100644 --- a/src/pkg/go/printer/testdata/comments.input +++ b/src/pkg/go/printer/testdata/comments.input @@ -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: |