Skip to content

Commit bd72441

Browse files
committed
state machine polish
1 parent d83b577 commit bd72441

File tree

1 file changed

+23
-49
lines changed

1 file changed

+23
-49
lines changed

src/vs/workbench/contrib/interactiveEditor/browser/interactiveEditorController.ts

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,23 @@ import { INotebookEditorService } from 'vs/workbench/contrib/notebook/browser/se
3838
import { CellUri } from 'vs/workbench/contrib/notebook/common/notebookCommon';
3939

4040
const enum State {
41-
CREATE_SESSION,
42-
INIT_UI,
43-
WAIT_FOR_INPUT,
44-
MAKE_REQUEST,
45-
APPLY_RESPONSE,
46-
SHOW_RESPONSE,
47-
PAUSE,
48-
DONE,
41+
CREATE_SESSION = 'CREATE_SESSION',
42+
INIT_UI = 'INIT_UI',
43+
WAIT_FOR_INPUT = 'WAIT_FOR_INPUT',
44+
MAKE_REQUEST = 'MAKE_REQUEST',
45+
APPLY_RESPONSE = 'APPLY_RESPONSE',
46+
SHOW_RESPONSE = 'SHOW_RESPONSE',
47+
PAUSE = 'PAUSE',
48+
DONE = 'DONE',
4949
}
5050

5151
const enum Message {
5252
NONE = 0,
53-
END_SESSION = 2 ** 0,
54-
PAUSE_SESSION = 2 ** 1,
55-
CANCEL_REQUEST = 2 ** 2,
56-
CANCEL_INPUT = 2 ** 3,
57-
ACCEPT_INPUT = 2 ** 4
53+
END_SESSION = 1 << 0,
54+
PAUSE_SESSION = 1 << 1,
55+
CANCEL_REQUEST = 1 << 2,
56+
CANCEL_INPUT = 1 << 3,
57+
ACCEPT_INPUT = 1 << 4
5858
}
5959

6060
export interface InteractiveEditorRunOptions {
@@ -151,39 +151,13 @@ export class InteractiveEditorController implements IEditorContribution {
151151

152152
private async _nextState(state: State, options: InteractiveEditorRunOptions | undefined): Promise<void> {
153153
this._logService.trace('[IE] setState to ', state);
154-
let nextState: State | undefined;
155-
switch (state) {
156-
case State.CREATE_SESSION:
157-
nextState = await this._createSession(options);
158-
break;
159-
case State.INIT_UI:
160-
nextState = await this._initUI();
161-
break;
162-
case State.WAIT_FOR_INPUT:
163-
nextState = await this._waitForInput(options);
164-
break;
165-
case State.MAKE_REQUEST:
166-
nextState = await this._makeRequest();
167-
break;
168-
case State.APPLY_RESPONSE:
169-
nextState = await this._applyResponse();
170-
break;
171-
case State.SHOW_RESPONSE:
172-
nextState = await this._showResponse();
173-
break;
174-
case State.PAUSE:
175-
this._pause();
176-
break;
177-
case State.DONE:
178-
this._done();
179-
break;
180-
}
154+
const nextState = await this[state](options);
181155
if (nextState) {
182156
this._nextState(nextState, options);
183157
}
184158
}
185159

186-
private async _createSession(options: InteractiveEditorRunOptions | undefined): Promise<State.DONE | State.INIT_UI> {
160+
private async [State.CREATE_SESSION](options: InteractiveEditorRunOptions | undefined): Promise<State.DONE | State.INIT_UI> {
187161
assertType(this._editor.hasModel());
188162

189163
let session: Session | undefined = options?.existingSession;
@@ -228,7 +202,7 @@ export class InteractiveEditorController implements IEditorContribution {
228202
return State.INIT_UI;
229203
}
230204

231-
private async _initUI(): Promise<State.WAIT_FOR_INPUT | State.SHOW_RESPONSE> {
205+
private async [State.INIT_UI](): Promise<State.WAIT_FOR_INPUT | State.SHOW_RESPONSE> {
232206
assertType(this._activeSession);
233207

234208
// hide/cancel inline completions when invoking IE
@@ -299,7 +273,7 @@ export class InteractiveEditorController implements IEditorContribution {
299273
}
300274
}
301275

302-
private async _waitForInput(options: InteractiveEditorRunOptions | undefined): Promise<State.DONE | State.PAUSE | State.WAIT_FOR_INPUT | State.MAKE_REQUEST> {
276+
private async [State.WAIT_FOR_INPUT](options: InteractiveEditorRunOptions | undefined): Promise<State.DONE | State.PAUSE | State.WAIT_FOR_INPUT | State.MAKE_REQUEST> {
303277
assertType(this._activeSession);
304278

305279
this._zone.show(this._activeSession.wholeRange.getEndPosition());
@@ -363,7 +337,7 @@ export class InteractiveEditorController implements IEditorContribution {
363337
return State.MAKE_REQUEST;
364338
}
365339

366-
private async _makeRequest(): Promise<State.APPLY_RESPONSE | State.PAUSE | State.DONE> {
340+
private async [State.MAKE_REQUEST](): Promise<State.APPLY_RESPONSE | State.PAUSE | State.DONE> {
367341
assertType(this._editor.hasModel());
368342
assertType(this._activeSession);
369343
assertType(this._activeSession.lastInput);
@@ -430,7 +404,7 @@ export class InteractiveEditorController implements IEditorContribution {
430404
}
431405
}
432406

433-
private async _applyResponse(): Promise<State.SHOW_RESPONSE | State.DONE> {
407+
private async [State.APPLY_RESPONSE](): Promise<State.SHOW_RESPONSE | State.DONE> {
434408
assertType(this._activeSession);
435409
assertType(this._strategy);
436410

@@ -464,7 +438,7 @@ export class InteractiveEditorController implements IEditorContribution {
464438
return State.SHOW_RESPONSE;
465439
}
466440

467-
private async _showResponse(): Promise<State.WAIT_FOR_INPUT | State.DONE> {
441+
private async [State.SHOW_RESPONSE](): Promise<State.WAIT_FOR_INPUT | State.DONE> {
468442
assertType(this._activeSession);
469443
assertType(this._strategy);
470444

@@ -513,7 +487,7 @@ export class InteractiveEditorController implements IEditorContribution {
513487
return State.WAIT_FOR_INPUT;
514488
}
515489

516-
private async _pause() {
490+
private async [State.PAUSE]() {
517491
assertType(this._activeSession);
518492

519493
this._ctxLastEditKind.reset();
@@ -531,10 +505,10 @@ export class InteractiveEditorController implements IEditorContribution {
531505
this._activeSession = undefined;
532506
}
533507

534-
private async _done() {
508+
private async [State.DONE]() {
535509
assertType(this._activeSession);
536510
this._interactiveEditorSessionService.releaseSession(this._activeSession);
537-
this._pause();
511+
this[State.PAUSE]();
538512
}
539513

540514
// ---- controller API

0 commit comments

Comments
 (0)