Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion tests/unit/web-public.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
progressFields,
} from '../../web/public/facts.js';
import { diffGraph } from '../../web/public/diff.js';
import { openCards, parsePatch, unquotePath, wordParts } from '../../web/public/diffparse.js';
import { longestLine, openCards, parsePatch, unquotePath, wordParts } from '../../web/public/diffparse.js';
import { commitOf, githubCommitUrl } from '../../web/public/commits.js';
import { clampDrawerWidth, DRAWER_MIN, GRAPH_MIN, parseStoredWidth } from '../../web/public/resize.js';
import { formatTokens, inputTotal, runsFor, stripText, totalTitle } from '../../web/public/usage.js';
Expand Down Expand Up @@ -1046,3 +1046,17 @@ Deno.test('openCards opens files until a total row budget, and never one over th
assertEquals(many.filter(Boolean).length, 10);
assertEquals(many.slice(0, 10).every(Boolean), true);
});

Deno.test('longestLine counts the widest cell in columns, a tab to the next multiple of four', () => {
const file = parsePatch(
['diff --git a/x b/x', '--- a/x', '+++ b/x', '@@ -1,2 +1,2 @@', ' short', '-\tab', '+a much longer line'].join(
'\n',
),
)
.files[0];
assertEquals(longestLine(file), 'a much longer line'.length);
const tabs =
parsePatch(['diff --git a/x b/x', '--- a/x', '+++ b/x', '@@ -1 +1 @@', '-ab\tc', '+x'].join('\n')).files[0];
assertEquals(longestLine(tabs), 5); // "ab" then a tab to column 4, then "c"
assertEquals(longestLine({ hunks: [] } as never), 0);
});
22 changes: 22 additions & 0 deletions web/public/diffparse.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,25 @@ export function parsePatch(text, truncated = false) {
while (headLines.length && headLines[headLines.length - 1] === '') headLines.pop();
return { head: headLines.join('\n'), files };
}

/** Columns a tab advances to: the next multiple of this. The view sets the same `tab-size`. */
export const TAB_SIZE = 4;

/**
* The widest line of a file in columns, tabs expanded, so the view knows how far a side can scroll.
* @param {DiffFile} f
*/
export function longestLine(f) {
let widest = 0;
for (const hk of f.hunks) {
for (const r of hk.rows) {
for (const c of [r.left, r.right]) {
if (!c) continue;
let col = 0;
for (const ch of c.text) col = ch === '\t' ? col + TAB_SIZE - (col % TAB_SIZE) : col + 1;
if (col > widest) widest = col;
}
}
}
return widest;
}
66 changes: 62 additions & 4 deletions web/public/drawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { writeSection } from './forms.js';
import { createLogView } from './logview.js';
import { kindOf, shortName, verdictOf, workerLabel } from './names.js';
import { commitOf, githubCommitUrl } from './commits.js';
import { openCards, parsePatch } from './diffparse.js';
import { longestLine, openCards, parsePatch } from './diffparse.js';
import { clampDrawerWidth, DRAWER_MIN, KEY_STEP, parseStoredWidth, WIDTH_KEY } from './resize.js';
import { formatTokens, inputTotal, runsFor } from './usage.js';

