summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2009-11-05 17:02:29 -0800
committerRobert Griesemer <gri@golang.org>2009-11-05 17:02:29 -0800
commit1516e741990b9573089c8a723c51a7bd34e2b3fd (patch)
treebbc688a50a6f320d2f3575f6420bd96eceb6a8ad
parent653742d126637af89b68e6d97d4131d1210436be (diff)
downloadgolang-1516e741990b9573089c8a723c51a7bd34e2b3fd.tar.gz
fix a comment formatting bug:
- 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
-rw-r--r--src/pkg/go/printer/printer.go18
-rw-r--r--src/pkg/go/printer/testdata/comments.golden3
-rw-r--r--src/pkg/go/printer/testdata/comments.input3
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("&#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 {
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: