Repository navigation
fix(format): never add whitespace where the source had none - #493
Merged
Merged
Conversation
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]>
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.
What this fixes
citry formatput 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 offover 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.
stays as it is, while
becomes
How
classify_gapincrates/citry_template_formatter/src/html.rstreats 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._canonicalize_multiline_framinginpackages/py/citry/citry/analysis.pyhandles 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:
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
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_convergenow asserts that every compact parent and child pair comes back byte for byte, and checks that the spaced variants settle.python-hosts.framing.inline-structural-triple-doublenow locks the flush framing.indented-own-line-close) and one that touches only its closing quote (flush-close).classify_gapand projection unit tests assert that an empty gap is never structural, and that filling one is a contract mismatch.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.citry,citry_coreandcitry_lsp, and the analysis, browser-analysis, embedded-provider, LSP server, project and semantic suites: all pass.ruff check,ruff format --check, andmypy --platform linuxonanalysis.py.{{ ... }}expressions, as before.citry format --checkon 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 indocs_site/content/advanced/cli.mdtells users how to spread compact markup over lines. The rootCHANGELOG.mdandpackages/py/citry_core/CHANGELOG.mdhave 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'stextContentchanges. 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