Skip to content

pxf: formatDocument keeps every directive in document order (#188) - #192

Merged
trendvidia merged 2 commits into
mainfrom
pxf/format-directives-188
Nov 6, 2026
Merged

trendvidia merged 2 commits into
mainfrom
pxf/format-directives-188

Conversation

@bfrancojr

Copy link
Copy Markdown
Collaborator

Fixes #188.

Format.formatDocument used to drop @proto. It also wrote every named directive before every dataset, so a document with @dataset t.v1.T (id) / (1) and then @entry x { a = 1 } came out with the @entry first. Order matters here: an anonymous @proto binds the typed directive after it.

It now keeps every directive in document order, each after its comments, as the draft requires (trendvidia/protowire#558, #559) and as protowire-go does since protowire-go#262. @type is still written first, since it binds no @proto. A dataset is its header and then one row per line, an absent cell left empty and null written. A named directive's body and a @proto body are written as they were read. A document whose only directive is @type formats exactly as before.

Two smaller output changes come with matching Go byte for byte: directives are no longer separated by blank lines, and a dataset with no type (after an anonymous @proto) is written @dataset (id) rather than @dataset (id).

This ports Go's topDirectives, formatNamedDirective, formatDataset and formatProto. The new FormatDirectivesTest ports Go's format_directives_test.go and adds the issue's two inputs. Every case also checks that the output re-parses to the same directives and that formatting it again changes nothing.

Evidence

  • New tests: FormatDirectivesTest, 8 tests, 0 failures (from its JUnit XML).
  • Negative control: with main's Format.java and the new tests, the runner printed 8 tests completed, 7 failed. The one that passed, onlyTypeUnchanged, pins behaviour that did not change. Failing: dataset, entryAndNamedDirective, protoShapes, directivesKeepSourceOrder, directiveComments, issue188ProtoIsKept, issue188DatasetBeforeEntryKeepsOrder.
  • Full build: ./gradlew --no-daemon --continue build -PpxfJavaMeta.repo=/nonexistent (the plugin at the pinned pxfJavaMeta.ref, as CI builds it): BUILD SUCCESSFUL. JUnit XML across all modules: 830 tests, 0 failures, 0 errors.
  • Conformance gates, run locally against protowire main (a0b1417) and protowire-go main (903b4a7), REQUIRE_PORTS=go,java,java-lite:
    • text: ✓ Every port that ran writes the golden text. (173 pairs)
    • security: ✓ All ports conform to docs/HARDENING.md on the adversarial corpus. (222 pairs)
    • write: ✓ Every port that ran writes protowire-go's bytes. (256 pairs)
  • Byte comparison with Go: 14 directive-heavy documents (comments between directives, @entry with label and type, a bare @flag, @flag with two prefixes, a dataset with list, timestamp, duration, bytes, escaped-string, non-ASCII and absent cells, a triple-quoted @proto, @type after @proto, the issue's two inputs) formatted by protowire-go main's FormatDocument and by this branch. 13 are byte-identical; both ports refuse @type beside @dataset with the same error. The one difference is not about directives. The Java parser does not keep a trailing comment on an entry, so b = 1 # tc comes out as b = 1, or with # tc moved onto the next line when another entry follows. That happens on main too, with or without directives, and is not changed here.

formatDocument dropped @proto and wrote every named directive before
every dataset. The draft requires a formatter to keep every directive in
document order (trendvidia/protowire#558, #559): an anonymous @proto
binds the typed directive after it, so order is part of the meaning.

Port protowire-go's topDirectives, formatNamedDirective, formatDataset
and formatProto: the three directive lists are merged back by source
offset, each directive written after its comments. @type stays first. A
document whose only directive is @type formats as before.

FormatDirectivesTest ports Go's format_directives_test.go and adds the
issue's two inputs; each case checks the output re-parses to the same
directives and is a fixed point.
@trendvidia
trendvidia merged commit 62512ec into main Nov 6, 2026
4 checks passed
@trendvidia
trendvidia deleted the pxf/format-directives-188 branch November 6, 2026 18:52
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.

pxf: formatDocument drops @proto, and writes named directives before datasets whatever the source order

2 participants