Skip to content

Commit

Permalink
chat: make undo/redo work with manually restored snapshots/removed re…
Browse files Browse the repository at this point in the history
  • Loading branch information
connor4312 authored Feb 24, 2025
1 parent a0055ca commit eb6f14e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -572,11 +572,6 @@ registerAction2(class RemoveAction extends Action2 {
// Restore the snapshot to what it was before the request(s) that we deleted
const snapshotRequestId = chatRequests[itemIndex].id;
await session.restoreSnapshot(snapshotRequestId, undefined);

// Remove the request and all that come after it
for (const request of requestsToRemove) {
await chatService.removeRequest(item.sessionId, request.id);
}
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,13 @@ export class ChatEditingSession extends Disposable implements IChatEditingSessio
return this._linearHistory.get().find(s => s.requestId === requestId);
}

private _findEditStop(requestId: string, undoStop: string | undefined): IChatEditingSessionStop | undefined {
return this._findSnapshot(requestId)?.stops.find(s => s.stopId === undoStop);
private _findEditStop(requestId: string, undoStop: string | undefined) {
const snapshot = this._findSnapshot(requestId);
if (!snapshot) {
return undefined;
}
const idx = snapshot.stops.findIndex(s => s.stopId === undoStop);
return idx === -1 ? undefined : { stop: snapshot.stops[idx], snapshot, historyIndex: snapshot.startIndex + idx };
}

private _ensurePendingSnapshot() {
Expand Down Expand Up @@ -467,7 +472,7 @@ export class ChatEditingSession extends Disposable implements IChatEditingSessio
public async getSnapshotModel(requestId: string, undoStop: string | undefined, snapshotUri: URI): Promise<ITextModel | null> {
const entries = undoStop === POST_EDIT_STOP_ID
? this._findSnapshot(requestId)?.postEdit
: this._findEditStop(requestId, undoStop)?.entries;
: this._findEditStop(requestId, undoStop)?.stop.entries;
if (!entries) {
return null;
}
Expand All @@ -491,10 +496,14 @@ export class ChatEditingSession extends Disposable implements IChatEditingSessio
private _pendingSnapshot: IChatEditingSessionStop | undefined;
public async restoreSnapshot(requestId: string | undefined, stopId: string | undefined): Promise<void> {
if (requestId !== undefined) {
const snapshot = this._findEditStop(requestId, stopId);
if (snapshot) {
const stopRef = this._findEditStop(requestId, stopId);
if (stopRef) {
this._ensurePendingSnapshot();
await this._restoreSnapshot(snapshot, undefined);
await asyncTransaction(async tx => {
this._linearHistoryIndex.set(stopRef.historyIndex, tx);
await this._restoreSnapshot(stopRef.stop, tx);
});
this._updateRequestHiddenState();
}
} else {
const pendingSnapshot = this._pendingSnapshot;
Expand Down

0 comments on commit eb6f14e

Please sign in to comment.