]> Cypherpunks repositories - gostls13.git/commitdiff
better placement of /*-style comments interspersed with code on one line
authorRobert Griesemer <gri@golang.org>
Wed, 4 Nov 2009 23:33:28 +0000 (15:33 -0800)
committerRobert Griesemer <gri@golang.org>
Wed, 4 Nov 2009 23:33:28 +0000 (15:33 -0800)
R=rsc
http://go/go-review/1017030

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

index 1511beee97f600ade18d20f4ff89203170ec5bfa..6c649eb38c6aea2e9e73a913a9ff793e4737a715 100644 (file)
@@ -256,11 +256,10 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, isFirst, isKeywor
                return;
        }
 
-       n := pos.Line - p.last.Line;
-       if n == 0 {
+       if pos.Line == p.last.Line {
                // comment on the same line as last item:
-               // separate with at least one tab
-               hasTab := false;
+               // separate with at least one separator
+               hasSep := false;
                if isFirst {
                        j := 0;
                        for i, ch := range p.buffer {
@@ -272,7 +271,7 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, isFirst, isKeywor
                                case vtab:
                                        // respect existing tabs - important
                                        // for proper formatting of commented structs
-                                       hasTab = true;
+                                       hasSep = true;
                                        continue;
                                case indent:
                                        // apply pending indentation
@@ -283,9 +282,16 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, isFirst, isKeywor
                        }
                        p.writeWhitespace(j);
                }
-               // make sure there is at least one tab
-               if !hasTab {
-                       p.write(htab);
+               // make sure there is at least one separator
+               if !hasSep {
+                       if pos.Line == next.Line {
+                               // next item is on the same line as the comment
+                               // (which must be a /*-style comment): separate
+                               // with a blank instead of a tab
+                               p.write([]byte{' '});
+                       } else {
+                               p.write(htab);
+                       }
                }
 
        } else {
@@ -321,7 +327,7 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, isFirst, isKeywor
                        }
                        p.writeWhitespace(j);
                }
-               p.writeNewlines(n);
+               p.writeNewlines(pos.Line - p.last.Line);
        }
 }
 
@@ -560,14 +566,21 @@ func (p *printer) writeCommentSuffix(needsLinebreak bool) {
 func (p *printer) intersperseComments(next token.Position, isKeyword bool) {
        isFirst := true;
        needsLinebreak := false;
+       var last *ast.Comment;
        for ; p.commentBefore(next); p.comment = p.comment.Next {
                for _, c := range p.comment.List {
                        p.writeCommentPrefix(c.Pos(), next, isFirst, isKeyword);
                        isFirst = false;
                        p.writeComment(c);
                        needsLinebreak = c.Text[1] == '/';
+                       last = c;
                }
        }
+       if last != nil && !needsLinebreak && last.Pos().Line == next.Line {
+               // the last comment is a /*-style comment and the next item
+               // follows on the same line: separate with an extra blank
+               p.write([]byte{' '});
+       }
        p.writeCommentSuffix(needsLinebreak);
 }
 
index 38acce5cbb0f4fbe339e5363d3d507ca836dc1a0..05399a3c6d01c926bc590215b4a956d3a3505b1c 100644 (file)
@@ -215,6 +215,11 @@ func _() {
 }
 
 
+// Some interesting interspersed comments
+func _(/* this */x/* is *//* an */ int) {
+}
+
+
 // Line comments with tabs
 func _() {
 var    finput          *bufio.Reader;                  // input file
index 2f4fb24071baee5a55d8426fc0beba36d3b7a875..5772c56298afb2e3181c33def5e9977412ae3d36 100644 (file)
@@ -215,6 +215,11 @@ func _() {
 }
 
 
+// Some interesting interspersed comments
+func _( /* this */ x /* is */ /* an */ int) {
+}
+
+
 // Line comments with tabs
 func _() {
        var finput *bufio.Reader;       // input file
index 2071543c52b6f836d93e12439f64bb1d8366a77f..2f5cf6059facc36261b742f679a8098f13f1292c 100644 (file)
@@ -294,7 +294,7 @@ func _() {
 // formatting of structs
 type _ struct{}
 
-type _ struct {        /* this comment should be visible */}
+type _ struct { /* this comment should be visible */ }
 
 type _ struct {
        // this comment should be visible and properly indented