5 files, 219 lines added, 50 removed.
--- a/client/src/history-answers.ts
+++ b/client/src/history-answers.ts
@@ -166,7 +166,10 @@
166166 readonly message: string; 167167 readonly time: number; 168168 readonly text: string; 169+ /** It only changed formatting, or only moved the line. */ 169170 readonly formatting: boolean; 171+ /** The file it moved the line from, when another. */ 172+ readonly movedFrom?: string; 170173 readonly publish: Publish | null; 172175export interface WhyAnswer { --- a/client/src/history.ts
+++ b/client/src/history.ts
@@ -9,7 +9,7 @@
99import { findRoot, readConfig } from "./config.ts"; 1010import { relativeTo } from "./folder.ts"; 1111import { type GitOrigin, gitOrigin, issueLinks } from "./git-origin.ts"; 12−import { byWhom, type TrailStep } from "./history-answers.ts";
12+import { byWhom, type TrailStep, type WhyAnswer } from "./history-answers.ts"; 1414function open(env: Env): { root: string; api: Api; agent: string } { 1515 const root = findRoot(env.cwd); @@ -61,9 +61,20 @@
6161 return [` From git: commit ${o.commit}${o.author ? ` by ${o.author}` : ""}${o.time ? `, ${o.time}` : ""}.`]; 64+/** "1 formatting change and 2 moves": what why skipped. */ 65+function skippedKinds(steps: readonly TrailStep[]): string { 66+ const moves = steps.filter((s) => s.movedFrom !== undefined).length; 67+ const formatting = steps.length - moves; 69+ ...(formatting > 0 ? [`${formatting} formatting change${formatting === 1 ? "" : "s"}`] : []), 70+ ...(moves > 0 ? [`${moves} move${moves === 1 ? "" : "s"}`] : []), 6474/** A trail step in a few words: its version (or that it isn't pushed), who, and the first line of why. */ 6575function stepName(s: TrailStep, long = false): string { 66− const subject = (s.publish ? (gitOrigin(s.publish)?.message ?? s.publish.message) : s.message).split("\n")[0]!;
76+ const first = (s.publish ? (gitOrigin(s.publish)?.message ?? s.publish.message) : s.message).split("\n")[0]!; 77+ const subject = first.length > 100 ? `${first.slice(0, 97).trimEnd()}...` : first; 6778 const where = s.publish === null ? "not pushed" : `version ${s.publish.version}`; 6879 const who = s.publish ? byWhom(s.publish) : s.author; @@ -147,8 +158,15 @@
147158 const w = await api.why(file, lineNumber(raw, "the line")!, view, at); 148159 if (at !== undefined && w.version !== at) 149160 throw new UsageError(`this server doesn't read why at a version yet; abso blame ${w.file} --version ${at} does`); 150− // The line's changes (abso-core issue 20): the newest one that changed what it does leads, past formatting-only ones.
151− const all = bool(args, "all");
161+ return { text: whyText(w, { at, all: bool(args, "all") }), json: { ...w } }; 165+ * What abso why prints for an answer. The line's changes (abso-core issue 166+ * 20): the newest one that changed what it does leads, past formatting-only 167+ * ones, unless `all`. 169+export function whyText(w: WhyAnswer, { at, all = false }: { at?: number; all?: boolean } = {}): string { 152170 const trail = w.trail ?? []; 153171 const lead = (all ? trail[0] : trail.find((s) => !s.formatting)) ?? trail[0]; 154172 const leadAt = lead === undefined ? 0 : trail.indexOf(lead); @@ -161,13 +179,13 @@
161179 ? [`Written by ${w.change.author} (${when(w.change.time)}), pushed in ${pushed(w.publish, w.gitIssues)}.`] 163181 `Written by ${lead.author} (${when(lead.time)}), pushed in ${pushed(lead.publish, w.gitIssues)}.`, 164− ` It read: ${lead.text.trim()}`,
182+ ...(lead.text.trim() !== w.text.trim() ? [` It read: ${lead.text.trim()}`] : []), 166184 ...(origin ? gitLines(origin) : []), 167185 ...issueLinks(`${w.text}\n${origin?.message ?? ""}`, origin?.issues).map((l) => ` ${l}`), 168186 ...(skipped.length > 0 170− `Skipped ${skipped.length} formatting change${skipped.length === 1 ? "" : "s"} since: ${skipped.map((s) => stepName(s)).join("; ")}. abso why --all shows ${skipped.length === 1 ? "it" : "them"}.`,
188+ `Skipped ${skippedKinds(skipped)} since: ${skipped.map((s) => `${stepName(s)}${s.movedFrom ? `, moved from ${s.movedFrom}` : ""}`).join("; ")}. abso why --all shows ${skipped.length === 1 ? "it" : "them"}.`, 173191 ...(earlier.length > 0 @@ -177,8 +195,8 @@
177195 const newer = i === 0 ? lead! : earlier[i - 1]!; 178196 const o = s.publish ? gitOrigin(s.publish, w.gitIssues) : undefined; 180− ` ${stepName(s, true)}${s.formatting ? " (formatting only)" : ""}${o ? `, from git commit ${o.commit.slice(0, 12)}${o.author ? ` by ${o.author}` : ""}` : ""}`,
181− ...(s.text !== newer.text ? [` It read: ${s.text.trim()}`] : []),
198+ ` ${stepName(s, true)}${s.movedFrom ? ` (moved from ${s.movedFrom})` : s.formatting ? " (formatting only)" : ""}${o ? `, from git commit ${o.commit.slice(0, 12)}${o.author ? ` by ${o.author}` : ""}` : ""}`, 199+ ...(s.text.trim() !== newer.text.trim() ? [` It read: ${s.text.trim()}`] : []), @@ -209,7 +227,7 @@
212− return { text: lines.join("\n"), json: { ...w } };
230+ return lines.join("\n"); 215233function ranges(rs: readonly [number, number][]): string { --- a/worker/project/blame.ts
+++ b/worker/project/blame.ts
@@ -9,7 +9,7 @@
99import { pathKey } from "./pathkey.ts"; 1010import type { Project } from "./project.ts"; 1111import { projectFileIn } from "./project-helpers.ts"; 12−import { trail } from "./trail.ts";
12+import { type TrailStep, trail } from "./trail.ts"; 1414export interface BlameLine { @@ -257,43 +257,66 @@
259259 * The line's changes, newest first (abso-core issue 20), its own change 260− * first: each change that rewrote it, with the line's text after it, and
261− * whether it only changed formatting. Several changes in one push count as
260+ * first: each change that rewrote it, with the line's text after it, 261+ * whether it only changed formatting, and the file it moved from, if it did. Several changes in one push count as 262262 * one, formatting only if all of them were. 264264function lineTrail(project: Project, view: ReadView, file: string, line: LineId) { 265265 const graph = project.graph; 266− const { steps, more } = trail(graph, view, file, line);
267− const out: {
268− atom: AtomId;
269− author: string;
270− message: string;
271− time: number;
272− text: string;
273− formatting: boolean;
274− publish: Publish | null;
275− }[] = [];
266+ // Changes pushed together: the same version of the same space, or both still in one agent's draft. 267+ const samePush = (a: Atom, b: Atom) => samePublish(publishOf(project, a.id), publishOf(project, b.id)); 268+ const { steps, more } = trail(graph, view, file, line, undefined, samePush); 269+ const groups: { steps: TrailStep[]; publish: Publish | null }[] = []; 276270 for (const s of steps) { 277− const a = graph.atom(s.atom);
278− const publish = publishOf(project, a.id);
279− const prev = out[out.length - 1];
280− if (prev && samePublish(prev.publish, publish) && (publish !== null || prev.author === a.author)) {
281− prev.formatting &&= s.formatting;
282− continue;
283− }
284− out.push({
271+ const publish = publishOf(project, s.atom); 272+ const prev = groups[groups.length - 1]; 275+ samePublish(prev.publish, publish) && 276+ (publish !== null || graph.atom(prev.steps[0]!.atom).author === graph.atom(s.atom).author) 279+ else groups.push({ steps: [s], publish }); 281+ const out = groups.map(({ steps: [newest, ...rest], publish }) => { 282+ const a = graph.atom(newest!.atom); 283+ const oldest = rest[rest.length - 1] ?? newest!; 284+ const moved = [newest!, ...rest].some((s) => s.maybeMoved) 285+ ? movedFrom(project, newest!.line, oldest.replaced, publish) 287290 message: a.message, 289− text: s.text,
290− formatting: s.formatting,
293+ formatting: [newest!, ...rest].every((s) => s.formatting), 294+ ...(moved !== undefined ? { movedFrom: moved } : {}), 292− });
293− }
294298 return { trail: out, ...(more ? { trailMore: true as const } : {}) }; 302+ * The file a push moved a line from: where the line it replaced was just 303+ * before the push, when that's not where the push left the line. 305+function movedFrom(project: Project, line: LineId, replaced: LineId | undefined, publish: Publish | null) { 306+ if (replaced === undefined || publish === null || publish.version < 1) return undefined; 307+ const where = publish.draft === null ? {} : { draft: publish.draft }; 308+ const fileIn = (version: number, id: LineId) => { 310+ return project.viewAt(version, where).fileOf(id); 315+ const before = fileIn(publish.version - 1, replaced); 316+ const after = fileIn(publish.version, line); 317+ return before !== undefined && after !== undefined && before !== after ? before : undefined; 297320/** Where "#N" in history imported from git points (abso.json's "gitIssues"), when shared's project file says. */ 298321function gitIssuesOf(view: ReadView): string | undefined { 299322 const file = projectFileIn(view); --- a/worker/project/test/trail.test.ts
+++ b/worker/project/test/trail.test.ts
@@ -96,3 +96,41 @@
9696 assert.equal(t.more, true); 9797 assert.equal(trail(p.graph, view, "a.ts", line).steps.length, 5); 100+test("code moved to another file in one push is followed back into the file it came from", () => { 102+ "big.ts": "export const keep = 1;\nexport function tide(level: number) {\n\treturn level * 2;\n}\n", 103+ "small.ts": "export const other = 1;\n", 105+ push(p, "ann", "big.ts", "level * 2", "level * 3", "Tides run higher", 10); // v1 106+ // Moved as it is: the engine keeps the lines themselves, so their history comes along. 108+ const tide = "export function tide(level: number) {\n\treturn level * 3;\n}\n"; 109+ p.edit("bob", { file: "big.ts", old: tide, new: "" }, 20); 110+ p.edit("bob", { file: "small.ts", old: "export const other = 1;\n", new: `export const other = 1;\n${tide}` }, 21); 111+ p.publish("bob", "Move tide to small.ts", 22); // v2 113+ why(p, "small.ts", 3).trail.map((s) => [s.publish?.version, s.text]), 115+ [1, "\treturn level * 3;"], 116+ [0, "\treturn level * 2;"], 119+ // Moved and reindented in one push: new lines, followed back by their code. 121+ const moved = tide.replaceAll("\t", " "); 122+ p.edit("ann", { file: "small.ts", old: tide, new: "" }, 30); 123+ p.edit("ann", { file: "big.ts", old: "export const keep = 1;\n", new: `export const keep = 1;\n${moved}` }, 31); 124+ p.publish("ann", "Move tide back, two-space indent", 32); // v3 125+ const w = why(p, "big.ts", 3); 127+ w.trail.map((s) => [s.publish?.version, s.formatting, s.movedFrom, s.text]), 129+ [3, true, "small.ts", " return level * 3;"], 130+ [1, false, undefined, "\treturn level * 3;"], 131+ [0, false, undefined, "\treturn level * 2;"], 134+ // A short line (a brace) is too common to follow. 135+ assert.equal(why(p, "big.ts", 4).trail.length, 1); --- a/worker/project/trail.ts
+++ b/worker/project/trail.ts
@@ -8,7 +8,9 @@
88// change that only touched formatting (whitespace, line wrapping, quotes, 99// trailing commas and semicolons, parentheses around one arrow parameter) 1010// is marked, so why can skip past it to the change that said what the line 11−// does.
11+// does. So is a move: code a push took out of one place and wrote, the same, 12+// in another (a file split in two), which the trail follows into the file it 1315import type { Atom, AtomId, Graph, LineId, ReadView, SeqLine } from "../../src/engine/index.ts"; @@ -23,6 +25,12 @@
2325 readonly formatting: boolean; 2426 /** The line it replaced, when it replaced one. */ 2527 readonly replaced?: LineId; 29+ * The change may have moved the line: the line it replaced was found 30+ * elsewhere, or was written in another file. Which file it was in then 31+ * takes the versions before and after (blame.ts). 33+ readonly maybeMoved?: true; 2836/** At most this many steps back. */ @@ -174,7 +182,10 @@
175183 * The line's changes, newest first: the change that wrote it, then the one 176184 * that wrote the line it replaced, and so on back to a change that wrote it 177− * new. At most `max` steps; `more` says the trail went on.
185+ * new. A line written new where the same push deleted the same code 186+ * elsewhere (code moved to another file, or within one) follows that line 187+ * on; `samePush` says which changes were pushed together. At most `max` 188+ * steps; `more` says the trail went on. 179190export function trail( @@ -182,36 +193,112 @@
196+ samePush: (a: Atom, b: Atom) => boolean = (a, b) => a.id === b.id, 185197): { steps: TrailStep[]; more: boolean } { 186− const seq = view.sequence(file);
187− const index = new Map<LineId, number>();
188− for (const [i, s] of seq.entries()) index.set(s.id, i);
198+ const files = new Map<string, { seq: readonly SeqLine[]; index: Map<LineId, number> }>(); 199+ const sequenceOf = (path: string) => { 200+ let f = files.get(path); 202+ const seq = view.sequence(path); 203+ f = { seq, index: new Map(seq.map((s, i) => [s.id, i])) }; 189208 const steps: TrailStep[] = []; 190209 let current: LineId | undefined = line; 191211 while (current !== undefined && steps.length < max) { 192− const at = index.get(current);
193− if (at === undefined) break;
212+ // The file to read its hunk in: where the view has it, or where it was written, whichever pairs it. 213+ const id: LineId = current; 214+ let found: { seq: readonly SeqLine[]; at: number; hunk: { before: number[]; after: number[] } } | undefined; 215+ let pair: ReturnType<typeof pairIn>; 216+ for (const path of new Set([hint, fileIn(view, id), graph.lineFile(id)])) { 217+ if (path === undefined) continue; 218+ const { seq, index } = sequenceOf(path); 219+ const at = index.get(id); 220+ if (at === undefined) continue; 221+ const hunk = hunkOf(graph, seq, at, graph.atom(seq[at]!.atom)); 223+ hunk.before.map((i) => seq[i]!.text), 224+ hunk.after.map((i) => seq[i]!.text), 225+ hunk.after.indexOf(at), 227+ if (!found || p) [found, pair] = [{ seq, at, hunk }, p]; 231+ const { seq, at, hunk } = found; 194232 const atom = graph.atom(seq[at]!.atom); 195− const hunk = hunkOf(graph, seq, at, atom);
196− const pair = pairIn(
197− hunk.before.map((i) => seq[i]!.text),
198− hunk.after.map((i) => seq[i]!.text),
199− hunk.after.indexOf(at),
200− );
201− const replaced = pair ? seq[hunk.before[pair.index]!]!.id : undefined;
233+ let replaced = pair ? seq[hunk.before[pair.index]!]!.id : undefined; 234+ let formatting = pair?.formatting ?? false; 235+ let searched = false; 236+ if (replaced === undefined) { 237+ const moved = movedFrom(graph, view, atom, seq[at]!.text, new Set(hunk.before.map((i) => seq[i]!.id)), samePush); 238+ if (moved) [replaced, formatting, searched] = [moved, true, true]; 240+ const maybeMoved = searched || (replaced !== undefined && graph.lineFile(replaced) !== graph.lineFile(id)); 204− line: current,
205244 text: seq[at]!.text, 206− formatting: pair?.formatting ?? false,
207246 ...(replaced !== undefined ? { replaced } : {}), 247+ ...(maybeMoved ? { maybeMoved: true as const } : {}), 249+ hint = replaced === undefined ? hint : (fileIn(view, replaced) ?? hint); 209250 current = replaced; 211252 return { steps, more: current !== undefined }; 255+/** The file a line is in, in a view, or undefined when the view can't place it. */ 256+function fileIn(view: ReadView, line: LineId): string | undefined { 258+ return view.fileOf(line); 264+/** How far either side of a change to look for the others pushed with it. */ 268+ * Where a line written new came from, when it was moved: a line with the same 269+ * code (exactly, else formatted differently) that a change pushed with this 270+ * one deleted, if exactly one such line has the best match. Short lines 271+ * (a brace, a blank) match too much to follow. 278+ skip: ReadonlySet<LineId>, 279+ samePush: (a: Atom, b: Atom) => boolean, 280+): LineId | undefined { 281+ const want = normalize([text]); 282+ if (want.length < 12) return undefined; 283+ const all = graph.atoms(); 284+ const exact: LineId[] = []; 285+ const same: LineId[] = []; 286+ for (let s = Math.max(0, atom.seq - NEAR); s <= Math.min(all.length - 1, atom.seq + NEAR); s++) { 287+ const other = all[s]!; 288+ if (other.author !== atom.author || !view.set.has(other.id) || !samePush(other, atom)) continue; 289+ for (const d of other.deletes) { 290+ if (skip.has(d)) continue; 291+ const t = graph.lineText(d); 292+ if (t === undefined) continue; 293+ if (t === text) exact.push(d); 294+ else if (normalize([t]) === want) same.push(d); 297+ const found = exact.length > 0 ? exact : same; 298+ return found.length === 1 ? found[0] : undefined; 215302 * The hunk a change made around one of its lines, as indexes into the 216303 * sequence in file order: the lines it deleted there and the lines it wrote. 217304 * Lines nobody could see when it was made (written later, or already