Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix https://github.com/microsoft/vscode-copilot-release/issues/211, better method names #184392

Merged
merged 1 commit into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class MakeRequestAction extends AbstractInteractiveEditorAction {
}

runInteractiveEditorCommand(_accessor: ServicesAccessor, ctrl: InteractiveEditorController, _editor: ICodeEditor, ..._args: any[]): void {
ctrl.accept();
ctrl.acceptInput();
}
}

Expand Down Expand Up @@ -313,7 +313,7 @@ export class DiscardAction extends AbstractInteractiveEditorAction {
}

async runInteractiveEditorCommand(_accessor: ServicesAccessor, ctrl: InteractiveEditorController, _editor: ICodeEditor, ..._args: any[]): Promise<void> {
await ctrl.cancelSession();
ctrl.cancelSession();
}
}

Expand All @@ -339,7 +339,7 @@ export class DiscardToClipboardAction extends AbstractInteractiveEditorAction {

override async runInteractiveEditorCommand(accessor: ServicesAccessor, ctrl: InteractiveEditorController): Promise<void> {
const clipboardService = accessor.get(IClipboardService);
const changedText = await ctrl.cancelSession();
const changedText = ctrl.cancelSession();
if (changedText !== undefined) {
clipboardService.writeText(changedText);
}
Expand All @@ -363,7 +363,7 @@ export class DiscardUndoToNewFileAction extends AbstractInteractiveEditorAction

override async runInteractiveEditorCommand(accessor: ServicesAccessor, ctrl: InteractiveEditorController, editor: ICodeEditor, ..._args: any[]): Promise<void> {
const editorService = accessor.get(IEditorService);
const changedText = await ctrl.cancelSession();
const changedText = ctrl.cancelSession();
if (changedText !== undefined) {
const input: IUntitledTextResourceEditorInput = { forceUntitled: true, resource: undefined, contents: changedText, languageId: editor.getModel()?.getLanguageId() };
editorService.openEditor(input, SIDE_GROUP);
Expand Down Expand Up @@ -460,7 +460,7 @@ export class ApplyPreviewEdits extends AbstractInteractiveEditorAction {
}

override async runInteractiveEditorCommand(_accessor: ServicesAccessor, ctrl: InteractiveEditorController): Promise<void> {
await ctrl.applyChanges();
ctrl.acceptSession();
}
}

Expand All @@ -486,7 +486,7 @@ export class CancelSessionAction extends AbstractInteractiveEditorAction {
}

async runInteractiveEditorCommand(_accessor: ServicesAccessor, ctrl: InteractiveEditorController, _editor: ICodeEditor, ..._args: any[]): Promise<void> {
await ctrl.cancelSession();
ctrl.cancelSession();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ export class InteractiveEditorController implements IEditorContribution {
if (this._activeSession) {
if (this._activeSession.editMode === EditMode.Preview) {
this._log('finishing existing session, using CANCEL', this._activeSession.editMode);
await this.cancelSession();
this.cancelSession();
} else {
this._log('finishing existing session, using APPLY', this._activeSession.editMode);
await this.applyChanges();
this.acceptSession();
}
}
}
Expand Down Expand Up @@ -425,7 +425,7 @@ export class InteractiveEditorController implements IEditorContribution {
return State.MAKE_REQUEST;
}

private async [State.MAKE_REQUEST](): Promise<State.APPLY_RESPONSE | State.PAUSE | State.CANCEL> {
private async [State.MAKE_REQUEST](): Promise<State.APPLY_RESPONSE | State.PAUSE | State.CANCEL | State.ACCEPT> {
assertType(this._editor.hasModel());
assertType(this._activeSession);
assertType(this._activeSession.lastInput);
Expand Down Expand Up @@ -490,6 +490,8 @@ export class InteractiveEditorController implements IEditorContribution {
return State.CANCEL;
} else if (message & Message.PAUSE_SESSION) {
return State.PAUSE;
} else if (message & Message.ACCEPT_SESSION) {
return State.ACCEPT;
} else {
return State.APPLY_RESPONSE;
}
Expand Down Expand Up @@ -599,14 +601,35 @@ export class InteractiveEditorController implements IEditorContribution {

private async [State.ACCEPT]() {
assertType(this._activeSession);
assertType(this._strategy);

try {
await this._strategy.apply();
} catch (err) {
this._dialogService.error(localize('err.apply', "Failed to apply changes.", toErrorMessage(err)));
this._log('FAILED to apply changes');
this._log(err);
}

this._interactiveEditorSessionService.releaseSession(this._activeSession);

this[State.PAUSE]();
}

private async [State.CANCEL]() {
assertType(this._activeSession);
assertType(this._strategy);

const mySession = this._activeSession;

try {
await this._strategy.cancel();
} catch (err) {
this._dialogService.error(localize('err.discard', "Failed to discard changes.", toErrorMessage(err)));
this._log('FAILED to discard changes');
this._log(err);
}

this[State.PAUSE]();

this._stashedSession.clear();
Expand All @@ -620,7 +643,7 @@ export class InteractiveEditorController implements IEditorContribution {

// ---- controller API

accept(): void {
acceptInput(): void {
this._messages.fire(Message.ACCEPT_INPUT);
}

Expand Down Expand Up @@ -688,38 +711,17 @@ export class InteractiveEditorController implements IEditorContribution {
}
}

async applyChanges(): Promise<void> {
if (this._strategy) {
const strategy = this._strategy;
this._strategy = undefined;
try {
await strategy?.apply();
} catch (err) {
this._dialogService.error(localize('err.apply', "Failed to apply changes.", toErrorMessage(err)));
this._log('FAILED to apply changes');
this._log(err);
}
strategy?.dispose();
this._messages.fire(Message.ACCEPT_SESSION);
}
acceptSession(): void {
this._messages.fire(Message.ACCEPT_SESSION);
}

async cancelSession() {
cancelSession() {
if (!this._strategy || !this._activeSession) {
return undefined;
}

const changedText = this._activeSession.asChangedText();
const strategy = this._strategy;
this._strategy = undefined;
try {
await strategy?.cancel();
} catch (err) {
this._dialogService.error(localize('err.discard', "Failed to discard changes.", toErrorMessage(err)));
this._log('FAILED to discard changes');
this._log(err);
}
strategy?.dispose();

this._messages.fire(Message.CANCEL_SESSION);
return changedText;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { IEditorProgressService, IProgressRunner } from 'vs/platform/progress/co
import { mock } from 'vs/base/test/common/mock';
import { Emitter, Event } from 'vs/base/common/event';
import { equals } from 'vs/base/common/arrays';
import { timeout } from 'vs/base/common/async';

suite('InteractiveEditorController', function () {

Expand Down Expand Up @@ -140,9 +141,9 @@ suite('InteractiveEditorController', function () {
ctrl = instaService.createInstance(TestController, editor);
const run = ctrl.run({ message: 'Hello', autoSend: true });

await Event.toPromise(Event.filter(ctrl.onDidChangeState, e => e === State.WAIT_FOR_INPUT));
await ctrl.waitFor(TestController.INIT_SEQUENCE_AUTO_SEND);
assert.ok(ctrl.getWidgetPosition() !== undefined);
await ctrl.cancelSession();
ctrl.cancelSession();

await run;

Expand Down Expand Up @@ -255,10 +256,45 @@ suite('InteractiveEditorController', function () {
assert.ok(session);
assert.deepStrictEqual(session.wholeRange.value, new Range(3, 1, 3, 12));

ctrl.accept();
ctrl.acceptInput();

await ctrl.waitFor([State.MAKE_REQUEST, State.APPLY_RESPONSE, State.SHOW_RESPONSE, State.WAIT_FOR_INPUT]);

assert.deepStrictEqual(session.wholeRange.value, new Range(1, 1, 4, 12));
});

test('Stuck inline chat widget #211', async function () {
const d = interactiveEditorService.addProvider({
debugName: 'Unit Test',
prepareInteractiveEditorSession() {
return {
id: Math.random(),
wholeRange: new Range(3, 1, 3, 3)
};
},
async provideResponse(session, request) {

// SLOW response
await timeout(50000);

return {
type: InteractiveEditorResponseType.EditorEdit,
id: Math.random(),
edits: [{
range: new Range(1, 1, 1, 1), // EDIT happens outside of whole range
text: `${request.prompt}\n${request.prompt}`
}]
};
}
});
store.add(d);
ctrl = instaService.createInstance(TestController, editor);
const p = ctrl.run({ message: 'Hello', autoSend: true });

await ctrl.waitFor([...TestController.INIT_SEQUENCE, State.MAKE_REQUEST]);
ctrl.acceptSession();

await p;
assert.strictEqual(ctrl.getWidgetPosition(), undefined);
});
});