Skip to content

Commit ddbac48

Browse files
committed
notebooks create/dispose editors. this means controllers must be created eagerly (😢) and that notebooks need a custom way of plugging comparision keys for session. works unless creating another session for the same cell of a duplicated editor
1 parent a5ab253 commit ddbac48

File tree

3 files changed

+80
-27
lines changed

3 files changed

+80
-27
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ import { IInteractiveEditorService, INTERACTIVE_EDITOR_ID } from 'vs/workbench/c
1111
import { InstantiationType, registerSingleton } from 'vs/platform/instantiation/common/extensions';
1212
import { InteractiveEditorServiceImpl } from 'vs/workbench/contrib/interactiveEditor/common/interactiveEditorServiceImpl';
1313
import { IInteractiveEditorSessionService, InteractiveEditorSessionService } from 'vs/workbench/contrib/interactiveEditor/browser/interactiveEditorSession';
14+
import { Registry } from 'vs/platform/registry/common/platform';
15+
import { IWorkbenchContributionsRegistry, Extensions } from 'vs/workbench/common/contributions';
16+
import { LifecyclePhase } from 'vs/workbench/services/lifecycle/common/lifecycle';
17+
import { InteractiveEditorNotebookContribution } from 'vs/workbench/contrib/interactiveEditor/browser/interactiveEditorNotebook';
1418

1519
registerSingleton(IInteractiveEditorService, InteractiveEditorServiceImpl, InstantiationType.Delayed);
1620
registerSingleton(IInteractiveEditorSessionService, InteractiveEditorSessionService, InstantiationType.Delayed);
1721

18-
registerEditorContribution(INTERACTIVE_EDITOR_ID, InteractiveEditorController, EditorContributionInstantiation.Lazy);
22+
registerEditorContribution(INTERACTIVE_EDITOR_ID, InteractiveEditorController, EditorContributionInstantiation.Eager); // EAGER because of notebook dispose/create of editors
1923

2024
registerAction2(interactiveEditorActions.StartSessionAction);
2125
registerAction2(interactiveEditorActions.UnstashSessionAction);
@@ -42,3 +46,7 @@ registerAction2(interactiveEditorActions.FeebackUnhelpfulCommand);
4246
registerAction2(interactiveEditorActions.ApplyPreviewEdits);
4347

4448
registerAction2(interactiveEditorActions.CopyRecordings);
49+
50+
51+
Registry.as<IWorkbenchContributionsRegistry>(Extensions.Workbench)
52+
.registerWorkbenchContribution(InteractiveEditorNotebookContribution, LifecyclePhase.Restored);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { illegalState } from 'vs/base/common/errors';
7+
import { Schemas } from 'vs/base/common/network';
8+
import { isEqual } from 'vs/base/common/resources';
9+
import { IInteractiveEditorSessionService } from 'vs/workbench/contrib/interactiveEditor/browser/interactiveEditorSession';
10+
import { INotebookEditorService } from 'vs/workbench/contrib/notebook/browser/services/notebookEditorService';
11+
import { CellUri } from 'vs/workbench/contrib/notebook/common/notebookCommon';
12+
13+
export class InteractiveEditorNotebookContribution {
14+
15+
constructor(
16+
@IInteractiveEditorSessionService sessionService: IInteractiveEditorSessionService,
17+
@INotebookEditorService notebookEditorService: INotebookEditorService,
18+
) {
19+
20+
sessionService.registerSessionKeyComputer(Schemas.vscodeNotebookCell, {
21+
getComparisonKey: (_editor, uri) => {
22+
const data = CellUri.parse(uri);
23+
if (!data) {
24+
throw illegalState('Expected notebook');
25+
}
26+
for (const editor of notebookEditorService.listNotebookEditors()) {
27+
if (isEqual(editor.textModel?.uri, data.notebook)) {
28+
return `<notebook>${editor.getId()}#${uri}`;
29+
}
30+
}
31+
throw illegalState('Expected notebook');
32+
}
33+
});
34+
}
35+
}

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

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { EditMode, IInteractiveEditorSessionProvider, IInteractiveEditorSession,
1313
import { IRange, Range } from 'vs/editor/common/core/range';
1414
import { IActiveCodeEditor, ICodeEditor } from 'vs/editor/browser/editorBrowser';
1515
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
16-
import { ResourceMap } from 'vs/base/common/map';
1716
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
1817
import { IModelService } from 'vs/editor/common/services/model';
1918
import { ITextModelService } from 'vs/editor/common/services/resolverService';
@@ -266,6 +265,10 @@ export class EditResponse {
266265
}
267266
}
268267

