debt - move notebook specific session management into its own place (#185955)

This commit is contained in:
Johannes Rieken
2023-06-23 11:01:16 +02:00
committed by GitHub
parent a0c5c22ede
commit ee2b3a261e
4 changed files with 59 additions and 59 deletions

View File

@@ -4,20 +4,25 @@
*--------------------------------------------------------------------------------------------*/
import { illegalState } from 'vs/base/common/errors';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { Schemas } from 'vs/base/common/network';
import { isEqual } from 'vs/base/common/resources';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { InlineChatController } from 'vs/workbench/contrib/inlineChat/browser/inlineChatController';
import { IInlineChatSessionService } from 'vs/workbench/contrib/inlineChat/browser/inlineChatSession';
import { INotebookEditorService } from 'vs/workbench/contrib/notebook/browser/services/notebookEditorService';
import { CellUri } from 'vs/workbench/contrib/notebook/common/notebookCommon';
export class InlineChatNotebookContribution {
private readonly _store = new DisposableStore();
constructor(
@IInlineChatSessionService sessionService: IInlineChatSessionService,
@INotebookEditorService notebookEditorService: INotebookEditorService,
) {
sessionService.registerSessionKeyComputer(Schemas.vscodeNotebookCell, {
this._store.add(sessionService.registerSessionKeyComputer(Schemas.vscodeNotebookCell, {
getComparisonKey: (_editor, uri) => {
const data = CellUri.parse(uri);
if (!data) {
@@ -30,6 +35,37 @@ export class InlineChatNotebookContribution {
}
throw illegalState('Expected notebook');
}
});
}));
this._store.add(sessionService.onWillStartSession(newSessionEditor => {
const candidate = CellUri.parse(newSessionEditor.getModel().uri);
if (!candidate) {
return;
}
for (const notebookEditor of notebookEditorService.listNotebookEditors()) {
if (isEqual(notebookEditor.textModel?.uri, candidate.notebook)) {
let found = false;
const editors: ICodeEditor[] = [];
for (const [, codeEditor] of notebookEditor.codeEditors) {
editors.push(codeEditor);
found = codeEditor === newSessionEditor || found;
}
if (found) {
// found the this editor in the outer notebook editor -> make sure to
// cancel all sibling sessions
for (const editor of editors) {
if (editor !== newSessionEditor) {
InlineChatController.get(editor)?.finishExistingSession();
}
}
break;
}
}
}
}));
}
dispose(): void {
this._store.dispose();
}
}