Skip to content

Commit

Permalink
go/printer: set prefix correctly when all comment lines blank
Browse files Browse the repository at this point in the history
Fixes (second half of?) golang issue 5128.

In stripCommonPrefix, the prefix was correctly calculated in all cases
except when there are more than 2 lines but all lines except the first
and last are blank.
This fix detects that case and correctly sets the prefix calculated
from the last line.
That fixes the issue where gofmt was continuously adding indentation in
such cases on every format operation.
  • Loading branch information
dmitshur committed Feb 2, 2015
1 parent bfc69a3 commit f416cec
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions 101/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,17 +506,24 @@ func stripCommonPrefix(lines []string) {
var prefix string
if len(lines) > 2 {
first := true
allBlank := true
for i, line := range lines[1 : len(lines)-1] {
switch {
case isBlank(line):
lines[1+i] = "" // range starts with lines[1]
case first:
prefix = commonPrefix(line, line)
first = false
allBlank = false
default:
prefix = commonPrefix(prefix, line)
allBlank = false
}
}
if allBlank { // all lines other than first and last are blank
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)
Expand Down

0 comments on commit f416cec

Please sign in to comment.