Commit 928c83ff authored by Dmitri Shuralyov's avatar Dmitri Shuralyov Committed by Robert Griesemer

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/3684Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
parent 187cccbd
......@@ -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)
......
......@@ -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
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment