printer: support multiple skipped comments

There may be multiple skipped comments in a row, convert
p.skippedComments to a list and add a test.

Change-Id: I30dcff269bee56fd51ef9513dab7c7885c44b7d7
This commit is contained in:
Colin Cross 2016-07-12 16:03:08 -07:00
parent 2e32b2facc
commit 6b6735d3be
2 changed files with 25 additions and 10 deletions

View file

@ -40,7 +40,7 @@ type printer struct {
indentList []int
wsBuf []byte
skippedComments *CommentGroup
skippedComments []*CommentGroup
}
func newPrinter(file *File) *printer {
@ -209,10 +209,7 @@ func (p *printer) printInLineCommentsBefore(pos scanner.Position) {
for p.curComment < len(p.comments) && p.comments[p.curComment].Pos().Offset < pos.Offset {
c := p.comments[p.curComment]
if c.Comments[0].Comment[0][0:2] == "//" || len(c.Comments[0].Comment) > 1 {
if p.skippedComments != nil {
panic("multiple skipped comments")
}
p.skippedComments = c
p.skippedComments = append(p.skippedComments, c)
} else {
p.printComment(c)
p.requestSpace()
@ -224,8 +221,10 @@ func (p *printer) printInLineCommentsBefore(pos scanner.Position) {
// Print any comments, including end of line comments, that appear _before_ the line specified
// by pos
func (p *printer) printEndOfLineCommentsBefore(pos scanner.Position) {
if p.skippedComments != nil {
p.printComment(p.skippedComments)
if len(p.skippedComments) > 0 {
for _, c := range p.skippedComments {
p.printComment(c)
}
p._requestNewline()
p.skippedComments = nil
}
@ -320,11 +319,11 @@ func (p *printer) printComment(cg *CommentGroup) {
// Print any comments that occur after the last token, and a trailing newline
func (p *printer) flush() {
if p.skippedComments != nil {
if !p.requestNewlinesForPos(p.skippedComments.Pos()) {
for _, c := range p.skippedComments {
if !p.requestNewlinesForPos(c.Pos()) {
p.requestSpace()
}
p.printComment(p.skippedComments)
p.printComment(c)
}
for p.curComment < len(p.comments) {
p.printComment(p.comments[p.curComment])

View file

@ -294,6 +294,22 @@ test {}
// Multiline
// Comment
`,
},
{
input: `
test // test
// test
{
}
`,
output: `
test { // test
// test
}
`,
},
}