src/pkg/go/printer/printer.go | 31 +++++++++++++++++++++---------- src/pkg/go/printer/testdata/comments.golden | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/pkg/go/printer/testdata/comments.input | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++ diff --git a/src/pkg/go/printer/printer.go b/src/pkg/go/printer/printer.go index 304ba0b0ac284991799105739e501391089d288d..90f3e20161a0ecf263d2c06f7514738a4fc0a701 100644 --- a/src/pkg/go/printer/printer.go +++ b/src/pkg/go/printer/printer.go @@ -429,6 +429,7 @@ func stripCommonPrefix(lines [][]byte) { if len(lines) < 2 { return // at most one line - nothing to do } + // len(lines) >= 2 // The heuristic in this function tries to handle a few // common patterns of /*-style comments: Comments where @@ -441,18 +442,28 @@ // 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. + // + // Note that the first and last line are never empty (they + // contain the opening /* and closing */ respectively) and + // thus they can be ignored by the blank line check. var prefix []byte - for i, line := range lines { - switch { - case i == 0 || i == len(lines)-1: - // ignore - case isBlank(line): - lines[i] = nil - case prefix == nil: - prefix = commonPrefix(line, line) - default: - prefix = commonPrefix(prefix, line) + if len(lines) > 2 { + for i, line := range lines[1 : len(lines)-1] { + switch { + case isBlank(line): + lines[i+1] = nil + case prefix == nil: + prefix = commonPrefix(line, line) + default: + prefix = commonPrefix(prefix, line) + } } + } else { // len(lines) == 2 + line := lines[1] + prefix = commonPrefix(line, line) } /* diff --git a/src/pkg/go/printer/testdata/comments.golden b/src/pkg/go/printer/testdata/comments.golden index 741500c621bc4e8a486019051bab5adf8f4b7fa2..18a407478ecb8363e669d4f806bc9e096d45627e 100644 --- a/src/pkg/go/printer/testdata/comments.golden +++ b/src/pkg/go/printer/testdata/comments.golden @@ -217,6 +217,80 @@ aligned line aligned line */ } +func _() { + /* freestanding comment + aligned line + */ +} + +func _() { + /* freestanding comment + aligned line + */ +} + +func _() { + /* freestanding comment + aligned line */ +} + +func _() { + /* freestanding comment + aligned line + */ +} + +func _() { + /* freestanding comment + aligned line + */ +} + +func _() { + /* freestanding comment + aligned line */ +} + + +func _() { + /* + freestanding comment + aligned line + */ +} + +func _() { + /* + freestanding comment + aligned line + */ +} + +func _() { + /* + freestanding comment + aligned line */ +} + +func _() { + /* + freestanding comment + aligned line + */ +} + +func _() { + /* + freestanding comment + aligned line + */ +} + +func _() { + /* + freestanding comment + aligned line */ +} /* * line @@ -231,6 +305,24 @@ /* and another line * of * stars */ + +/* a line of + * stars */ + +/* and another line of + * stars */ + +/* a line of stars + */ + +/* and another line of + */ + +/* a line of stars + */ + +/* and another line of + */ /* aligned in middle diff --git a/src/pkg/go/printer/testdata/comments.input b/src/pkg/go/printer/testdata/comments.input index 700a4923069460b0ca73cd4454328e50caa8098f..8cba7e5a2cd4090bb132477e6ffcbb39011165e5 100644 --- a/src/pkg/go/printer/testdata/comments.input +++ b/src/pkg/go/printer/testdata/comments.input @@ -217,6 +217,80 @@ aligned line aligned line */ } +func _() { + /* freestanding comment + aligned line + */ +} + +func _() { + /* freestanding comment + aligned line + */ +} + +func _() { + /* freestanding comment + aligned line */ +} + +func _() { + /* freestanding comment + aligned line + */ +} + +func _() { + /* freestanding comment + aligned line + */ +} + +func _() { + /* freestanding comment + aligned line */ +} + + +func _() { + /* + freestanding comment + aligned line + */ +} + +func _() { + /* + freestanding comment + aligned line + */ +} + +func _() { + /* + freestanding comment + aligned line */ +} + +func _() { + /* + freestanding comment + aligned line + */ +} + +func _() { + /* + freestanding comment + aligned line + */ +} + +func _() { + /* + freestanding comment + aligned line */ +} /* * line @@ -231,6 +305,24 @@ /* and another line * of * stars */ + +/* a line of + * stars */ + +/* and another line of + * stars */ + +/* a line of stars +*/ + +/* and another line of +*/ + +/* a line of stars + */ + +/* and another line of + */ /* aligned in middle