Skip to content

feat(formatter): assign trivia across method bodies - #3142

Draft
floitsch wants to merge 1 commit into
floitsch/formatter-rewrite.50-early-triviafrom
floitsch/formatter-rewrite.60-body-trivia
Draft

floitsch wants to merge 1 commit into
floitsch/formatter-rewrite.50-early-triviafrom
floitsch/formatter-rewrite.60-body-trivia

Conversation

@floitsch

@floitsch floitsch commented Aug 5, 2026

Copy link
Copy Markdown
Member

This follow-up proves the early-trivia model across a complete, explicitly bounded method body.

  • lowers statement sequences with source bounds so comments without AST nodes still belong to the body
  • uses indentation to distinguish nested comments from comments belonging to an enclosing sequence
  • requires every comment in the formatted body region to be consumed exactly once
  • turns own-line line and block comments into statement-like layout items
  • freezes simple expression and return lines containing // while allowing indentation changes
  • freezes only an if header containing // and continues formatting its nested body
  • keeps inline block comments attached to their semantic expression
  • includes trailing comments after the final AST statement

This remains a deliberately narrow body slice: ordinary supported expressions, return statements, and if statements without else. Unsupported statement shapes fail explicitly rather than silently copying source.

The fixture is reparsed, compared structurally, and formatted again to check semantics and idempotence.

Tests: ctest --test-dir build/host --output-on-failure -R tests/ctest/format-(body|expression|layout|method|syntax|trivia)-test.cc
Also analyzed the Toit body fixture with toit.compile --analyze.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant