]> Cypherpunks repositories - gostls13.git/commitdiff
gofmt, go/printer: update internal estimated position correctly
authorRobert Griesemer <gri@golang.org>
Thu, 9 Sep 2010 05:01:48 +0000 (22:01 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 9 Sep 2010 05:01:48 +0000 (22:01 -0700)
Fixes #1089.

R=rsc
CC=golang-dev
https://golang.org/cl/2172043

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

index f1873d92ddb4680cd2325ca4fcd95be3967d07df..5ee2491958576b9e5a31434be2901dc7353cdc8c 100644 (file)
@@ -209,6 +209,11 @@ func (p *printer) write(data []byte) {
 
                case tabwriter.Escape:
                        p.escape = !p.escape
+
+                       // ignore escape chars introduced by printer - they are
+                       // invisible and must not affect p.pos (was issue #1089)
+                       p.pos.Offset--
+                       p.pos.Column--
                }
        }
 
index 4c9f71d95b7ea2edde725a952bcedd64b4a628c1..200ea332f6ae9f9fdfea9d0cb192e5a1115dd3a5 100644 (file)
@@ -431,6 +431,38 @@ func _() {
 }
 
 
+// Comments immediately adjacent to punctuation (for which the go/printer
+// may obly have estimated position information) must remain after the punctuation.
+func _() {
+       _ = T{
+               1,      // comment after comma
+               2,      /* comment after comma */
+               3,      // comment after comma
+       }
+       _ = T{
+               1,      // comment after comma
+               2,      /* comment after comma */
+               3,      // comment after comma
+       }
+       _ = T{
+               /* comment before literal */ 1,
+               2,      /* comment before comma - ok to move after comma */
+               3,      /* comment before comma - ok to move after comma */
+       }
+
+       for i = 0;      // comment after semicolon
+       i < 9;          /* comment after semicolon */
+       i++ {           // comment after opening curly brace
+       }
+
+       // TODO(gri) the last comment in this example should be aligned */
+       for i = 0;      // comment after semicolon
+       i < 9;          /* comment before semicolon - ok to move after semicolon */
+       i++ /* comment before opening curly brace */ {
+       }
+}
+
+
 // Line comments with tabs
 func _() {
        var finput *bufio.Reader        // input file
index 335e813918d804631b96e1177049e83a47e31cb0..4a9ea4742a11966478367d4461021810b97892e3 100644 (file)
@@ -429,6 +429,40 @@ func _() {
        /* closing curly brace should be on new line */ }
 
 
+// Comments immediately adjacent to punctuation (for which the go/printer
+// may obly have estimated position information) must remain after the punctuation.
+func _() {
+       _ = T{
+               1,    // comment after comma
+               2,    /* comment after comma */
+               3  ,  // comment after comma
+       }
+       _ = T{
+               1  ,// comment after comma
+               2  ,/* comment after comma */
+               3,// comment after comma
+       }
+       _ = T{
+               /* comment before literal */1,
+               2/* comment before comma - ok to move after comma */,
+               3  /* comment before comma - ok to move after comma */  ,
+       }
+
+       for
+               i=0;// comment after semicolon
+               i<9;/* comment after semicolon */
+               i++{// comment after opening curly brace
+       }
+
+       // TODO(gri) the last comment in this example should be aligned */
+       for
+               i=0;// comment after semicolon
+               i<9/* comment before semicolon - ok to move after semicolon */;
+               i++ /* comment before opening curly brace */ {
+       }
+}
+
+
 // Line comments with tabs
 func _() {
 var    finput          *bufio.Reader                   // input file
index 39c4c34164e487c8b23e07e3d390627f7e20329b..0436bf11ff5107e16f737885f4b6dfc0fdacd912 100644 (file)
@@ -335,7 +335,6 @@ func _() {
                2,
                3,
        )
-       // TODO(gri) the cases below are not correct yet
        f(1,
                2,
                3)      // comment
@@ -348,8 +347,7 @@ func _() {
                3)      // comment
        f(1,
                2,
-               3 // comment
-               ,
+               3,      // comment
        )
 }
 
index 0c0b813a63cb52fd4c846db025491ce9b52e2007..b79916624105aa7d7521d21cc62ce74c07e06c5b 100644 (file)
@@ -328,7 +328,6 @@ func _() {
                2,
                3,
        )
-       // TODO(gri) the cases below are not correct yet
        f(1,
                2,
                3)  // comment
index cba8636c48332acfe6fd1c4a88967b3c7b7c3baf..d7009b3773c61e7c57e5cdb3a90cf599d6866d66 100644 (file)
@@ -335,7 +335,6 @@ func _() {
                2,
                3,
        )
-       // TODO(gri) the cases below are not correct yet
        f(1,
                2,
                3)      // comment
@@ -348,8 +347,7 @@ func _() {
                3)      // comment
        f(1,
                2,
-               3 // comment
-               ,
+               3,      // comment
        )
 }