From: Dmitri Shuralyov Date: Mon, 2 Feb 2015 09:28:10 +0000 (-0800) Subject: go/printer: set prefix correctly when all comment lines blank X-Git-Tag: go1.5beta1~2184 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=928c83ff2cd13e5973827b2abde43b6dc901ec85;p=gostls13.git go/printer: set prefix correctly when all comment lines blank 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 --- diff --git a/src/go/printer/printer.go b/src/go/printer/printer.go index 280c697a0d..5160757e14 100644 --- a/src/go/printer/printer.go +++ b/src/go/printer/printer.go @@ -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) diff --git a/src/go/printer/testdata/comments.golden b/src/go/printer/testdata/comments.golden index b1af7958a9..849fa62448 100644 --- a/src/go/printer/testdata/comments.golden +++ b/src/go/printer/testdata/comments.golden @@ -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 diff --git a/src/go/printer/testdata/comments.input b/src/go/printer/testdata/comments.input index 983e2b2c97..30cd23c6dd 100644 --- a/src/go/printer/testdata/comments.input +++ b/src/go/printer/testdata/comments.input @@ -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