Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions experimental/ast/printer/decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,16 +338,20 @@ func (p *printer) printBody(body ast.DeclBody) {
forceBroken := triviaHasComments(trivia) ||
len(closeComments) > 0 ||
p.scopeHasAttachedComments(body.Braces())
if !forceBroken && !p.bodyShouldBreak(openTok, closeTok) {
if !forceBroken && !p.bodyShouldBreak(openTok, closeTok, body.Decls().Len()) {
decls := body.Decls()
p.withGroup(func(p *printer) {
p.withIndent(func(indented *printer) {
for i := range decls.Len() {
indented.printDecl(decls.At(i), gapSoftline)
}
// Only create the indented [dom.Group] in the case where there are decls
// to avoid an indent in the flat case with no decls.
if decls.Len() > 0 {
Comment thread
emcfarlane marked this conversation as resolved.
p.withGroup(func(p *printer) {
p.withIndent(func(indented *printer) {
for i := range decls.Len() {
indented.printDecl(decls.At(i), gapSoftline)
}
})
p.push(tagSoftlineFlat, tagSoftbreak)
})
p.push(tagSoftlineFlat, tagSoftbreak)
})
}
p.printToken(closeTok, gapNone)
return
}
Expand Down
6 changes: 3 additions & 3 deletions experimental/ast/printer/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,20 @@ func (p *printer) literalShouldBreak(openTok, closeTok token.Token, count int) b
// decl-bearing body scope (`{ ... }` on message, enum, service,
// oneof, extend, or RPC method):
//
// - [LayoutStrict]: always broken. Callers handle the empty-body
// - [LayoutStrict]: always broken for >0 decls. Callers handle the empty-body
// case (rendered as `{}`) before consulting this helper.
//
// - [LayoutDynamic]: broken if and only if source had a newline between open
// and close brace.
//
// Callers should OR the result with their own forceBroken signal
// (e.g. for scope-attached comments that require expansion).
func (p *printer) bodyShouldBreak(openTok, closeTok token.Token) bool {
func (p *printer) bodyShouldBreak(openTok, closeTok token.Token, declsLen int) bool {
switch p.options.Formatting.BodyLayout {
case LayoutDynamic:
return !sourceWasFlat(openTok, closeTok)
default: // LayoutStrict
return true
return declsLen > 0
}
}

Expand Down
5 changes: 5 additions & 0 deletions experimental/ast/printer/testdata/format/basic.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ import "acme/payment/v1/payment.proto"; /* trailing */
message Foo { /* Foo */
string name = 1;
}

message Bar {


}
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ option optimize_for = SPEED;
message Foo { /* Foo */
string name = 1;
}

message Bar {
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ option optimize_for = SPEED;
message Foo { /* Foo */
string name = 1;
}

message Bar {}
Loading