]> Cypherpunks repositories - gostls13.git/commitdiff
go/printer: set prefix correctly when all comment lines blank
authorDmitri Shuralyov <shurcooL@gmail.com>
Mon, 2 Feb 2015 09:28:10 +0000 (01:28 -0800)
committerRobert Griesemer <gri@golang.org>
Mon, 2 Feb 2015 18:41:05 +0000 (18:41 +0000)
In stripCommonPrefix, the prefix was correctly calculated in all cases,
except one. That unhandled case is when there are more than 2 lines,
but all lines are blank (other than the first and last lines,
which contain /* and */ respectively).
This change detects that case and correctly sets the prefix calculated
from the last line. This is consistent with the (correct) behavior
that happens when there's at least one non-blank line.
That fixes issue #9751 that occurs for problematic input,
where cmd/gofmt and go/source would insert extra indentation on
every format operation. It also allows go/printer itself to print
such parsed files in an expected way.

Fixes #9751.

Change-Id: Id3dfb945beb59ffad3705085a3c285fca30a5f87
Reviewed-on: https://go-review.googlesource.com/3684
Reviewed-by: Robert Griesemer <gri@golang.org>
src/go/printer/printer.go
src/go/printer/testdata/comments.golden
src/go/printer/testdata/comments.input

index 280c697a0dd7caced68425182c1c11a4da0c9074..5160757e142aceab954daf7af888df7360a9244c 100644 (file)
@@ -496,9 +496,10 @@ func stripCommonPrefix(lines []string) {
        // Compute maximum common white prefix of all but the first,
        // last, and blank lines, and replace blank lines with empty
        // lines (the first line starts with /* and has no prefix).
-       // In case of two-line comments, consider the last line for
-       // the prefix computation since otherwise the prefix would
-       // be empty.
+       // In cases where only the first and last lines are not blank,
+       // such as two-line comments, or comments where all inner lines
+       // are blank, consider the last line for the prefix computation
+       // since otherwise the prefix would be empty.
        //
        // Note that the first and last line are never empty (they
        // contain the opening /* and closing */ respectively) and
@@ -517,6 +518,10 @@ func stripCommonPrefix(lines []string) {
                                prefix = commonPrefix(prefix, line)
                        }
                }
+               if first { // all lines were blank (except first and last)
+                       line := lines[len(lines)-1]
+                       prefix = commonPrefix(line, line)
+               }
        } else { // len(lines) == 2, lines cannot be blank (contain /* and */)
                line := lines[1]
                prefix = commonPrefix(line, line)
index b1af7958a96fd93e12ea9b3b181006011030dba5..849fa624489a61eeeea84bd66b6bce63fea9670f 100644 (file)
@@ -413,6 +413,68 @@ func _() {
                aligned line */
 }
 
+// Issue 9751.
+func _() {
+       /*a string
+
+       b string*/
+
+       /*A string
+
+
+
+       Z string*/
+
+       /*a string
+
+       b string
+
+       c string*/
+
+       {
+               /*a string
+               b string*/
+
+               /*a string
+
+               b string*/
+
+               /*a string
+
+               b string
+
+               c string*/
+       }
+
+       {
+               /*a string
+               b string*/
+
+               /*a string
+
+               b string*/
+
+               /*a string
+
+               b string
+
+               c string*/
+       }
+
+       /*
+        */
+
+       /*
+
+        */
+
+       /*
+
+        * line
+
+        */
+}
+
 /*
  * line
  * of
index 983e2b2c97e25a54bd0b96ba1d7e7a05985ebeb0..30cd23c6dd4cd006ca348d172aea0181e951e7be 100644 (file)
@@ -418,6 +418,68 @@ func _() {
                aligned line */
 }
 
+// Issue 9751.
+func _() {
+       /*a string
+
+       b string*/
+
+       /*A string
+
+
+
+       Z string*/
+
+       /*a string
+
+       b string
+
+       c string*/
+
+       {
+               /*a string
+b string*/
+
+               /*a string
+
+b string*/
+
+               /*a string
+
+b string
+
+c string*/
+       }
+
+       {
+               /*a string
+                               b string*/
+
+               /*a string
+
+                               b string*/
+
+               /*a string
+
+                               b string
+
+                               c string*/
+       }
+
+       /*
+       */
+
+       /*
+
+       */
+
+       /*
+
+        * line
+
+       */
+}
+
 /*
  * line
  * of