Skip to content

Commit c9a5f20

Browse files
max-sixtyclaude
andauthored
Run a diff row's fill to the end of its line (#784)
Pierre sizes the diff's code column `1fr`, which is the width of the box that scrolls, so a changed row painted its green or red only as far as the reader could already see. On the walkthrough's widest file the line runs 3,234px inside a 718px box and the fill stopped at 671px: scrolling right left every addition and deletion sitting on the file's plain paper with nothing to say which it was. The track now takes its floor from the longest line and keeps `1fr` over it, so a file whose lines all fit still fills its box rather than ending at its longest line. That floor measures everything standing in the column, which is where the other two changes come from. A thread outlet stands there with the lines, and what it contributes is not its remark's own length but the thread's own `max-width: 760px` cap plus the outlet's padding — a 796px contribution from any remark long enough to reach that cap, against the 671px of rows in the file it was measured on, so one ordinary remark set a file that fit its box scrolling sideways with 843px of content in 718px of room. Inline-size containment keeps the outlet out of the measure while it still stretches and wraps. And the wrap switch and the print sheet both force `pre-wrap` while a wrapped line still measures its unwrapped self, so both drop the floor. Three probes had to move with it, because the rows are now all one width — each sized to the longest line in its file — and a reading taken off a row's own box no longer says anything about that row's line. `DIFF_CLIPPING` and `_SELECT_IN_ROW` both measure the row's text now, by a Range over its contents, against the room the scrolling box gives it; without that, `DIFF_CLIPPING` reported the file's overhang for all 18 of one file's rows where one line overflows, and named a trailing-context line the reader could see whole. Each has an assertion holding it to naming the overflowing line, so it cannot quietly degenerate again. The new `DIFF_ROW_FILL` case asserts the fill reaches both the line's end and the box's edge, and reads again after a comment lands; every one of those was verified paired, green with the change and red without it. Measured on the 47-file patch: 3,769 rows relayout in 147-150ms against the baseline's 154-156ms, across two paired rounds, so the intrinsic sizing costs nothing here. One commit only adds a TODO: `lf-code` has a milder version of the same defect, where a short highlighted line's tint ends at the scrollport rather than at the block's width. Its notes share the column and position sticky, so that fix wants its own change. Thanks to @leaf-agent for both review findings — the untested containment and the degenerated probe. > _This was written by Claude Code on behalf of max-sixty_ --------- Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
1 parent 9bbd250 commit c9a5f20

4 files changed

Lines changed: 198 additions & 53 deletions

File tree

