1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Emitter } from 'vs/base/common/event';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { ResourceMap } from 'vs/base/common/map';
import { ExtensionIdentifier, IExtensionDescription } from 'vs/platform/extensions/common/extensions';
import { ILogService } from 'vs/platform/log/common/log';
import { ExtHostNotebookProxyKernelsShape, IMainContext, INotebookProxyKernelDto, MainContext, MainThreadNotebookProxyKernelsShape } from 'vs/workbench/api/common/extHost.protocol';
import { createKernelId, ExtHostNotebookKernels } from 'vs/workbench/api/common/extHostNotebookKernels';
import { checkProposedApiEnabled } from 'vs/workbench/services/extensions/common/extensions';
import * as vscode from 'vscode';
interface IProxyKernelData {
extensionId: ExtensionIdentifier;
controller: vscode.NotebookProxyController;
onDidChangeSelection: Emitter<{ selected: boolean; notebook: vscode.NotebookDocument }>;
associatedNotebooks: ResourceMap<boolean>;
}
export type SelectKernelReturnArgs = ControllerInfo | { notebookEditorId: string } | ControllerInfo & { notebookEditorId: string } | undefined;
type ControllerInfo = { id: string; extension: string };
export class ExtHostNotebookProxyKernels implements ExtHostNotebookProxyKernelsShape {
private readonly _proxy: MainThreadNotebookProxyKernelsShape;
private readonly _proxyKernelData: Map<number, IProxyKernelData> = new Map<number, IProxyKernelData>();
private _handlePool: number = 0;
private readonly _onDidChangeCellExecutionState = new Emitter<vscode.NotebookCellExecutionStateChangeEvent>();
readonly onDidChangeNotebookCellExecutionState = this._onDidChangeCellExecutionState.event;
constructor(
mainContext: IMainContext,
private readonly extHostNotebook: ExtHostNotebookKernels,
@ILogService private readonly _logService: ILogService
) {
this._proxy = mainContext.getProxy(MainContext.MainThreadNotebookProxyKernels);
}
createNotebookProxyController(extension: IExtensionDescription, id: string, viewType: string, label: string, handler: () => vscode.NotebookController | string | Thenable<vscode.NotebookController | string>): vscode.NotebookProxyController {
const handle = this._handlePool++;
let isDisposed = false;
const commandDisposables = new DisposableStore();
const onDidChangeSelection = new Emitter<{ selected: boolean; notebook: vscode.NotebookDocument }>();
const data: INotebookProxyKernelDto = {
id: createKernelId(extension.identifier, id),
notebookType: viewType,
extensionId: extension.identifier,
extensionLocation: extension.extensionLocation,
label: label || extension.identifier.value,
};
let _resolveHandler = handler;
this._proxy.$addProxyKernel(handle, data).catch(err => {
// this can happen when a kernel with that ID is already registered
console.log(err);
isDisposed = true;
});
let tokenPool = 0;
const _update = () => {
if (isDisposed) {
return;
}
const myToken = ++tokenPool;
Promise.resolve().then(() => {
if (myToken === tokenPool) {
this._proxy.$updateProxyKernel(handle, data);
}
});
};
// notebook documents that are associated to this controller
const associatedNotebooks = new ResourceMap<boolean>();
const controller: vscode.NotebookProxyController = {
get id() { return id; },
get notebookType() { return data.notebookType; },
onDidChangeSelectedNotebooks: onDidChangeSelection.event,
get label() {
return data.label;
},
set label(value) {
data.label = value ?? extension.displayName ?? extension.name;
_update();
},
get detail() {
return data.detail ?? '';
},
set detail(value) {
data.detail = value;
_update();
},
get description() {
return data.description ?? '';
},
set description(value) {
data.description = value;
_update();
},
get kind() {
checkProposedApiEnabled(extension, 'notebookControllerKind');
return data.kind ?? '';
},
set kind(value) {
checkProposedApiEnabled(extension, 'notebookControllerKind');
data.kind = value;
_update();
},
get resolveHandler() {
return _resolveHandler;
},
dispose: () => {
if (!isDisposed) {
this._logService.trace(`NotebookProxyController[${handle}], DISPOSED`);
isDisposed = true;
this._proxyKernelData.delete(handle);
commandDisposables.dispose();
onDidChangeSelection.dispose();
this._proxy.$removeProxyKernel(handle);
}
}
};
this._proxyKernelData.set(handle, {
extensionId: extension.identifier,
controller,
onDidChangeSelection,
associatedNotebooks
});
return controller;
}
async $resolveKernel(handle: number): Promise<string | null> {
const obj = this._proxyKernelData.get(handle);
if (!obj) {
// extension can dispose kernels in the meantime
return null;
}
const controller = await obj.controller.resolveHandler();
if (typeof controller === 'string') {
return controller;
} else {
return this.extHostNotebook.getIdByController(controller);
}
}
}
|