Skip to content

Commit 371c259

Browse files
More reuse via abstract modified file entry (microsoft#242311)
* More reuse via abstract modified file entry re microsoft/vscode-copilot#13692 and microsoft/vscode-copilot#13693 * Updates * add todo * Fixes --------- Co-authored-by: Don Jayamanne <[email protected]>
1 parent e8c27bc commit 371c259

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

src/vs/workbench/contrib/chat/browser/chatEditing/chatEditingModifiedDocumentEntry.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { IFileService } from '../../../../../platform/files/common/files.js';
3333
import { IInstantiationService } from '../../../../../platform/instantiation/common/instantiation.js';
3434
import { observableConfigValue } from '../../../../../platform/observable/common/platformObservableUtils.js';
3535
import { editorSelectionBackground } from '../../../../../platform/theme/common/colorRegistry.js';
36-
import { IUndoRedoService } from '../../../../../platform/undoRedo/common/undoRedo.js';
36+
import { IUndoRedoElement, IUndoRedoService } from '../../../../../platform/undoRedo/common/undoRedo.js';
3737
import { SaveReason, IEditorPane } from '../../../../common/editor.js';
3838
import { IFilesConfigurationService } from '../../../../services/filesConfiguration/common/filesConfigurationService.js';
3939
import { IResolvedTextFileEditorModel, stringToSnapshot } from '../../../../services/textfile/common/textfiles.js';
@@ -107,8 +107,8 @@ export class ChatEditingModifiedDocumentEntry extends AbstractChatEditingModifie
107107
@IFilesConfigurationService fileConfigService: IFilesConfigurationService,
108108
@IChatService chatService: IChatService,
109109
@IEditorWorkerService private readonly _editorWorkerService: IEditorWorkerService,
110-
@IUndoRedoService private readonly _undoRedoService: IUndoRedoService,
111110
@IFileService fileService: IFileService,
111+
@IUndoRedoService undoRedoService: IUndoRedoService,
112112
@IInstantiationService instantiationService: IInstantiationService
113113
) {
114114
super(
@@ -119,6 +119,7 @@ export class ChatEditingModifiedDocumentEntry extends AbstractChatEditingModifie
119119
fileConfigService,
120120
chatService,
121121
fileService,
122+
undoRedoService,
122123
instantiationService
123124
);
124125

@@ -199,12 +200,9 @@ export class ChatEditingModifiedDocumentEntry extends AbstractChatEditingModifie
199200
this._setDocValue(this.initialContent);
200201
}
201202

202-
override async acceptStreamingEditsEnd(tx: ITransaction) {
203+
protected override async _areOriginalAndModifiedIdentical(): Promise<boolean> {
203204
const diff = await this._diffOperation;
204-
super.acceptStreamingEditsEnd(tx);
205-
if (diff?.identical) {
206-
this.accept(tx);
207-
}
205+
return diff ? diff.identical : false;
208206
}
209207

210208
protected override _resetEditsState(tx: ITransaction): void {
@@ -266,13 +264,10 @@ export class ChatEditingModifiedDocumentEntry extends AbstractChatEditingModifie
266264
}
267265
}
268266

