Skip to content

Commit ee2b3a2

Browse files
authored
debt - move notebook specific session management into its own place (microsoft#185955)
1 parent a0c5c22 commit ee2b3a2

File tree

4 files changed

+59
-59
lines changed

4 files changed

+59
-59
lines changed

src/vs/workbench/contrib/codeEditor/browser/untitledTextEditorHint/untitledTextEditorHint.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editor
2121
import { IContentActionHandler, renderFormattedText } from 'vs/base/browser/formattedTextRenderer';
2222
import { ApplyFileSnippetAction } from 'vs/workbench/contrib/snippets/browser/commands/fileTemplateSnippets';
2323
import { IInlineChatSessionService } from 'vs/workbench/contrib/inlineChat/browser/inlineChatSession';
24-
import { isEqual } from 'vs/base/common/resources';
2524

2625
const $ = dom.$;
2726

@@ -49,8 +48,8 @@ export class UntitledTextEditorHintContribution implements IEditorContribution {
4948
this.update();
5049
}
5150
}));
52-
this.toDispose.push(inlineChatSessionService.onWillStartSession(uri => {
53-
if (isEqual(uri, this.editor.getModel()?.uri)) {
51+
this.toDispose.push(inlineChatSessionService.onWillStartSession(editor => {
52+
if (this.editor === editor) {
5453
this.untitledTextHintContentWidget?.dispose();
5554
}
5655
}));

src/vs/workbench/contrib/inlineChat/browser/inlineChatController.ts

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { CancellationTokenSource } from 'vs/base/common/cancellation';
1010
import { toErrorMessage } from 'vs/base/common/errorMessage';
1111
import { Emitter, Event } from 'vs/base/common/event';
1212
import { DisposableStore, IDisposable, MutableDisposable, toDisposable } from 'vs/base/common/lifecycle';
13-
import { isEqual } from 'vs/base/common/resources';
1413
import { StopWatch } from 'vs/base/common/stopwatch';
1514
import { assertType } from 'vs/base/common/types';
1615
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
@@ -35,8 +34,6 @@ import { InlineChatZoneWidget } from 'vs/workbench/contrib/inlineChat/browser/in
3534
import { CTX_INLINE_CHAT_HAS_ACTIVE_REQUEST, CTX_INLINE_CHAT_LAST_FEEDBACK, IInlineChatRequest, IInlineChatResponse, INLINE_CHAT_ID, EditMode, InlineChatResponseFeedbackKind, CTX_INLINE_CHAT_LAST_RESPONSE_TYPE, InlineChatResponseType, CTX_INLINE_CHAT_DID_EDIT, CTX_INLINE_CHAT_HAS_STASHED_SESSION, InlineChateResponseTypes, CTX_INLINE_CHAT_RESPONSE_TYPES } from 'vs/workbench/contrib/inlineChat/common/inlineChat';
3635
import { IChatAccessibilityService, IChatWidgetService } from 'vs/workbench/contrib/chat/browser/chat';
3736
import { IChatService } from 'vs/workbench/contrib/chat/common/chatService';
38-
import { INotebookEditorService } from 'vs/workbench/contrib/notebook/browser/services/notebookEditorService';
39-
import { CellUri } from 'vs/workbench/contrib/notebook/common/notebookCommon';
4037
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
4138
import { Lazy } from 'vs/base/common/lazy';
4239

@@ -111,7 +108,6 @@ export class InlineChatController implements IEditorContribution {
111108
@ILogService private readonly _logService: ILogService,
112109
@IConfigurationService private readonly _configurationService: IConfigurationService,
113110
@IModelService private readonly _modelService: IModelService,
114-
@INotebookEditorService private readonly _notebookEditorService: INotebookEditorService,
115111
@IDialogService private readonly _dialogService: IDialogService,
116112
@IContextKeyService contextKeyService: IContextKeyService,
117113
@IAccessibilityService private readonly _accessibilityService: IAccessibilityService,
@@ -144,7 +140,7 @@ export class InlineChatController implements IEditorContribution {
144140

145141
dispose(): void {
146142
this._stashedSession.clear();
147-
this._finishExistingSession();
143+
this.finishExistingSession();
148144
this._store.dispose();
149145
this._log('controller disposed');
150146
}
@@ -177,25 +173,13 @@ export class InlineChatController implements IEditorContribution {
177173

178174
async run(options: InlineChatRunOptions | undefined = {}): Promise<void> {
179175
this._log('session starting');
180-
await this._finishExistingSession();
176+
await this.finishExistingSession();
181177
this._stashedSession.clear();
182178

183179
await this._nextState(State.CREATE_SESSION, options);
184180
this._log('session done or paused');
185181
}
186182

187-
private async _finishExistingSession(): Promise<void> {
188-
if (this._activeSession) {
189-
if (this._activeSession.editMode === EditMode.Preview) {
190-
this._log('finishing existing session, using CANCEL', this._activeSession.editMode);
191-
this.cancelSession();
192-
} else {
193-
this._log('finishing existing session, using APPLY', this._activeSession.editMode);
194-
this.acceptSession();
195-
}
196-
}
197-
}
198-
199183
// ---- state machine
200184

201185
private _showWidget(initialRender: boolean = false) {
@@ -296,8 +280,6 @@ export class InlineChatController implements IEditorContribution {
296280
// hide/cancel inline completions when invoking IE
297281
InlineCompletionsController.get(this._editor)?.hide();
298282

299-
this._cancelNotebookSiblingEditors();
300-
301283
this._sessionStore.clear();
302284

303285
const wholeRangeDecoration = this._editor.createDecorationsCollection();
@@ -348,7 +330,7 @@ export class InlineChatController implements IEditorContribution {
348330

349331
if (editIsOutsideOfWholeRange) {
350332
this._log('text changed outside of whole range, FINISH session');
351-
this._finishExistingSession();
333+
this.finishExistingSession();
352334
}
353335
}));
354336

@@ -375,35 +357,6 @@ export class InlineChatController implements IEditorContribution {
375357
return result;
376358
}
377359

378-
private _cancelNotebookSiblingEditors(): void {
379-
if (!this._editor.hasModel()) {
380-
return;
381-
}
382-
const candidate = CellUri.parse(this._editor.getModel().uri);
383-
if (!candidate) {
384-
return;
385-
}
386-
for (const editor of this._notebookEditorService.listNotebookEditors()) {
387-
if (isEqual(editor.textModel?.uri, candidate.notebook)) {
388-
let found = false;
389-
const editors: ICodeEditor[] = [];
390-
for (const [, codeEditor] of editor.codeEditors) {
391-
editors.push(codeEditor);
392-
found = codeEditor === this._editor || found;
393-
}
394-
if (found) {
395-
// found the this editor in the outer notebook editor -> make sure to
396-
// cancel all sibling sessions
397-
for (const editor of editors) {
398-
if (editor !== this._editor) {
399-
InlineChatController.get(editor)?._finishExistingSession();
400-
}
401-
}
402-
break;
403-
}
404-
}
405-
}
406-
}
407360

408361
private async [State.WAIT_FOR_INPUT](options: InlineChatRunOptions): Promise<State.ACCEPT | State.CANCEL | State.PAUSE | State.WAIT_FOR_INPUT | State.MAKE_REQUEST> {
409362
assertType(this._activeSession);
@@ -818,6 +771,18 @@ export class InlineChatController implements IEditorContribution {
818771
return result;
819772
}
820773

774+
async finishExistingSession(): Promise<void> {
775+
if (this._activeSession) {
776+
if (this._activeSession.editMode === EditMode.Preview) {
777+
this._log('finishing existing session, using CANCEL', this._activeSession.editMode);
778+
this.cancelSession();
779+
} else {
780+
this._log('finishing existing session, using APPLY', this._activeSession.editMode);
781+
this.acceptSession();
782+
}
783+
}
784+
}
785+
821786
unstashLastSession(): Session | undefined {
822787
return this._stashedSession.value?.unstash();
823788
}

src/vs/workbench/contrib/inlineChat/browser/inlineChatNotebook.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,25 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { illegalState } from 'vs/base/common/errors';
7+
import { DisposableStore } from 'vs/base/common/lifecycle';
78
import { Schemas } from 'vs/base/common/network';
89
import { isEqual } from 'vs/base/common/resources';
10+
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
11+
import { InlineChatController } from 'vs/workbench/contrib/inlineChat/browser/inlineChatController';
912
import { IInlineChatSessionService } from 'vs/workbench/contrib/inlineChat/browser/inlineChatSession';
1013
import { INotebookEditorService } from 'vs/workbench/contrib/notebook/browser/services/notebookEditorService';
1114
import { CellUri } from 'vs/workbench/contrib/notebook/common/notebookCommon';
1215

1316
export class InlineChatNotebookContribution {
1417

18+
private readonly _store = new DisposableStore();
19+
1520
constructor(
1621
@IInlineChatSessionService sessionService: IInlineChatSessionService,
1722
@INotebookEditorService notebookEditorService: INotebookEditorService,
1823
) {
1924

20-
sessionService.registerSessionKeyComputer(Schemas.vscodeNotebookCell, {
25+
this._store.add(sessionService.registerSessionKeyComputer(Schemas.vscodeNotebookCell, {
2126
getComparisonKey: (_editor, uri) => {
2227
const data = CellUri.parse(uri);
2328
if (!data) {
@@ -30,6 +35,37 @@ export class InlineChatNotebookContribution {
3035
}
3136
throw illegalState('Expected notebook');
3237
}
33-
});
38+
}));
39+
40+
this._store.add(sessionService.onWillStartSession(newSessionEditor => {
41+
const candidate = CellUri.parse(newSessionEditor.getModel().uri);
42+
if (!candidate) {
43+
return;
44+
}
45+
for (const notebookEditor of notebookEditorService.listNotebookEditors()) {
46+
if (isEqual(notebookEditor.textModel?.uri, candidate.notebook)) {
47+
let found = false;
48+
const editors: ICodeEditor[] = [];
49+
for (const [, codeEditor] of notebookEditor.codeEditors) {
50+
editors.push(codeEditor);
51+
found = codeEditor === newSessionEditor || found;
52+
}
53+
if (found) {
54+
// found the this editor in the outer notebook editor -> make sure to
55+
// cancel all sibling sessions
56+
for (const editor of editors) {
57+
if (editor !== newSessionEditor) {
58+
InlineChatController.get(editor)?.finishExistingSession();
59+
}
60+
}
61+
break;
62+
}
63+
}
64+
}
65+
}));
66+
}
67+
68+
dispose(): void {
69+
this._store.dispose();
3470
}
3571
}

src/vs/workbench/contrib/inlineChat/browser/inlineChatSession.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ export const IInlineChatSessionService = createDecorator<IInlineChatSessionServi
363363
export interface IInlineChatSessionService {
364364
_serviceBrand: undefined;
365365

366-
onWillStartSession: Event<URI>;
366+
onWillStartSession: Event<IActiveCodeEditor>;
367367

368368
createSession(editor: IActiveCodeEditor, options: { editMode: EditMode; wholeRange?: IRange }, token: CancellationToken): Promise<Session | undefined>;
369369

@@ -387,8 +387,8 @@ export class InlineChatSessionService implements IInlineChatSessionService {
387387

388388
declare _serviceBrand: undefined;
389389

390-
private readonly _onWillStartSession = new Emitter<URI>();
391-
readonly onWillStartSession: Event<URI> = this._onWillStartSession.event;
390+
private readonly _onWillStartSession = new Emitter<IActiveCodeEditor>();
391+
readonly onWillStartSession: Event<IActiveCodeEditor> = this._onWillStartSession.event;
392392

393393
private readonly _sessions = new Map<string, SessionData>();
394394
private readonly _keyComputers = new Map<string, ISessionKeyComputer>();
@@ -416,7 +416,7 @@ export class InlineChatSessionService implements IInlineChatSessionService {
416416
return undefined;
417417
}
418418

419-
this._onWillStartSession.fire(editor.getModel().uri);
419+
this._onWillStartSession.fire(editor);
420420

421421
const textModel = editor.getModel();
422422
const selection = editor.getSelection();

0 commit comments

Comments
 (0)