268+
export interface ISessionKeyComputer {
269+
getComparisonKey(editor: ICodeEditor, uri: URI): string;
270+
}
271+
269272
export const IInteractiveEditorSessionService = createDecorator<IInteractiveEditorSessionService>('IInteractiveEditorSessionService');
270273

271274
export interface IInteractiveEditorSessionService {
@@ -277,6 +280,8 @@ export interface IInteractiveEditorSessionService {
277280

278281
releaseSession(session: Session): void;
279282

283+
registerSessionKeyComputer(scheme: string, value: ISessionKeyComputer): IDisposable;
284+
280285
//
281286

282287
recordings(): readonly Recording[];
@@ -291,7 +296,8 @@ export class InteractiveEditorSessionService implements IInteractiveEditorSessio
291296

292297
declare _serviceBrand: undefined;
293298

294-
private readonly _sessions = new Map<ICodeEditor, ResourceMap<SessionData>>();
299+
private readonly _sessions = new Map<string, SessionData>();
300+
private readonly _keyComputers = new Map<string, ISessionKeyComputer>();
295301
private _recordings: Recording[] = [];
296302

297303
constructor(
@@ -360,37 +366,27 @@ export class InteractiveEditorSessionService implements IInteractiveEditorSessio
360366

361367
const session = new Session(options.editMode, editor, textModel0, textModel, provider, raw, wholeRangeDecorationId);
362368

363-
// store: editor -> uri -> session
364-
let map = this._sessions.get(editor);
365-
if (!map) {
366-
map = new ResourceMap<SessionData>();
367-
this._sessions.set(editor, map);
369+
// store: key -> session
370+
const key = this._key(editor, textModel.uri);
371+
if (this._sessions.has(key)) {
372+
store.dispose();
373+
throw new Error(`Session already stored for ${key}`);
368374
}
369-
if (map.has(textModel.uri)) {
370-
throw new Error(`Session already stored for ${textModel.uri}`);
371-
}
372-
map.set(textModel.uri, { session, store });
375+
this._sessions.set(key, { session, store });
373376
return session;
374377
}
375378

376379
releaseSession(session: Session): void {
377380

378-
const { editor, textModelN } = session;
381+
const { editor } = session;
379382

380383
// cleanup
381-
const map = this._sessions.get(editor);
382-
if (map) {
383-
const data = map.get(textModelN.uri);
384-
if (data) {
385-
data.store.dispose();
386-
data.session.session.dispose?.();
387-
388-
map.delete(textModelN.uri);
389-
this._logService.trace(`[IE] did RELEASE session for ${editor.getId()}, ${session.provider.debugName}`);
390-
391-
}
392-
if (map.size === 0) {
393-
this._sessions.delete(editor);
384+
for (const [key, value] of this._sessions) {
385+
if (value.session === session) {
386+
value.store.dispose();
387+
this._sessions.delete(key);
388+
this._logService.trace(`[IE] did RELEASED session for ${editor.getId()}, ${session.provider.debugName}`);
389+
break;
394390
}
395391
}
396392

@@ -405,7 +401,21 @@ export class InteractiveEditorSessionService implements IInteractiveEditorSessio
405401
}
406402

407403
getSession(editor: ICodeEditor, uri: URI): Session | undefined {
408-
return this._sessions.get(editor)?.get(uri)?.session;
404+
const key = this._key(editor, uri);
405+
return this._sessions.get(key)?.session;
406+
}
407+
408+
private _key(editor: ICodeEditor, uri: URI): string {
409+
const item = this._keyComputers.get(uri.scheme);
410+
return item
411+
? item.getComparisonKey(editor, uri)
412+
: `${editor.getId()}@${uri.toString()}`;
413+
414+
}
415+
416+
registerSessionKeyComputer(scheme: string, value: ISessionKeyComputer): IDisposable {
417+
this._keyComputers.set(scheme, value);
418+
return toDisposable(() => this._keyComputers.delete(scheme));
409419
}
410420

411421
// --- debug

0 commit comments

Comments
 (0)