269-
override acceptStreamingEditsStart(responseModel: IChatResponseModel, tx: ITransaction) {
270-
super.acceptStreamingEditsStart(responseModel, tx);
271-
272-
// push stack element whenever streaming starts
273-
const request = responseModel.session.getRequests().find(req => req.id === responseModel.requestId);
267+
protected override _createUndoRedoElement(response: IChatResponseModel): IUndoRedoElement {
268+
const request = response.session.getRequests().find(req => req.id === response.requestId);
274269
const label = request?.message.text ? localize('chatEditing1', "Chat Edit: '{0}'", request.message.text) : localize('chatEditing2', "Chat Edit");
275-
this._undoRedoService.pushElement(new SingleModelEditStackElement(label, 'chat.edit', this.modifiedModel, null));
270+
return new SingleModelEditStackElement(label, 'chat.edit', this.modifiedModel, null);
276271
}
277272

278273
async acceptAgentEdits(resource: URI, textEdits: (TextEdit | ICellEditOperation)[], isLastEdits: boolean, responseModel: IChatResponseModel): Promise<void> {

src/vs/workbench/contrib/chat/browser/chatEditing/chatEditingModifiedFileEntry.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { IFileService } from '../../../../../platform/files/common/files.js';
1717
import { IInstantiationService } from '../../../../../platform/instantiation/common/instantiation.js';
1818
import { observableConfigValue } from '../../../../../platform/observable/common/platformObservableUtils.js';
1919
import { editorBackground, registerColor, transparent } from '../../../../../platform/theme/common/colorRegistry.js';
20+
import { IUndoRedoElement, IUndoRedoService } from '../../../../../platform/undoRedo/common/undoRedo.js';
2021
import { IEditorPane } from '../../../../common/editor.js';
2122
import { IFilesConfigurationService } from '../../../../services/filesConfiguration/common/filesConfigurationService.js';
2223
import { ICellEditOperation } from '../../../notebook/common/notebookCommon.js';
@@ -88,6 +89,7 @@ export abstract class AbstractChatEditingModifiedFileEntry extends Disposable im
8889
@IFilesConfigurationService fileConfigService: IFilesConfigurationService,
8990
@IChatService protected readonly _chatService: IChatService,
9091
@IFileService protected readonly _fileService: IFileService,
92+
@IUndoRedoService private readonly _undoRedoService: IUndoRedoService,
9193
@IInstantiationService protected readonly _instantiationService: IInstantiationService,
9294
) {
9395
super();
@@ -223,15 +225,26 @@ export abstract class AbstractChatEditingModifiedFileEntry extends Disposable im
223225
this._resetEditsState(tx);
224226
this._isCurrentlyBeingModifiedByObs.set(responseModel, tx);
225227
this._autoAcceptCtrl.get()?.cancel();
228+
229+
const undoRedoElement = this._createUndoRedoElement(responseModel);
230+
if (undoRedoElement) {
231+
this._undoRedoService.pushElement(undoRedoElement);
232+
}
226233
}
227234

235+
protected abstract _createUndoRedoElement(response: IChatResponseModel): IUndoRedoElement | undefined;
236+
228237
abstract acceptAgentEdits(uri: URI, edits: (TextEdit | ICellEditOperation)[], isLastEdits: boolean, responseModel: IChatResponseModel): Promise<void>;
229238

230239
async acceptStreamingEditsEnd(tx: ITransaction) {
231240
this._resetEditsState(tx);
232241

233-
// AUTO accept mode
234-
if (!this.reviewMode.get() && !this._autoAcceptCtrl.get()) {
242+
if (await this._areOriginalAndModifiedIdentical()) {
243+
// ACCEPT if identical
244+
this.accept(tx);
245+
246+
} else if (!this.reviewMode.get() && !this._autoAcceptCtrl.get()) {
247+
// AUTO accept mode
235248

236249
const acceptTimeout = this._autoAcceptTimeout.get() * 1000;
237250
const future = Date.now() + acceptTimeout;
@@ -259,6 +272,8 @@ export abstract class AbstractChatEditingModifiedFileEntry extends Disposable im
259272
}
260273
}
261274

275+
protected abstract _areOriginalAndModifiedIdentical(): Promise<boolean>;
276+
262277
protected _resetEditsState(tx: ITransaction): void {
263278
this._isCurrentlyBeingModifiedByObs.set(undefined, tx);
264279
this._rewriteRatioObs.set(0, tx);

src/vs/workbench/contrib/chat/browser/chatEditing/chatEditingModifiedNotebookEntry.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { IFileService } from '../../../../../platform/files/common/files.js';
3737
import { IInstantiationService } from '../../../../../platform/instantiation/common/instantiation.js';
3838
import { observableConfigValue } from '../../../../../platform/observable/common/platformObservableUtils.js';
3939
import { editorSelectionBackground } from '../../../../../platform/theme/common/colorRegistry.js';
40-
import { IUndoRedoService } from '../../../../../platform/undoRedo/common/undoRedo.js';
40+
import { IUndoRedoElement, IUndoRedoService } from '../../../../../platform/undoRedo/common/undoRedo.js';
4141
import { IEditorPane, SaveReason } from '../../../../common/editor.js';
4242
import { IFilesConfigurationService } from '../../../../services/filesConfiguration/common/filesConfigurationService.js';
4343
import { SnapshotContext } from '../../../../services/workingCopy/common/fileWorkingCopy.js';
@@ -193,9 +193,10 @@ export class ChatEditingModifiedNotebookEntry extends AbstractChatEditingModifie
193193
@IFileService fileService: IFileService,
194194
@IInstantiationService instantiationService: IInstantiationService,
195195
@ITextModelService private readonly textModelService: ITextModelService,
196-
@IModelService private readonly modelService: IModelService
196+
@IModelService private readonly modelService: IModelService,
197+
@IUndoRedoService undoRedoService: IUndoRedoService
197198
) {
198-
super(modifiedResourceRef.object.notebook.uri, telemetryInfo, kind, configurationService, fileConfigService, chatService, fileService, instantiationService);
199+
super(modifiedResourceRef.object.notebook.uri, telemetryInfo, kind, configurationService, fileConfigService, chatService, fileService, undoRedoService, instantiationService);
199200
this._register(modifiedResourceRef);
200201
this._register(originalResourceRef);
201202
this.modifiedModel = modifiedResourceRef.object.notebook;
@@ -335,6 +336,15 @@ export class ChatEditingModifiedNotebookEntry extends AbstractChatEditingModifie
335336
this.cellEntryMap.forEach(entry => !entry.disposed && entry.clearCurrentEditLineDecoration());
336337
}
337338

339+
protected override _createUndoRedoElement(_response: IChatResponseModel): IUndoRedoElement | undefined {
340+
// TODO@amunger
341+
return undefined;
342+
}
343+
344+
protected override async _areOriginalAndModifiedIdentical(): Promise<boolean> {
345+
return createSnapshot(this.originalModel, this.transientOptions, this.configurationService) === createSnapshot(this.modifiedModel, this.transientOptions, this.configurationService);
346+
}
347+
338348
override async acceptAgentEdits(resource: URI, edits: (TextEdit | ICellEditOperation)[], isLastEdits: boolean, responseModel: IChatResponseModel): Promise<void> {
339349
const isCellUri = resource.scheme === Schemas.vscodeNotebookCell;
340350
const cell = isCellUri && this.modifiedModel.cells.find(cell => isEqual(cell.uri, resource));

0 commit comments

Comments
 (0)