Skip to content

fix: measure a start tag with the text that stays on its line - #462

Merged
JuroOravec merged 2 commits into
mainfrom
fix/format-tag-width-tail
Oct 28, 2026
Merged

JuroOravec merged 2 commits into
mainfrom
fix/format-tag-width-tail

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

What this fixes

The formatter puts a start tag on one line when it fits its 100-column width. It measured the tag alone, plus the end tag of an empty element. So it joined a tag onto one line even when text it cannot move followed on that line:

<span
  class="cui-listbox__group-label"
  c-id="label_id"
  data-citry-ui-part="listbox-group-label"
>{{ label }}</span>

became one line of about 120 characters. A dry run of citry format over this repository added 13 lines that Ruff reports as E501. This blocks the repository-wide reformat.

What changes

  • The fit check counts the text that stays on the tag's line (same_line_tail in source.rs, used by fits_one_line in printer.rs). That text runs from > or /> to the end of the source line, or to the next start tag, which is measured on its own. It covers text, expressions, end tags and comments, so <div ...></div> and <span ...>{{ label }}</span> are measured as the lines they occupy.
  • A tag stays on one line when wrapping cannot help. Wrapping moves that text to a line that starts with > at the tag's column. When even that line would be over the width, as with a long sentence after <p class="lead">, a tag that fits by itself stays on one line.
  • The design doc (docs/design/template_formatter.md, the M0 fit rule) describes the new rule. It also states the trade-off of stopping at the next start tag. When an outer tag fits and a later tag on the same line does not, the later tag wraps, so its attributes can be indented far to the right. Measuring through later tags would wrap the outer tag first, but it also wraps tags on lines the formatter cannot otherwise shorten. The existing corpus case <main><c-if cond="show">... is one such line.

Not done here: counting the Python indent

The formatter measures a template in its own columns. A template inside a Python string sits 6 to 12 columns further right in the file. Counting that would need a new option on format_template(), which is a change to the PyO3 surface, and that needs a plan first. With this fix, formatted templates in this repository stay within Ruff's 119-character limit: a template line is at most 100 columns plus the string's indent.

Tests and checks

  • New corpus case tags/wrap-with-same-line-text. It covers:

    • a tag followed by {{ label }}</span>;
    • a tag followed by text;
    • the long-sentence guard, and the guard's boundary: a > line of exactly 100 columns wraps, and one of 101 stays joined;
    • <div ...></div>tail;
    • a self-closing /> tail;
    • a block child after >, which stays one line and moves the child.

    Its expected output came from the formatter and is stable on a second format. On main, the first two lines are joined past the width.

  • Existing checks. No existing corpus case changed. The crate's tests and clippy pass. The Python formatter tests pass with an extension built from this branch: citry_core, citry format, and the citry_lsp formatting tests.

  • On a copy of the repository's non-test paths, formatted with this branch:

    • E501 reports 21 lines, down from 36 with main's formatter and 23 before formatting;
    • a second format changes nothing.
  • Independent review. A reviewer fuzzed 3,600 random templates (nested inline and block tags, text, expressions, comments, CRLF). It found no new errors and no output that changes on a second format. It also read the 78 changed hunks in the repository copy. A second pass checked the guard's arithmetic at the boundary for > and />, and looked for output that alternates between two layouts on repeated formats; it found none. Its findings are folded in: the guard, the doc rewrite, the fixture boundaries and the wording. The design doc notes the guard's one limit: it checks only the > line, so a later line indented from that line moves right with it.

🤖 Generated with Claude Code

JuroOravec and others added 2 commits October 27, 2026 18:36
The formatter puts a start tag on one line when it fits the 100-column
width. It measured the tag alone, plus the end tag of an empty element, so
a tag followed by text it cannot move, as in
`<span class="..." c-id="..." data-...="...">{{ label }}</span>`, was
kept on one line well past the width. The formatter now also counts
the text after `>` that stays on the tag's line whether or not the tag
wraps. That text runs to the end of the source line, or to the next start
tag, which the formatter measures separately. When wrapping could not make
the line fit, because even the line that starts with `>` would be too
long, a tag that fits by itself stays on one line.

With this repository's Ruff configuration, E501 reports 21 long lines on a
formatted copy, down from 36 with the previous formatter and 23 before
formatting, and a second format changes nothing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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