дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 01:31:41 +02:00
Opening a working-tree diff from the commit panel computes a key for every hunk (hunkKeys). Each key called currentHunkText, which rebuilt the line-start table of the whole file, so a file with many changes cost hunks x file size: a 9.4 MB JSON with 17,931 changes kept the web view busy for minutes and froze the window. rollbackEdit and currentHunkText now take an optional precomputed lineStarts table. hunkKeys builds it once. The other per-hunk loops pass theirs too: line-level partial commits (commitSelection), the partial commit projection, the commit-check rollback fallback, and stage/unstage from the gutter.
61 рядки
2.3 KiB
TypeScript
61 рядки
2.3 KiB
TypeScript
import { ChangeSet, Text } from "@codemirror/state";
|
|
import type { HunkRange, LineHunk, PartialCommitFile } from "@/bindings";
|
|
import { currentHunkText, lineStarts, rollbackEdit } from "./hunks";
|
|
|
|
const key = (h: HunkRange) => `${h.oldStart}:${h.oldLines}:${h.start}:${h.lines}`;
|
|
|
|
/** Inverts the hunk selection so edits can be mapped back to their original working lines. */
|
|
export function partialCommitProjection(
|
|
working: Text,
|
|
selected: Text,
|
|
hunks: readonly LineHunk[],
|
|
file: PartialCommitFile,
|
|
): ChangeSet | null {
|
|
const text = working.toString();
|
|
const starts = lineStarts(text);
|
|
const whole = new Set(file.hunks.map(key));
|
|
const lines = new Map((file.lineHunks ?? []).map((h) => [key(h.range), h]));
|
|
const edits = [];
|
|
for (const hunk of hunks) {
|
|
const id = key(hunk);
|
|
if (whole.delete(id)) continue;
|
|
const partial = lines.get(id);
|
|
if (!partial) {
|
|
edits.push(rollbackEdit(text, hunk, starts));
|
|
continue;
|
|
}
|
|
lines.delete(id);
|
|
if (partial.oldText !== hunk.oldText || partial.newText !== currentHunkText(text, hunk, starts)) return null;
|
|
const additions = new Set(partial.additions);
|
|
const deletions = new Set(partial.deletions);
|
|
const oldLines = hunk.oldText.match(/[^\n]*\n|[^\n]+$/g) ?? [];
|
|
let kept = oldLines.filter((_, line) => !deletions.has(line)).join("");
|
|
if (kept && !kept.endsWith("\n") && additions.size) kept += "\n";
|
|
if (kept) edits.push({ from: starts[hunk.start]!, insert: kept });
|
|
for (let line = 0; line < hunk.lines; line++) {
|
|
if (additions.has(line)) continue;
|
|
const start = hunk.start + line;
|
|
edits.push({ from: starts[start]!, to: starts[start + 1]! });
|
|
}
|
|
}
|
|
if (whole.size || lines.size) return null;
|
|
const reverted = ChangeSet.of(edits, working.length);
|
|
if (!reverted.apply(working).eq(selected)) return null;
|
|
return reverted.invert(working);
|
|
}
|
|
|
|
/** A fix can change shared text but must leave every excluded edit in place. */
|
|
export function keepsExcludedChanges(changes: ChangeSet, excluded: ChangeSet): boolean {
|
|
let overlaps = false;
|
|
excluded.iterChangedRanges((from, to) => {
|
|
changes.iterChangedRanges((start, end) => {
|
|
if (from === to || start === end) {
|
|
overlaps ||= from <= end && start <= to;
|
|
} else {
|
|
overlaps ||= from < end && start < to;
|
|
}
|
|
});
|
|
});
|
|
return !overlaps;
|
|
}
|