Skip to content

Add experimental/ast/printer package#657

Open
emcfarlane wants to merge 104 commits into
mainfrom
ed/printer2
Open

Add experimental/ast/printer package#657
emcfarlane wants to merge 104 commits into
mainfrom
ed/printer2

Conversation

@emcfarlane
Copy link
Copy Markdown
Contributor

@emcfarlane emcfarlane commented Jan 22, 2026

This adds experimental/ast/printer, an AST printer for protobuf files. The printer has two entrypoints, PrintFile (for printing an entire ast.File) and Print (for printing individual decls). Both take a set of printer options, which includes whether or not to format the printed output and/or apply any AST edits prior to printing.

The printer preserves comments and whitespace using a trivia index. Each comment is classified as either attached (bound to a specific token) or detached (bound to a positional slot between declarations). Attached comments travel with their token when declarations are moved; detached comments stay in place. At print time, the printer zips slot trivia with children within each scope, and looks up attached trivia when emitting individual tokens. This works identically for natural and synthetic tokens (no special-casing needed). Synthetic tokens won't have a trivia and fallback to the declared gap. This gap is also what will be used to inform how to format the trivia between tokens when formatting.

A set of formatting options have been pre-configured to match the behaviour of the legacy protobuf formatter, and this has been confirmed through conformance tests with legacy formatter goldens. To ensure conformance and for regression testing, goldens to verify the legacy configurations have been included in the formatter tests (and verified against the current legacy formatter) and the legacy formatter behaviour tests have been vendored locally as a regression test.

Copy link
Copy Markdown
Member

@doriable doriable left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API looks very reasonable to me, I just had a few nitpicks/discussion points for clarifying a few things. Thank you!

Comment thread experimental/printer/printer.go Outdated
Comment thread experimental/printer/printer.go Outdated
Comment thread experimental/printer/printer.go Outdated
Comment thread experimental/ast/printer/printer_test.go Outdated
@emcfarlane emcfarlane changed the title Add experimental/printer package Add experimental/ast/printer package Feb 6, 2026
@emcfarlane emcfarlane marked this pull request as ready for review February 17, 2026 16:52
Comment thread experimental/ast/printer/printer.go Outdated
)

// PrintFile renders an AST file to protobuf source text.
func PrintFile(file *ast.File, opts Options) string {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want this to take function options right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean type Option func(...)? Was following dom and report options

Comment thread experimental/ast/printer/printer.go Outdated
Comment on lines +135 to +136
// the pending buffer so that adjacent pure-newline runs are combined into a
// single kindBreak dom tag, preventing the dom from merging them and
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be dealt with by making kindBreak a bit more sophisticated?

Comment thread experimental/ast/printer/trivia.go
@emcfarlane emcfarlane marked this pull request as draft March 11, 2026 19:23
Add TestBufFormat that reads buf's bufformat testdata (54 test cases)
and compares printer output against golden files. Currently 35/54 pass.

Fixes:
- Empty files produce empty output instead of trailing newline
- New gapInline style keeps punctuation on same line as preceding
  comments: `value /* comment */;` instead of breaking to new line
- New gapGlue style for path separators glues comments without
  spaces: `header/*comment*/.v1`
- Preserve source blank lines after detached comments using actual
  newline count from pending trivia tokens
- Body declarations at file level only get blank lines when source
  had them, instead of unconditionally
- Compound string first element on new indented line after `=`
- Strip trailing whitespace from comments in format mode
Add test cases documenting 8 remaining formatting issues:
- Issue 1: trailing // before ] should become /* */ on single-line
- Issue 2: trailing comment after , should stay inline
- Issue 3: comment after [ opener should expand to multi-line
- Issue 5: comment before } in enum (already passes)
- Issue 6: EOF comment after blank line should preserve blank line
- Issue 7: block comments in RPC parens should not add extra spaces
- Issue 8: extension path comments should preserve spaces
- Issue 9: message literal with block comments should expand
Two formatting fixes:
- Extract inline trailing comments after commas in walkDecl so they
  stay on the same line as the comma instead of becoming leading
  trivia on the next token
- Preserve blank lines before EOF comments by checking
  trivia.blankBeforeClose in printFile
@doriable doriable marked this pull request as ready for review May 12, 2026 22:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants