From 49347719be311a62ee335a4f4a5cad8eb2a62cc4 Mon Sep 17 00:00:00 2001 From: oxura Date: Tue, 24 Nov 2026 12:21:21 +0600 Subject: [PATCH] Group lint preview edits by source - Keep file order while avoiding a complete edit scan per preview hunk. Fixes #2292 --- tools/bend-lint/README.md | 1 + tools/bend-lint/src/lint.test.ts | 21 +++++++++++++++++++++ tools/bend-lint/src/lint.ts | 30 +++++++++++++++++++++--------- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/tools/bend-lint/README.md b/tools/bend-lint/README.md index bf4d13107e..f1d9f31613 100644 --- a/tools/bend-lint/README.md +++ b/tools/bend-lint/README.md @@ -47,6 +47,7 @@ also edits another file is skipped. When two fixes overlap, the later one is skipped. Run it again to get it. All edit offsets refer to the original text. Accepted edits are assembled in one pass rather than copying the whole file for each edit. +In a multi-file preview, files appear in the order their first edit was reported. Exit code is 0 if there were no errors (warnings don't count), 1 for a checker or rule error, and 2 for bad arguments or a crash. diff --git a/tools/bend-lint/src/lint.test.ts b/tools/bend-lint/src/lint.test.ts index 525de5e1e3..99c4b98908 100644 --- a/tools/bend-lint/src/lint.test.ts +++ b/tools/bend-lint/src/lint.test.ts @@ -1671,6 +1671,27 @@ describe("review fixes", () => { }])).toEqual({ text: "a😀c!", skipped: 0, elsewhere: 0 }); }); + test("a multi-file patch preview keeps file order and groups interleaved edits", () => { + const first = { path: "b.bend", text: "# abcd\n", root: true, base: false }; + const second = { path: "a.bend", text: "# wxyz\n", root: false, base: false }; + const edit = (file: Source, at: number, text: string): Edit => + ({ span: { file, beg: at, end: at + 1 }, text }); + const preview = render({ + code: "test/preview", + message: "update comments", + severity: "warning", + fixes: [{ + title: "update comments", + applicability: "safe", + edits: [edit(first, 2, "A"), edit(second, 3, "X"), edit(first, 4, "C"), edit(second, 5, "Z")], + }], + }); + expect(preview.slice(preview.indexOf("\n---"))).toBe( + "\n--- b.bend\n+++ b.bend\n@@ -1,1 +1,1 @@\n-# abcd\n+# AbCd" + + "\n--- a.bend\n+++ a.bend\n@@ -1,1 +1,1 @@\n-# wxyz\n+# wXyZ", + ); + }); + test("a fix that also edits another file is skipped whole", () => { const file: Source = { path: "", text: "abc", root: true, base: false }; const other: Source = { path: "", text: "xyz", root: false, base: false }; diff --git a/tools/bend-lint/src/lint.ts b/tools/bend-lint/src/lint.ts index a4c0e456e1..a29072e578 100644 --- a/tools/bend-lint/src/lint.ts +++ b/tools/bend-lint/src/lint.ts @@ -513,20 +513,31 @@ export function applyFixes( // bend's own error layout; the head names the severity and the code, and // each fix follows as a unified diff of the lines it touches. export function render(d: Diag): string { - const fixes = d.fixes.map( - (fix) => + const fixes = d.fixes.map((fix) => { + const files = new Map(); + for (const edit of fix.edits) { + const { file, beg, end } = edit.span; + const group = files.get(file); + if (group === undefined) { + files.set(file, { edits: [edit], beg, end }); + } else { + group.edits.push(edit); + group.beg = Math.min(group.beg, beg); + group.end = Math.max(group.end, end); + } + } + return ( "\n\nFix: " + fix.title + " [" + fix.applicability + "]" + - [...new Set(fix.edits.map((e) => e.span.file))] - .map((file) => { - const mine = fix.edits.filter((e) => e.span.file === file); + [...files] + .map(([file, { edits: mine, beg, end }]) => { const ls = starts(file, file.text); - const first = line(ls, Math.min(...mine.map((e) => e.span.beg))); + const first = line(ls, beg); const from = ls[first]; - const to = ls[line(ls, Math.max(...mine.map((e) => e.span.end))) + 1] ?? file.text.length; + const to = ls[line(ls, end) + 1] ?? file.text.length; const old = file.text.slice(from, to); const gone = old.match(LINE) ?? []; const came = @@ -559,8 +570,9 @@ export function render(d: Diag): string { [...show(gone, "-"), ...show(came, "+")].join("\n") ); }) - .join(""), - ); + .join("") + ); + }); return layout(loaded, d, HEAD[d.severity] + " [" + d.code + "]:") + fixes.join(""); }