Expand Down Expand Up @@ -442,7 +442,7 @@ function fileCard(f, startOpen) {
if (body.childElementCount) return;
if (f.status === 'binary') body.append(h('p', 'note', 'Binary file: not shown.'));
else if (f.hunks.length === 0) body.append(h('p', 'note', 'No line changes.'));
else body.append(splitTable(f));
else body.append(splitTable(f), sideScroll(card, body, f));
if (f.incomplete) body.append(h('p', 'drawer-message error', 'This file is cut short.'));
};
if (open) fill();
Expand All @@ -461,6 +461,11 @@ function fileCard(f, startOpen) {
/** @param {import('./diffparse.js').DiffFile} f */
function splitTable(f) {
const table = h('table', 'diff-split');
// Explicit columns: narrow line numbers and code as wide as its longest line. Without them the spanning hunk row
// would set four equal columns.
const cols = h('colgroup');
cols.append(h('col', 'diff-col-n'), h('col', 'diff-col-code'), h('col', 'diff-col-n'), h('col', 'diff-col-code'));
table.append(cols);
const tbody = h('tbody');
for (const hk of f.hunks) {
const sep = h('tr', 'diff-hunk-row');
Expand All @@ -478,6 +483,56 @@ function splitTable(f) {
return table;
}

/** Width of one character in the diff's monospace font, measured once. */
let charPx = 0;

/** @param {HTMLElement} inside a code cell, so the measure uses its font */
function charWidth(inside) {
if (charPx) return charPx;
const probe = h('span', 'diff-text', '0'.repeat(100));
probe.style.visibility = 'hidden';
inside.append(probe);
charPx = probe.getBoundingClientRect().width / 100 || 7;
probe.remove();
return charPx;
}

/**
* One sideways scroll bar per file, stuck to the bottom of the visible diff while the file is on screen. Both sides
* keep half the width; the bar shifts every line of the file together, so old and new stay aligned. A sideways
* swipe, or shift with the wheel, over the file scrolls it too.
* @param {HTMLElement} card
* @param {HTMLElement} body
* @param {import('./diffparse.js').DiffFile} f
*/
function sideScroll(card, body, f) {
const bar = h('div', 'diff-hscroll');
const inner = h('div', 'diff-hscroll-inner');
bar.append(inner);
const widest = longestLine(f);
const measure = () => {
const code = /** @type {HTMLElement | null} */ (card.querySelector('td.diff-code'));
if (!code || !card.isConnected) return;
const pad = parseFloat(getComputedStyle(code).paddingLeft) + parseFloat(getComputedStyle(code).paddingRight);
const overflow = Math.ceil(widest * charWidth(code) + pad - code.clientWidth);
bar.hidden = overflow <= 0;
inner.style.width = `${card.clientWidth + Math.max(0, overflow)}px`;
if (overflow <= 0) bar.scrollLeft = 0;
card.style.setProperty('--diff-x', `${bar.scrollLeft}px`);
};
bar.addEventListener('scroll', () => card.style.setProperty('--diff-x', `${bar.scrollLeft}px`));
body.addEventListener('wheel', (e) => {
const sideways = Math.abs(e.deltaX) > Math.abs(e.deltaY) ? e.deltaX : e.shiftKey ? e.deltaY : 0;
if (!sideways || bar.hidden) return;
bar.scrollLeft += sideways;
e.preventDefault();
}, { passive: false });
const watch = new ResizeObserver(() => (card.isConnected ? measure() : watch.disconnect()));
watch.observe(card);
requestAnimationFrame(measure);
return bar;
}

/**
* A side's line number and text cells; an empty side is a blank pair.
* @param {import('./diffparse.js').Cell | null} c
Expand All @@ -487,9 +542,12 @@ function cells(c, side) {
if (!c) return [h('td', `diff-n diff-empty diff-${side}`), h('td', `diff-code diff-empty diff-${side}`)];
const kind = c.kind === 'context' ? '' : ` diff-${c.kind}`;
const code = h('td', `diff-code diff-${side}${kind}`);
// The text sits in a span that the file's scroll bar shifts sideways; the cell clips it.
const text = h('span', 'diff-text');
if (c.parts) {
code.append(...c.parts.map((p) => h('span', p.changed ? 'diff-word' : '', p.text)));
} else code.textContent = c.text;
text.append(...c.parts.map((p) => h('span', p.changed ? 'diff-word' : '', p.text)));
} else text.textContent = c.text;
code.append(text);
return [h('td', `diff-n diff-${side}${kind}`, String(c.n)), code];
}

Expand Down
37 changes: 33 additions & 4 deletions web/public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -1014,14 +1014,15 @@ button.danger {
}

.diff-card-body {
overflow-x: auto;
overflow: visible;
}

.diff-card-body > .note,
.diff-card-body > .drawer-message {
margin: 6px 8px;
}

/* Two equal halves: a long line is clipped, and the file's scroll bar shifts both sides together. */
.diff-split {
width: 100%;
border-collapse: collapse;
Expand All @@ -1036,16 +1037,41 @@ button.danger {
vertical-align: top;
}

.diff-col-n {
width: 7ch;
}

.diff-n {
width: 3.5em;
white-space: nowrap;
color: var(--muted);
text-align: right;
user-select: none;
}

.diff-code {
white-space: pre-wrap;
overflow-wrap: anywhere;
overflow: hidden;
white-space: pre;
tab-size: 4;
}

.diff-text {
display: inline-block;
transform: translateX(calc(-1 * var(--diff-x, 0px)));
}

/* Sticks to the bottom of the visible diff while its file is on screen. */
.diff-hscroll {
position: sticky;
bottom: 0;
z-index: 1;
overflow-x: auto;
overflow-y: hidden;
border-top: 1px solid var(--line);
background: var(--panel);
}

.diff-hscroll-inner {
height: 1px;
}

.diff-code.diff-left {
Expand Down Expand Up @@ -1078,6 +1104,9 @@ button.danger {
padding: 2px 8px;
background: rgb(59 130 246 / 12%);
color: var(--accent);
white-space: pre;
overflow: hidden;
text-overflow: ellipsis;
}

/* "Full width" on the Diff tab: wider than any dragged width, leaving the graph its minimum. */
Expand Down
Loading