Path: blob/main/src/vs/workbench/contrib/inlineChat/browser/inlineChatNotebook.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { illegalState } from '../../../../base/common/errors.js';6import { DisposableStore } from '../../../../base/common/lifecycle.js';7import { Schemas } from '../../../../base/common/network.js';8import { isEqual } from '../../../../base/common/resources.js';9import { ICodeEditor } from '../../../../editor/browser/editorBrowser.js';10import { InlineChatController } from './inlineChatController.js';11import { IInlineChatSessionService } from './inlineChatSessionService.js';12import { INotebookEditorService } from '../../notebook/browser/services/notebookEditorService.js';13import { CellUri } from '../../notebook/common/notebookCommon.js';14import { IEditorService } from '../../../services/editor/common/editorService.js';15import { NotebookTextDiffEditor } from '../../notebook/browser/diff/notebookDiffEditor.js';16import { NotebookMultiTextDiffEditor } from '../../notebook/browser/diff/notebookMultiDiffEditor.js';1718export class InlineChatNotebookContribution {1920private readonly _store = new DisposableStore();2122constructor(23@IInlineChatSessionService sessionService: IInlineChatSessionService,24@IEditorService editorService: IEditorService,25@INotebookEditorService notebookEditorService: INotebookEditorService,26) {2728this._store.add(sessionService.registerSessionKeyComputer(Schemas.vscodeNotebookCell, {29getComparisonKey: (editor, uri) => {30const data = CellUri.parse(uri);31if (!data) {32throw illegalState('Expected notebook cell uri');33}34let fallback: string | undefined;35for (const notebookEditor of notebookEditorService.listNotebookEditors()) {36if (notebookEditor.hasModel() && isEqual(notebookEditor.textModel.uri, data.notebook)) {3738const candidate = `<notebook>${notebookEditor.getId()}#${uri}`;3940if (!fallback) {41fallback = candidate;42}4344// find the code editor in the list of cell-code editors45if (notebookEditor.codeEditors.find((tuple) => tuple[1] === editor)) {46return candidate;47}4849// // reveal cell and try to find code editor again50// const cell = notebookEditor.getCellByHandle(data.handle);51// if (cell) {52// notebookEditor.revealInViewAtTop(cell);53// if (notebookEditor.codeEditors.find((tuple) => tuple[1] === editor)) {54// return candidate;55// }56// }57}58}5960if (fallback) {61return fallback;62}6364const activeEditor = editorService.activeEditorPane;65if (activeEditor && (activeEditor.getId() === NotebookTextDiffEditor.ID || activeEditor.getId() === NotebookMultiTextDiffEditor.ID)) {66return `<notebook>${editor.getId()}#${uri}`;67}6869throw illegalState('Expected notebook editor');70}71}));7273this._store.add(sessionService.onWillStartSession(newSessionEditor => {74const candidate = CellUri.parse(newSessionEditor.getModel().uri);75if (!candidate) {76return;77}78for (const notebookEditor of notebookEditorService.listNotebookEditors()) {79if (isEqual(notebookEditor.textModel?.uri, candidate.notebook)) {80let found = false;81const editors: ICodeEditor[] = [];82for (const [, codeEditor] of notebookEditor.codeEditors) {83editors.push(codeEditor);84found = codeEditor === newSessionEditor || found;85}86if (found) {87// found the this editor in the outer notebook editor -> make sure to88// cancel all sibling sessions89for (const editor of editors) {90if (editor !== newSessionEditor) {91InlineChatController.get(editor)?.acceptSession();92}93}94break;95}96}97}98}));99}100101dispose(): void {102this._store.dispose();103}104}105106107