Skip to content
Merged
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
66 changes: 59 additions & 7 deletions extensions/vscode/src/diff/vertical/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,7 @@ export class VerticalDiffHandler implements vscode.Disposable {
// Accept all: delete all the red ranges and clear green decorations
await this.deleteRangeLines(removedRanges.map((r) => r.range));
} else {
// Reject all: Re-insert red lines, delete green ones
for (const r of removedRanges) {
await this.deleteRangeLines([r.range]);
await this.insertTextAboveLine(r.range.start.line, r.line);
}
await this.deleteRangeLines(this.addedLineDecorations.ranges);
await this.unifiedRejectAll();
}

this.clearDecorations();
Expand Down Expand Up @@ -604,6 +599,63 @@ export class VerticalDiffHandler implements vscode.Disposable {
}

/**
* Gets the first line number that was changed in a diff
* Rejects all diffs in a single edit operation.
*/
private async unifiedRejectAll() {
await this.ensureCurrentFileIsFocused();

const removedRanges = this.removedLineDecorations.ranges;
const addedRanges = this.addedLineDecorations.ranges;

interface LineOperation {
type: "removed" | "added";
line?: string; // Only for removed lines
range: vscode.Range;
}
Comment on lines +610 to +614
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't even know you could do define an interface inside a function lol

Kind of awkward but nbd


const operations: LineOperation[] = [];
for (const r of removedRanges) {
operations.push({
type: "removed",
line: r.line,
range: r.range,
});
}
for (const range of addedRanges) {
operations.push({
type: "added",
range,
});
}

operations.sort((a, b) => b.range.start.line - a.range.start.line);

const document = this.editor.document;
const lines = document.getText().split("\n");

for (const op of operations) {
const lineNum = op.range.start.line;

if (op.type === "removed") {
// Replace the placeholder line with the original content
lines[lineNum] = op.line!;
} else if (op.type === "added") {
// Delete the added lines
const startLine = op.range.start.line;
const endLine = op.range.end.line;
const numLinesToDelete = endLine - startLine + 1;
lines.splice(startLine, numLinesToDelete);
}
}

const finalContent = lines.join("\n");

await this.editor.edit(
(editBuilder) => {
const fullRange = new vscode.Range(0, 0, document.lineCount, 0);
editBuilder.replace(fullRange, finalContent);
},
{ undoStopAfter: false, undoStopBefore: false },
);
}
}
Loading