Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/inlineChat/browser/inlineChatNotebook.ts
3296 views
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 '../../../../base/common/errors.js';
7
import { DisposableStore } from '../../../../base/common/lifecycle.js';
8
import { Schemas } from '../../../../base/common/network.js';
9
import { isEqual } from '../../../../base/common/resources.js';
10
import { ICodeEditor } from '../../../../editor/browser/editorBrowser.js';
11
import { InlineChatController } from './inlineChatController.js';
12
import { IInlineChatSessionService } from './inlineChatSessionService.js';
13
import { INotebookEditorService } from '../../notebook/browser/services/notebookEditorService.js';
14
import { CellUri } from '../../notebook/common/notebookCommon.js';
15
import { IEditorService } from '../../../services/editor/common/editorService.js';
16
import { NotebookTextDiffEditor } from '../../notebook/browser/diff/notebookDiffEditor.js';
17
import { NotebookMultiTextDiffEditor } from '../../notebook/browser/diff/notebookMultiDiffEditor.js';
18
19
export class InlineChatNotebookContribution {
20
21
private readonly _store = new DisposableStore();
22
23
constructor(
24
@IInlineChatSessionService sessionService: IInlineChatSessionService,
25
@IEditorService editorService: IEditorService,
26
@INotebookEditorService notebookEditorService: INotebookEditorService,
27
) {
28
29
this._store.add(sessionService.registerSessionKeyComputer(Schemas.vscodeNotebookCell, {
30
getComparisonKey: (editor, uri) => {
31
const data = CellUri.parse(uri);
32
if (!data) {
33
throw illegalState('Expected notebook cell uri');
34
}
35
let fallback: string | undefined;
36
for (const notebookEditor of notebookEditorService.listNotebookEditors()) {
37
if (notebookEditor.hasModel() && isEqual(notebookEditor.textModel.uri, data.notebook)) {
38
39
const candidate = `<notebook>${notebookEditor.getId()}#${uri}`;
40
41
if (!fallback) {
42
fallback = candidate;
43
}
44
45
// find the code editor in the list of cell-code editors
46
if (notebookEditor.codeEditors.find((tuple) => tuple[1] === editor)) {
47
return candidate;
48
}
49
50
// // reveal cell and try to find code editor again
51
// const cell = notebookEditor.getCellByHandle(data.handle);
52
// if (cell) {
53
// notebookEditor.revealInViewAtTop(cell);
54
// if (notebookEditor.codeEditors.find((tuple) => tuple[1] === editor)) {
55
// return candidate;
56
// }
57
// }
58
}
59
}
60
61
if (fallback) {
62
return fallback;
63
}
64
65
const activeEditor = editorService.activeEditorPane;
66
if (activeEditor && (activeEditor.getId() === NotebookTextDiffEditor.ID || activeEditor.getId() === NotebookMultiTextDiffEditor.ID)) {
67
return `<notebook>${editor.getId()}#${uri}`;
68
}
69
70
throw illegalState('Expected notebook editor');
71
}
72
}));
73
74
this._store.add(sessionService.onWillStartSession(newSessionEditor => {
75
const candidate = CellUri.parse(newSessionEditor.getModel().uri);
76
if (!candidate) {
77
return;
78
}
79
for (const notebookEditor of notebookEditorService.listNotebookEditors()) {
80
if (isEqual(notebookEditor.textModel?.uri, candidate.notebook)) {
81
let found = false;
82
const editors: ICodeEditor[] = [];
83
for (const [, codeEditor] of notebookEditor.codeEditors) {
84
editors.push(codeEditor);
85
found = codeEditor === newSessionEditor || found;
86
}
87
if (found) {
88
// found the this editor in the outer notebook editor -> make sure to
89
// cancel all sibling sessions
90
for (const editor of editors) {
91
if (editor !== newSessionEditor) {
92
InlineChatController.get(editor)?.acceptSession();
93
}
94
}
95
break;
96
}
97
}
98
}
99
}));
100
}
101
102
dispose(): void {
103
this._store.dispose();
104
}
105
}
106
107