Skip to content

fix(format): never add whitespace where the source had none - #493

Merged
JuroOravec merged 2 commits into
mainfrom
fix/format-never-add-whitespace
Oct 28, 2026
Merged

JuroOravec merged 2 commits into
mainfrom
fix/format-never-add-whitespace

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

What this fixes

citry format put a line break between two tags that were written next to each other when it judged both to be block tags. That line break is new text in the rendered HTML. For example, <td>a</td><td>b</td> became two lines, and the <td> elements then had a text node between them that was not there before.

A dry run of citry format --embedded off over the repository's non-test paths found 143 such places. They were between <td>, <tr>, <option>, <dt> and <dd> siblings, at the start and end of <c-fill> bodies, and at the edges of six Python templates written flush against their quotes (template = """<div>...</div>""").

The rule is now: the formatter re-indents whitespace the author wrote between block tags, and never puts whitespace where there was none.

<table><tr><td>a</td><td>b</td></tr></table>

stays as it is, while

<table> <tr><td>a</td><td>b</td></tr> </table>

becomes

<table>
  <tr><td>a</td><td>b</td></tr>
</table>

How

  • classify_gap in crates/citry_template_formatter/src/html.rs treats a gap with no whitespace as sensitive in every container. The printer then keeps it empty. The same classifier builds the before-and-after projection the formatter checks its output against, so an output that fills an empty gap is rejected instead of returned.
  • In a Python file, _canonicalize_multiline_framing in packages/py/citry/citry/analysis.py handles each edge of a template on its own. An edge where the template has whitespace (space, tab, newline, carriage return or form feed; not a non-breaking space) gets the usual layout, with the content on lines of its own. An edge that touches its quote keeps touching it, because a component renders the text at its template's edges. JavaScript and CSS strings still get the layout at both edges. This part is separate from the Rust change and can be reverted on its own if the edge rule should be different.

A template whose start touches the opening quote and that has to wrap a long tag now looks like this. Its later lines keep the columns the formatter gave them, counted from the left edge of the file, because Citry removes only indentation that every line shares, and the first line has none:

    template = """<div><c-CTreeGrid
       c-columns="columns"
       c-rows="rows"
     /></div>"""

The house style asks for multiline templates, so this only shows up in templates that do not follow it. Adding a newline after the opening quotes and before the closing ones opts back into the canonical framing.

Tests

  • New corpus case structural-layout.whitespace.no-gap-added: compact table, list, select and <c-fill> markup comes back unchanged, and the same <c-fill> written with spaces is re-indented. A new required feature label, no-gap-added, keeps the case from being dropped.
  • generated_boundary_combinations_converge now asserts that every compact parent and child pair comes back byte for byte, and checks that the spaced variants settle.
  • The corpus cases and tests that showed compact markup spread over lines now start from markup with whitespace, so they still test the layout. The Python host case python-hosts.framing.inline-structural-triple-double now locks the flush framing.
  • New tests: a compact document stays as written, a wrapped tag after no whitespace keeps its place on the line, every mix of empty and spaced gaps settles, and formatting a Python template keeps whether each of its edges has whitespace (including a non-breaking space at the edge). New host corpus cases cover a template that touches only its opening quote (indented-own-line-close) and one that touches only its closing quote (flush-close).
  • classify_gap and projection unit tests assert that an empty gap is never structural, and that filling one is a contract mismatch.
  • Two spaced cases were added to the browser probe in tests/e2e/test_formatter_semantics_e2e.py. I did not run that file, because it needs Playwright browsers.

Run locally:

  • cargo test -p citry_template_formatter, cargo clippy --no-deps -p citry_template_formatter --all-targets -- -D warnings, cargo fmt --check.
  • The Python formatter tests in citry, citry_core and citry_lsp, and the analysis, browser-analysis, embedded-provider, LSP server, project and semantic suites: all pass.
  • ruff check, ruff format --check, and mypy --platform linux on analysis.py.
  • The dry run again, with the render-equivalence script from the first run: 0 whitespace insertions remain, down from 143. The 11 other differences are parentheses added around {{ ... }} expressions, as before. citry format --check on the formatted copy reports 0 files to change.

Docs

The design doc (docs/design/template_formatter.md, sections 4, 6.1, 6.2, 6.3, 6.6, 6.7 and 14.1) states the rule. A short paragraph in docs_site/content/advanced/cli.md tells users how to spread compact markup over lines. The root CHANGELOG.md and packages/py/citry_core/CHANGELOG.md have entries, and the VS Code README says "lays out" rather than "expands".

One place still adds whitespace the author did not write: a formatted <script> or <style> body goes on lines of its own between its tags. That newline is inside JavaScript or CSS, so the page behaves the same, but the element's textContent changes. Section 6.7 of the design doc says so.

The attribute-per-line house rule from the same dry run is #492, and the repository reformat waits for it.

🤖 Generated with Claude Code

JuroOravec and others added 2 commits October 28, 2026 11:57
Two tags written with nothing between them render with nothing between
them, so a line break the formatter puts there is new text in the HTML. A
dry run over the repository found 143 such places: between `<td>`, `<tr>`,
`<option>`, `<dt>` and `<dd>` siblings, at the start and end of `<c-fill>`
bodies, and at the edges of templates written right against their quotes.

The formatter now treats a gap with no whitespace as one it must keep
exactly, inside any tag, so it leaves the gap empty, and the check that
compares the template before and after formatting rejects output that
fills it. Whitespace the author wrote between block tags is still
re-indented. In a Python file, each edge of a template is handled on its
own: an edge with whitespace gets the usual layout with the content on
lines of its own, and an edge that touches its quote keeps touching it.
JavaScript and CSS strings still get that layout at both edges.

The corpus cases and tests that showed compact markup being spread over
lines now start from markup with whitespace, so they keep testing the
layout. A new corpus case locks compact table, list, select and fill
markup as unchanged, and new tests cover a compact document, a wrapped tag
that keeps its place, every mix of empty and spaced gaps, and the edges of
Python templates.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit e121dbb into main Oct 28, 2026
0 of 30 checks passed
@JuroOravec
JuroOravec deleted the fix/format-never-add-whitespace branch October 28, 2026 16:20
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.

1 participant