Add experimental/ast/printer package#657
Open
emcfarlane wants to merge 104 commits into
Open
Conversation
dbf4d2f to
4422e60
Compare
4422e60 to
c170def
Compare
doriable
reviewed
Feb 3, 2026
Member
doriable
left a comment
There was a problem hiding this comment.
The API looks very reasonable to me, I just had a few nitpicks/discussion points for clarifying a few things. Thank you!
experimental/printer packageexperimental/ast/printer package
ee5138a to
67878ec
Compare
mcy
reviewed
Feb 17, 2026
| ) | ||
|
|
||
| // PrintFile renders an AST file to protobuf source text. | ||
| func PrintFile(file *ast.File, opts Options) string { |
Member
There was a problem hiding this comment.
You want this to take function options right?
Contributor
Author
There was a problem hiding this comment.
Do you mean type Option func(...)? Was following dom and report options
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 |
Member
There was a problem hiding this comment.
Could this be dealt with by making kindBreak a bit more sophisticated?
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
…on trailing line to block comments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds
experimental/ast/printer, an AST printer for protobuf files. The printer has two entrypoints,PrintFile(for printing an entireast.File) andPrint(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.