Skip to content

Commit 928c83f

Browse files
dmitshurgriesemer
authored andcommitted
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 <[email protected]>
1 parent 187cccb commit 928c83f

File tree

3 files changed

+132
-3
lines changed

3 files changed

+132
-3
lines changed

src/go/printer/printer.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,10 @@ func stripCommonPrefix(lines []string) {
496496
// Compute maximum common white prefix of all but the first,
497497
// last, and blank lines, and replace blank lines with empty
498498
// lines (the first line starts with /* and has no prefix).
499-
// In case of two-line comments, consider the last line for
500-
// the prefix computation since otherwise the prefix would
501-
// be empty.
499+
// In cases where only the first and last lines are not blank,
500+
// such as two-line comments, or comments where all inner lines
501+
// are blank, consider the last line for the prefix computation
502+
// since otherwise the prefix would be empty.
502503
//
503504
// Note that the first and last line are never empty (they
504505
// contain the opening /* and closing */ respectively) and
@@ -517,6 +518,10 @@ func stripCommonPrefix(lines []string) {
517518
prefix = commonPrefix(prefix, line)
518519
}
519520
}
521+
if first { // all lines were blank (except first and last)
522+
line := lines[len(lines)-1]
523+
prefix = commonPrefix(line, line)
524+
}
520525
} else { // len(lines) == 2, lines cannot be blank (contain /* and */)
521526
line := lines[1]
522527
prefix = commonPrefix(line, line)

src/go/printer/testdata/comments.golden

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,68 @@ func _() {
413413
aligned line */
414414
}
415415

416+
// Issue 9751.
417+
func _() {
418+
/*a string
419+
420+
b string*/
421+
422+
/*A string
423+
424+
425+
426+
Z string*/
427+
428+
/*a string
429+
430+
b string
431+
432+
c string*/
433+
434+
{
435+
/*a string
436+
b string*/
437+
438+
/*a string
439+
440+
b string*/
441+
442+
/*a string
443+
444+
b string
445+
446+
c string*/
447+
}
448+
449+
{
450+
/*a string
451+
b string*/
452+
453+
/*a string
454+
455+
b string*/
456+
457+
/*a string
458+
459+
b string
460+
461+
c string*/
462+
}
463+
464+
/*
465+
*/
466+
467+
/*
468+
469+
*/
470+
471+
/*
472+
473+
* line
474+
475+
*/
476+
}
477+
416478
/*
417479
* line
418480
* of

src/go/printer/testdata/comments.input

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,68 @@ func _() {
418418
aligned line */
419419
}
420420

421+
// Issue 9751.
422+
func _() {
423+
/*a string
424+
425+
b string*/
426+
427+
/*A string
428+
429+
430+
431+
Z string*/
432+
433+
/*a string
434+
435+
b string
436+
437+
c string*/
438+
439+
{
440+
/*a string
441+
b string*/
442+
443+
/*a string
444+
445+
b string*/
446+
447+
/*a string
448+
449+
b string
450+
451+
c string*/
452+
}
453+
454+
{
455+
/*a string
456+
b string*/
457+
458+
/*a string
459+
460+
b string*/
461+
462+
/*a string
463+
464+
b string
465+
466+
c string*/
467+
}
468+
469+
/*
470+
*/
471+
472+
/*
473+
474+
*/
475+
476+
/*
477+
478+
* line
479+
480+
*/
481+
}
482+
421483
/*
422484
* line
423485
* of

0 commit comments

Comments
 (0)