@@ -17,7 +17,6 @@ package printer
1717import (
1818 "github.com/bufbuild/protocompile/experimental/ast"
1919 "github.com/bufbuild/protocompile/experimental/dom"
20- "github.com/bufbuild/protocompile/experimental/seq"
2120)
2221
2322// printDecl dispatches to the appropriate printer based on declaration kind.
@@ -182,13 +181,15 @@ func (p *printer) printSignature(sig ast.Signature) {
182181 inputs := sig .Inputs ()
183182 if ! inputs .Brackets ().IsZero () {
184183 p .withGroup (func (p * printer ) {
185- p .printFusedBrackets (inputs .Brackets (), gapNone , func (p * printer ) {
186- p .withIndent (func (indented * printer ) {
187- indented .push (dom .TextIf (dom .Broken , "\n " ))
188- indented .printTypeListContents (inputs )
189- p .push (dom .TextIf (dom .Broken , "\n " ))
190- })
184+ openTok , closeTok := inputs .Brackets ().StartEnd ()
185+ slots := p .trivia .scopeSlots (inputs .Brackets ().ID ())
186+ p .printToken (openTok , gapNone )
187+ p .withIndent (func (indented * printer ) {
188+ indented .push (dom .TextIf (dom .Broken , "\n " ))
189+ indented .printTypeListContents (inputs , slots )
190+ p .push (dom .TextIf (dom .Broken , "\n " ))
191191 })
192+ p .printToken (closeTok , gapNone )
192193 })
193194 }
194195
@@ -197,45 +198,56 @@ func (p *printer) printSignature(sig ast.Signature) {
197198 outputs := sig .Outputs ()
198199 if ! outputs .Brackets ().IsZero () {
199200 p .withGroup (func (p * printer ) {
200- p .printFusedBrackets (outputs .Brackets (), gapSpace , func (p * printer ) {
201- p .withIndent (func (indented * printer ) {
202- indented .push (dom .TextIf (dom .Broken , "\n " ))
203- indented .printTypeListContents (outputs )
204- p .push (dom .TextIf (dom .Broken , "\n " ))
205- })
201+ openTok , closeTok := outputs .Brackets ().StartEnd ()
202+ slots := p .trivia .scopeSlots (outputs .Brackets ().ID ())
203+ p .printToken (openTok , gapSpace )
204+ p .withIndent (func (indented * printer ) {
205+ indented .push (dom .TextIf (dom .Broken , "\n " ))
206+ indented .printTypeListContents (outputs , slots )
207+ p .push (dom .TextIf (dom .Broken , "\n " ))
206208 })
209+ p .printToken (closeTok , gapNone )
207210 })
208211 }
209212 }
210213}
211214
212- func (p * printer ) printTypeListContents (list ast.TypeList ) {
215+ func (p * printer ) printTypeListContents (list ast.TypeList , slots [] slot ) {
213216 gap := gapNone
214217 for i := range list .Len () {
218+ p .emitSlot (slots , i )
215219 if i > 0 {
216220 p .printToken (list .Comma (i - 1 ), gapNone )
217- // Use Softline here so args break onto new lines if needed
218221 gap = gapSoftline
219222 }
220223 p .printType (list .At (i ), gap )
221224 }
225+ p .emitSlot (slots , list .Len ())
222226}
223227
224228func (p * printer ) printBody (body ast.DeclBody ) {
225229 if body .IsZero () {
226230 return
227231 }
228232
229- p .printFusedBrackets (body .Braces (), gapSpace , func (child * printer ) {
230- if body .Decls ().Len () > 0 {
231- child .withIndent (func (indented * printer ) {
232- for d := range seq .Values (body .Decls ()) {
233- indented .printDecl (d )
234- }
235- indented .printRemaining ()
236- })
237- }
238- })
233+ braces := body .Braces ()
234+ if braces .IsZero () {
235+ return
236+ }
237+
238+ openTok , closeTok := braces .StartEnd ()
239+ slots := p .trivia .scopeSlots (braces .ID ())
240+
241+ p .printToken (openTok , gapSpace )
242+
243+ closeGap := gapNone
244+ if body .Decls ().Len () > 0 || len (slots ) > 0 {
245+ closeGap = gapNewline
246+ p .withIndent (func (indented * printer ) {
247+ indented .printScopeDecls (slots , body .Decls ())
248+ })
249+ }
250+ p .printToken (closeTok , closeGap )
239251}
240252
241253func (p * printer ) printRange (r ast.DeclRange ) {
@@ -258,25 +270,36 @@ func (p *printer) printCompactOptions(co ast.CompactOptions) {
258270 if co .IsZero () {
259271 return
260272 }
273+
274+ brackets := co .Brackets ()
275+ if brackets .IsZero () {
276+ return
277+ }
278+
279+ openTok , closeTok := brackets .StartEnd ()
280+ slots := p .trivia .scopeSlots (brackets .ID ())
281+
261282 p .withGroup (func (p * printer ) {
262- p .printFusedBrackets (co .Brackets (), gapSpace , func (p * printer ) {
263- entries := co .Entries ()
264- p .withIndent (func (indented * printer ) {
265- for i := range entries .Len () {
266- if i > 0 {
267- indented .printToken (entries .Comma (i - 1 ), gapNone )
268- indented .printPath (entries .At (i ).Path , gapSoftline )
269- } else {
270- indented .printPath (entries .At (i ).Path , gapNone )
271- }
272-
273- opt := entries .At (i )
274- if ! opt .Equals .IsZero () {
275- indented .printToken (opt .Equals , gapSpace )
276- indented .printExpr (opt .Value , gapSpace )
277- }
283+ p .printToken (openTok , gapSpace )
284+ entries := co .Entries ()
285+ p .withIndent (func (indented * printer ) {
286+ for i := range entries .Len () {
287+ indented .emitSlot (slots , i )
288+ if i > 0 {
289+ indented .printToken (entries .Comma (i - 1 ), gapNone )
290+ indented .printPath (entries .At (i ).Path , gapSoftline )
291+ } else {
292+ indented .printPath (entries .At (i ).Path , gapNone )
278293 }
279- })
294+
295+ opt := entries .At (i )
296+ if ! opt .Equals .IsZero () {
297+ indented .printToken (opt .Equals , gapSpace )
298+ indented .printExpr (opt .Value , gapSpace )
299+ }
300+ }
301+ p .emitSlot (slots , entries .Len ())
280302 })
303+ p .printToken (closeTok , gapNone )
281304 })
282305}
0 commit comments