]> Cypherpunks repositories - gostls13.git/commitdiff
more comment formatting:
authorRobert Griesemer <gri@golang.org>
Thu, 5 Nov 2009 06:07:13 +0000 (22:07 -0800)
committerRobert Griesemer <gri@golang.org>
Thu, 5 Nov 2009 06:07:13 +0000 (22:07 -0800)
- preserve (some) indentation of comment text for /*-style comments
  even if the first comment line does not contain any text that might
  suggest the "correct" indentation
- enabled because otherwise existing larger comments get re-formatted
  (this will not introduce a lot of changes since comments of this
  kind - until now - were not changed with respect to indentation)

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

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

index 6c649eb38c6aea2e9e73a913a9ff793e4737a715..84238d8d3d6fa783d98faa5f4106bbe8cf105a93 100644 (file)
@@ -443,27 +443,45 @@ func stripCommonPrefix(lines [][]byte) {
                // Determine the white space on the first line after the /*
                // and before the beginning of the comment text, assume two
                // blanks instead of the /* unless the first character after
-               // the /* is a tab. This whitespace may be found as suffix
-               // in the common prefix.
+               // the /* is a tab. If the first comment line is empty but
+               // for the opening /*, assume up to 3 blanks or a tab. This
+               // whitespace may be found as suffix in the common prefix.
                first := lines[0];
-               suffix := make([]byte, len(first));
-               n := 2;
-               for n < len(first) && first[n] <= ' ' {
-                       suffix[n] = first[n];
-                       n++;
-               }
-               if n > 2 && suffix[2] == '\t' {
-                       // assume the '\t' compensates for the /*
-                       suffix = suffix[2:n];
+               if isBlank(first[2 : len(first)]) {
+                       // no comment text on the first line:
+                       // reduce prefix by up to 3 blanks or a tab
+                       // if present - this keeps comment text indented
+                       // relative to the /* and */'s if it was indented
+                       // in the first place
+                       i := len(prefix);
+                       for n := 0; n < 3 && i > 0 && prefix[i-1] == ' '; n++ {
+                               i--;
+                       }
+                       if i == len(prefix) && i > 0 && prefix[i-1] == '\t' {
+                               i--;
+                       }
+                       prefix = prefix[0:i];
                } else {
-                       // otherwise assume two blanks
-                       suffix[0], suffix[1] = ' ', ' ';
-                       suffix = suffix[0:n];
-               }
-               // Shorten the computed common prefix by the length of
-               // suffix, if it is found as suffix of the prefix.
-               if bytes.HasSuffix(prefix, suffix) {
-                       prefix = prefix[0 : len(prefix) - len(suffix)];
+                       // comment text on the first line
+                       suffix := make([]byte, len(first));
+                       n := 2;
+                       for n < len(first) && first[n] <= ' ' {
+                               suffix[n] = first[n];
+                               n++;
+                       }
+                       if n > 2 && suffix[2] == '\t' {
+                               // assume the '\t' compensates for the /*
+                               suffix = suffix[2:n];
+                       } else {
+                               // otherwise assume two blanks
+                               suffix[0], suffix[1] = ' ', ' ';
+                               suffix = suffix[0:n];
+                       }
+                       // Shorten the computed common prefix by the length of
+                       // suffix, if it is found as suffix of the prefix.
+                       if bytes.HasSuffix(prefix, suffix) {
+                               prefix = prefix[0 : len(prefix) - len(suffix)];
+                       }
                }
        }
 
index 5772c56298afb2e3181c33def5e9977412ae3d36..7bed90bb10a5e4872203c2e8fed95c4920867c17 100644 (file)
@@ -168,6 +168,53 @@ func _() {
 }
 
 
+func _() {
+       /*
+          freestanding comment
+          aligned              line
+          aligned line
+       */
+}
+
+func _() {
+       /*
+          freestanding comment
+          aligned              line
+          aligned line
+       */
+}
+
+func _() {
+       /*
+          freestanding comment
+          aligned              line
+          aligned line */
+}
+
+func _() {
+       /*
+               freestanding comment
+               aligned         line
+               aligned line
+       */
+}
+
+func _() {
+       /*
+               freestanding comment
+               aligned         line
+               aligned line
+       */
+}
+
+func _() {
+       /*
+               freestanding comment
+               aligned         line
+               aligned line */
+}
+
+
 /*
  * line
  * of
@@ -194,6 +241,18 @@ blank line in middle:
 with no leading spaces on blank line.
 */
 
+/*
+   aligned in middle
+   here
+           not here
+*/
+
+/*
+       blank line in middle:
+
+       with no leading spaces on blank line.
+*/
+
 func _() {
        /*
         * line
@@ -202,15 +261,15 @@ func _() {
         */
 
        /*
-       aligned in middle
-       here
-               not here
+               aligned in middle
+               here
+                       not here
        */
 
        /*
-       blank line in middle:
+               blank line in middle:
 
-       with no leading spaces on blank line.
+               with no leading spaces on blank line.
        */
 }
 
index 05399a3c6d01c926bc590215b4a956d3a3505b1c..b562241671cbb158b4022f608edb89bbcfbac862 100644 (file)
@@ -168,6 +168,53 @@ func _() {
 }
 
 
+func _() {
+       /*
+          freestanding comment
+          aligned              line
+          aligned line
+       */
+}
+
+func _() {
+       /*
+          freestanding comment
+          aligned              line
+          aligned line
+          */
+}
+
+func _() {
+       /*
+          freestanding comment
+          aligned              line
+          aligned line */
+}
+
+func _() {
+       /*
+               freestanding comment
+               aligned         line
+               aligned line
+       */
+}
+
+func _() {
+       /*
+               freestanding comment
+               aligned         line
+               aligned line
+               */
+}
+
+func _() {
+       /*
+               freestanding comment
+               aligned         line
+               aligned line */
+}
+
+
 /*
  * line
  * of
@@ -194,6 +241,18 @@ blank line in middle:
 with no leading spaces on blank line.
 */
 
+/*
+   aligned in middle
+   here
+           not here
+*/
+
+/*
+       blank line in middle:
+
+       with no leading spaces on blank line.
+*/
+
 func _() {
        /*
         * line