‎skills/leaf/packages/default/theme.css‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,8 +2087,15 @@ lf-tree .lf-tree-badge.del { background: var(--danger-tint); color: var(--danger
20872087
lf-code.lf-rendered pre { counter-reset: lf-code-line; padding: 12px 0; --lf-block-frame: 1; }
20882088
lf-code.lf-rendered pre:is(:focus-visible, .lf-focus-visible) { outline: var(--here-ring); --lf-here-ring: code-pre-light;
20892089
outline-offset: calc(-1 * var(--here-ring-w)); }
2090-
/* The same max-content floor as the shadow slice's line spans: an hi range's tint has
2091-
to run the whole scrolled line, not stop at the fold. */
2090+
/* An hi range's tint has to run the whole scrolled line, not stop at the fold. The diff
2091+
package answers the same question on its own rows, but from the track rather than the
2092+
line, because there every row has to end where the widest one does: this block's lines
2093+
are their own width, so a short hi line still stops at the scrollport rather than at
2094+
the block's own width.
2095+
TODO: give the rendered <pre> one column with that same floor, so every line ends
2096+
where the widest one does. A note shares the column and would need the diff outlet's
2097+
inline-size containment to stay out of its measure; notes also position sticky, which
2098+
is the part a grid could move. */
20922099
lf-code .lf-code-line { display: block; padding: 0 14px 0 0;
20932100
width: max-content; min-width: 100%; box-sizing: border-box; }
20942101
lf-code .lf-code-line::before { counter-increment: lf-code-line;

‎skills/leaf/packages/diff/theme.css‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,19 @@ lf-diff:not(.lf-rendered) > pre {
200200
--diffs-bg-addition-emphasis: color-mix(in srgb, var(--ok) 34%, var(--pre-bg));
201201
--diffs-bg-deletion-emphasis: color-mix(in srgb, var(--danger) 34%, var(--pre-bg));
202202
}
203+
/* A changed row's fill stops where the scrollport does. Pierre gives the code column
204+
`1fr`, which is the width of the box that scrolls, so a line running past that box
205+
paints its green or red only as far as the reader could already see: scrolling right
206+
ran the fill out mid-line and left the rest of the row on the file's plain paper,
207+
with the added or removed text sitting on nothing. The track takes its floor from the
208+
longest line instead, and keeps `1fr` over it so a file whose lines all fit still
209+
fills the box rather than ending at its longest line. `--lf-diff-row-fill` is that
210+
floor, and wrapping drops it (below): a wrapped line still measures its whole
211+
unwrapped self at max-content, so a floor left in place would size the column to the
212+
very line the reader asked to have wrapped. */
213+
:host(.lf-rendered) code[data-code] {
214+
--diffs-code-grid: var(--diffs-grid-number-column-width)
215+
minmax(var(--lf-diff-row-fill, max-content), 1fr); }
203216
:host(.lf-rendered) pre { margin: 0; border: none; border-top: 1px solid var(--code-border);
204217
border-radius: 0; overflow-x: auto; color: var(--code-ink); }
205218
/* The scroll region's tab stop (lf-diff.js): reachable is only half — focus has to
@@ -214,7 +227,16 @@ lf-diff:not(.lf-rendered) > pre {
214227
min-width: 0; border-block: 1px solid var(--rule);
215228
background: var(--paper); }
216229
.lf-diff-thread-gutter { border-inline-end: 1px solid var(--rule); }
217-
.lf-diff-thread-outlet { padding: 10px clamp(10px, 2vw, 18px); white-space: normal; }
230+
/* The outlet stands in the code column with the lines, so the floor above measures it
231+
too, and what it measures is not the remark's own length: the thread inside caps
232+
itself at 760px (below), which with this padding is a 796px contribution from any
233+
remark long enough to reach that cap. On the fixture this was measured on that is
234+
wider than the file's own 671px of rows, so one ordinary remark on a file that fit
235+
its box set it scrolling sideways, 843px of content in 718px of room. Inline-size
236+
containment takes the outlet out of the measure; it still stretches to whatever the
237+
code settles on, and wraps inside it. */
238+
.lf-diff-thread-outlet { padding: 10px clamp(10px, 2vw, 18px); white-space: normal;
239+
contain: inline-size; }
218240
.lf-diff-file-thread-outlet { margin-top: 8px; border: 1px solid var(--rule);
219241
border-radius: var(--r); }
220242
.lf-diff-thread-outlet > .lf-conversation-thread {
@@ -240,7 +262,7 @@ lf-diff:not(.lf-rendered) > pre {
240262
.lf-diff-tools:has(.lf-diff-wrap:checked) ~ .lf-diff-file [data-line] {
241263
white-space: pre-wrap; overflow-wrap: anywhere; }
242264
.lf-diff-tools:has(.lf-diff-wrap:checked) ~ .lf-diff-file code[data-code] {
243-
overflow-x: auto; }
265+
overflow-x: auto; --lf-diff-row-fill: 0; }
244266
/* Pierre owns the line and inline-diff paint. Leaf supplies brighter semantic fills
245267
through its variables above, and keeps its syntax roles readable on the denser one. */
246268
[data-diff-span] [data-lf-syn="cm"] { color: color-mix(in srgb, var(--syn-comment) 50%, var(--ink)); }
@@ -279,6 +301,6 @@ lf-diff:not(.lf-rendered) > pre {
279301
would have been reading the page to learn. The switch is not on the sheet to press,
280302
so the sheet cannot be left holding its answer. */
281303
[data-line] { white-space: pre-wrap !important; overflow-wrap: anywhere !important; }
282-
code[data-code] { overflow-x: visible !important; }
304+
code[data-code] { overflow-x: visible !important; --lf-diff-row-fill: 0; }
283305
}
284306
/* lf-shadow:end */

‎tests/render_cases_widgets.py‎

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -706,25 +706,66 @@ def _filler(name, count):
706706
+ '<lf-diff id="patch" source="review-patch" collapsed><pre></pre></lf-diff>',
707707
)
708708

709-
# Which of a diff's source lines are cut off by the box they sit in. A row is one line of
710-
# the patch however many line boxes it takes, so scrollWidth past clientWidth is text the
711-
# reader cannot see without scrolling the file's own box sideways — and on paper, text
712-
# that is simply gone. `worst` and `widest` are for the failure to say which line and by
713-
# how much, since "some row overflows" sends its reader back to the browser.
709+
# Which of a diff's source lines run past what the reader can see. A row is one line of
710+
# the patch however many line boxes it takes, and neither box in play is the line: the
711+
# row is sized to the longest line in its file so its fill reaches the end of it, and the
712+
# code box is the scrollport. So the room is measured out here — the code box's own width
713+
# less the gutter standing in front of the content column — and the line is measured by a
714+
# Range over the row's contents, which is what `_SELECT_IN_ROW` reads for the same reason.
715+
# Reading the row's own box instead gives every row in a file the file's overhang, which
716+
# is how this last went wrong: 18 of 24 rows counted cut where one line overflowed, and
717+
# `widest` named a trailing-context line the reader could see whole. Text wider than the
718+
# room is text the reader cannot reach without scrolling the file sideways, and on paper,
719+
# text that is simply gone. `worst` and `widest` are for the failure to say which line and
720+
# by how much, since "some row overflows" sends its reader back to the browser.
714721
DIFF_CLIPPING = """() => {
715722
const diff = document.querySelector('lf-diff');
716-
const rows = [...diff.shadowRoot.querySelectorAll('[data-content] [data-line]')];
717-
const cut = rows.filter((row) => row.scrollWidth > row.clientWidth);
723+
const rows = [];
724+
for (const code of diff.shadowRoot.querySelectorAll('code[data-code]')) {
725+
const gutter = code.querySelector('[data-gutter]');
726+
const room = code.clientWidth - (gutter ? gutter.getBoundingClientRect().width : 0);
727+
const range = document.createRange();
728+
for (const row of code.querySelectorAll('[data-content] [data-line]')) {
729+
range.selectNodeContents(row);
730+
rows.push({ row, over: Math.round(range.getBoundingClientRect().width - room) });
731+
}
732+
}
733+
const cut = rows.filter((entry) => entry.over > 0);
718734
return { rows: rows.length, cut: cut.length,
719-
worst: rows.reduce((most, row) =>
720-
Math.max(most, row.scrollWidth - row.clientWidth), 0),
735+
worst: rows.reduce((most, entry) => Math.max(most, entry.over), 0),
721736
widest: cut.length
722-
? cut.reduce((a, b) =>
723-
a.scrollWidth - a.clientWidth > b.scrollWidth - b.clientWidth ? a : b
724-
).textContent.slice(0, 70)
737+
? cut.reduce((a, b) => (a.over > b.over ? a : b))
738+
.row.textContent.slice(0, 70)
725739
: null };
726740
}"""
727741

742+
# Where a changed row's fill ends, against the line it is painting and against the room
743+
# the file's box gives it. A row's green or red is its own background, so it reaches
744+
# exactly as far as the row's box does: a box narrower than the row's own text is a fill
745+
# that stops mid-line and leaves the rest of the addition sitting on the file's plain
746+
# paper, which is what a reader who scrolls sideways finds. `short` counts those and
747+
# `gap` is the worst. `narrow` is the same reading from the other side — a file whose
748+
# lines all fit must still fill its box rather than end at its longest line. `scrolls`
749+
# is the population: on a file that does not scroll, `short` is zero for free.
750+
DIFF_ROW_FILL = """() => {
751+
const diff = document.querySelector('lf-diff');
752+
const out = { rows: 0, short: 0, gap: 0, narrow: 0, scrolls: 0, files: 0 };
753+
for (const code of diff.shadowRoot.querySelectorAll('code[data-code]')) {
754+
out.files += 1;
755+
if (code.scrollWidth > code.clientWidth) out.scrolls += 1;
756+
const gutter = code.querySelector('[data-gutter]');
757+
const room = code.clientWidth - (gutter ? gutter.getBoundingClientRect().width : 0);
758+
for (const row of code.querySelectorAll('[data-content] [data-line]')) {
759+
const painted = row.getBoundingClientRect().width;
760+
out.rows += 1;
761+
const gap = Math.round(row.scrollWidth - painted);
762+
if (gap > 1) { out.short += 1; out.gap = Math.max(out.gap, gap); }
763+
if (room - painted > 1) out.narrow += 1;
764+
}
765+
}
766+
return out;
767+
}"""
768+
728769
# Where each file's row starts against its own wrapper. The review press stands ahead of
729770
# the row and the row is pulled back up over it, so the row starts where it would with no
730771
# press at all — zero — on screen, and on paper, where an unreviewed press is not drawn

‎tests/test_render_widgets.py‎

Lines changed: 111 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
DIFF_CLIPPING,
6464
DIFF_LANDING,
6565
DIFF_PRESS,
66+
DIFF_ROW_FILL,
6667
DIFF_ROW_PLACEMENT,
6768
LONG_LINE_DIFF_PAGE,
6869
MANIFEST_DIFF_PAGE,
@@ -9515,7 +9516,7 @@ def test_a_chart_a_message_carries_waits_for_a_box_rather_than_drawing_into_none
95159516

95169517

95179518
def _bound_diff(browser, serve):
9518-
"""The review the three diff tests below read, with its feed in place before the page
9519+
"""The review the four diff tests below read, with its feed in place before the page
95199520
loads. Bound rather than written inline because that is the form a review arrives in,
95209521
and the only one whose rows are commentable data — `projectData` keys each by file,
95219522
side and source line, which is the coordinate a remark on a line is recorded at."""
@@ -9528,6 +9529,107 @@ def _bound_diff(browser, serve):
95289529
return page
95299530

95309531

9532+
# A phrase late in the diff's longest line: unwrapped it is off the right of the box, and
9533+
# wrapped it is on a line box of its own — the two states the test below is about.
9534+
_DIFF_TAIL = "whichever remote it came from"
9535+
# The line is one row split across syntax spans inside a shadow root, so the range is built
9536+
# over its text nodes rather than dragged: a pointer drag cannot reach words that are off
9537+
# the box in the state this starts in.
9538+
_SELECT_IN_ROW = """(row, phrase) => {
9539+
const walker = document.createTreeWalker(row, NodeFilter.SHOW_TEXT);
9540+
const nodes = [], starts = [];
9541+
let flat = '';
9542+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
9543+
starts.push(flat.length); nodes.push(node); flat += node.data;
9544+
}
9545+
const start = flat.indexOf(phrase);
9546+
if (start < 0) return null;
9547+
const at = (offset) => {
9548+
const index = starts.findLastIndex((value) => value <= offset);
9549+
return [nodes[index], offset - starts[index]];
9550+
};
9551+
const range = document.createRange();
9552+
range.setStart(...at(start));
9553+
range.setEnd(...at(start + phrase.length));
9554+
const selection = getSelection();
9555+
selection.removeAllRanges();
9556+
selection.addRange(range);
9557+
document.dispatchEvent(new MouseEvent('mouseup', { bubbles: true }));
9558+
// The row is a block, so it has one client rectangle however many line boxes are in
9559+
// it, and its height is what says how many. Whether the words selected were on screen
9560+
// is the range's own right edge against the file's scrolling box — not the row's,
9561+
// which is sized to the longest line in the file so its fill reaches the end of it.
9562+
const box = row.closest('code[data-code]').getBoundingClientRect();
9563+
return { text: selection.toString(),
9564+
height: Math.round(row.getBoundingClientRect().height),
9565+
cut: range.getBoundingClientRect().right > box.right };
9566+
}"""
9567+
9568+
9569+
def test_a_diff_row_fills_to_the_end_of_its_line_and_to_the_end_of_a_narrow_box(
9570+
browser, serve
9571+
):
9572+
"""A changed row says it changed by the colour behind it, and that colour is the row's
9573+
own background, so it stops where the row's box stops. The renderer sizes the code
9574+
column to the box that scrolls, which is the width the reader could already see: on
9575+
this patch the fill ran out 2,563px short of the line's end, so scrolling right left
9576+
every addition and deletion sitting on the file's plain paper with nothing to say
9577+
which it was.
9578+
9579+
Every direction in one reading, because they are one track. The floor that carries the
9580+
fill past the scrollport would, left alone, shrink a short file's rows to its own
9581+
longest line and leave the rest of the box blank; and it measures whatever stands in
9582+
that column, so a reader's own remark would size the file too. Then again with the
9583+
rows wrapped, where the scrollbar is gone and the room is all there is."""
9584+
page = _bound_diff(browser, serve)
9585+
9586+
filled = page.evaluate(DIFF_ROW_FILL)
9587+
assert filled["rows"] > 20 and filled["scrolls"] > 0, (
9588+
f"no file runs past its box, so a filled result would prove nothing: {filled}"
9589+
)
9590+
assert filled["short"] == 0, (
9591+
f"a row's fill stops before its own text ends, worst by {filled['gap']}px: "
9592+
f"{filled}"
9593+
)
9594+
assert filled["narrow"] == 0, f"a row's fill stops before its box does: {filled}"
9595+
9596+
# A thread stands in the code column with the lines, and its prose unwrapped is far
9597+
# wider than any of them, so the floor would take it for a line and size the file by
9598+
# the longest remark. `app/routes.py` is what makes that reading sharp: its rows fit
9599+
# their box, so a comment that reached the measure starts a file the reader could see
9600+
# whole scrolling sideways — 671px of rows in a 718px box became 796px in 843px.
9601+
page.locator('lf-diff [data-lf-datum=\'["app/routes.py","new",201]\']').evaluate(
9602+
_SELECT_IN_ROW, "new route"
9603+
)
9604+
expect(page.locator(".lf-fab-bar")).to_be_visible()
9605+
page.locator(".lf-composer textarea").fill(
9606+
"A remark of the ordinary length a reviewer writes, long enough that the line it "
9607+
"would make unwrapped runs well past the longest line in this file, which is the "
9608+
"whole of what the column is supposed to be measuring."
9609+
)
9610+
with sending(page, "the comment on the short file's line"):
9611+
page.keyboard.press("ControlOrMeta+Enter")
9612+
expect(page.locator("lf-diff .lf-diff-thread-outlet")).to_be_visible()
9613+
9614+
remarked = page.evaluate(DIFF_ROW_FILL)
9615+
assert remarked["scrolls"] == filled["scrolls"], (
9616+
f"a remark sized the code, so a file that fit its box now scrolls: {remarked}"
9617+
)
9618+
assert (remarked["short"], remarked["narrow"]) == (0, 0), (
9619+
f"the rows do not fill their box with a thread among them: {remarked}"
9620+
)
9621+
9622+
page.locator("lf-diff .lf-diff-wrap").click()
9623+
wrapped = page.evaluate(DIFF_ROW_FILL)
9624+
assert wrapped["rows"] == filled["rows"] and wrapped["scrolls"] == 0, (
9625+
wrapped,
9626+
filled,
9627+
)
9628+
assert (wrapped["short"], wrapped["narrow"]) == (0, 0), (
9629+
f"wrapped rows do not fill the box they wrapped into: {wrapped}"
9630+
)
9631+
9632+
95319633
def test_a_wrapped_diff_shows_every_line_whole_and_paper_wraps_whatever_the_switch_says(
95329634
browser, serve
95339635
):
@@ -9552,6 +9654,14 @@ def test_a_wrapped_diff_shows_every_line_whole_and_paper_wraps_whatever_the_swit
95529654
assert cut["cut"] > 0 and cut["worst"] > 300, (
95539655
f"no line runs past its box, so a wrapped result would prove nothing: {cut}"
95549656
)
9657+
# Which line, not just how many. The rows are all one width now — each is sized to the
9658+
# longest line in its file so its fill reaches the end of it — so a reading taken off
9659+
# the row's box rather than its text reports the file's overhang for every row alike
9660+
# and still satisfies the count above. Naming the line is what tells the two apart.
9661+
assert cut["widest"].strip().startswith('return "The comparison base'), (
9662+
f"the reading does not name the line that overflows, so it is measuring the rows' "
9663+
f"boxes rather than their text: {cut}"
9664+
)
95559665

95569666
switch.click()
95579667
wrapped = page.evaluate(DIFF_CLIPPING)
@@ -9753,41 +9863,6 @@ def test_a_backward_hunk_step_from_the_diff_itself_opens_one_file_and_lands_in_i
97539863
)
97549864

97559865

9756-
# A phrase late in the diff's longest line: unwrapped it is off the right of the box, and
9757-
# wrapped it is on a line box of its own — the two states the test below is about.
9758-
_DIFF_TAIL = "whichever remote it came from"
9759-
# The line is one row split across syntax spans inside a shadow root, so the range is built
9760-
# over its text nodes rather than dragged: a pointer drag cannot reach words that are off
9761-
# the box in the state this starts in.
9762-
_SELECT_IN_ROW = """(row, phrase) => {
9763-
const walker = document.createTreeWalker(row, NodeFilter.SHOW_TEXT);
9764-
const nodes = [], starts = [];
9765-
let flat = '';
9766-
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
9767-
starts.push(flat.length); nodes.push(node); flat += node.data;
9768-
}
9769-
const start = flat.indexOf(phrase);
9770-
if (start < 0) return null;
9771-
const at = (offset) => {
9772-
const index = starts.findLastIndex((value) => value <= offset);
9773-
return [nodes[index], offset - starts[index]];
9774-
};
9775-
const range = document.createRange();
9776-
range.setStart(...at(start));
9777-
range.setEnd(...at(start + phrase.length));
9778-
const selection = getSelection();
9779-
selection.removeAllRanges();
9780-
selection.addRange(range);
9781-
document.dispatchEvent(new MouseEvent('mouseup', { bubbles: true }));
9782-
// The row is a block, so it has one client rectangle however many line boxes are in
9783-
// it. Its height is what says how many, and its overhang says whether the words
9784-
// selected were on screen at all.
9785-
return { text: selection.toString(),
9786-
height: Math.round(row.getBoundingClientRect().height),
9787-
cut: row.scrollWidth > row.clientWidth };
9788-
}"""
9789-
9790-
97919866
def test_a_comment_on_a_wrapped_diff_line_names_the_line_an_unwrapped_one_names(
97929867
browser, serve
97939868
):

0 commit comments

Comments
 (0)