add onDidChangeNotebookAssociation-API and wire everything up using the glue adapter

上级 ade32e71
......@@ -1448,20 +1448,16 @@ declare module 'vscode' {
// select notebook of a type and/or by file-pattern
readonly selector: NotebookSelector;
// selection is tricky/bogous because a kernel can be selected for
// different notebook documents. A handler-approach might be the better
// fit here, e.g:
// selectionHandler?: (notebook: NotebookDocument, selected: boolean) => void;
// // is this kernel selected
// readonly selected: boolean;
// // fired when kernel is selected/unselected
// readonly onDidChangeSelection: Event<boolean>;
/**
* A kernel can apply to one or many notebook documents but a notebook has only one active
* kernel. This event fires whenever a notebook has been associated to a kernel or when
* that association has been removed.
*/
readonly onDidChangeNotebookAssociation: Event<{ notebook: NotebookDocument, selected: boolean }>;
// kernels can establish IPC channels to (visible) notebook editors
// createNotebookCommunication(editor: vscode.NotebookEditor): vscode.NotebookCommunication;
// UI properties (get/set)
label: string;
description: string;
......
......@@ -21,7 +21,7 @@ export class ExtHostNotebookKernels implements ExtHostNotebookKernelsShape {
private readonly _proxy: MainThreadNotebookKernelsShape;
private readonly _kernelData = new Map<number, { id: string, executeHandler: ExecuteHandler, interruptHandler?: InterruptHandler, onDidChangeSelection: Emitter<{ selected: boolean, uri: URI }> }>();
private readonly _kernelData = new Map<number, { id: string, executeHandler: ExecuteHandler, interruptHandler?: InterruptHandler, onDidChangeSelection: Emitter<{ selected: boolean, notebook: vscode.NotebookDocument }> }>();
private _handlePool: number = 0;
constructor(
......@@ -39,7 +39,7 @@ export class ExtHostNotebookKernels implements ExtHostNotebookKernelsShape {
let isDisposed = false;
const commandDisposables = new DisposableStore();
const emitter = new Emitter<{ selected: boolean, uri: URI }>();
const emitter = new Emitter<{ selected: boolean, notebook: vscode.NotebookDocument }>();
this._kernelData.set(handle, { id: options.id, executeHandler: options.executeHandler, onDidChangeSelection: emitter });
const data: INotebookKernelDto2 = {
......@@ -73,8 +73,7 @@ export class ExtHostNotebookKernels implements ExtHostNotebookKernelsShape {
return {
get id() { return data.id; },
get selector() { return data.selector; },
// onDidChangeSelection: emitter.event,
onDidChangeNotebookAssociation: emitter.event,
get label() {
return data.label;
},
......@@ -133,7 +132,7 @@ export class ExtHostNotebookKernels implements ExtHostNotebookKernelsShape {
if (obj) {
obj.onDidChangeSelection.fire({
selected: value,
uri: URI.revive(uri)
notebook: this._extHostNotebook.lookupNotebookDocument(URI.revive(uri))!.notebookDocument
});
}
}
......
......@@ -54,6 +54,9 @@ import { Event } from 'vs/base/common/event';
import { IAccessibilityService } from 'vs/platform/accessibility/common/accessibility';
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { getFormatedMetadataJSON } from 'vs/workbench/contrib/notebook/browser/diff/diffElementViewModel';
import { NotebookModelResolverServiceImpl } from 'vs/workbench/contrib/notebook/common/notebookEditorModelResolverServiceImpl';
import { INotebookKernelService } from 'vs/workbench/contrib/notebook/common/notebookKernelService';
import { NotebookKernelService } from 'vs/workbench/contrib/notebook/browser/notebookKernelServiceImpl';
// Editor Contribution
import 'vs/workbench/contrib/notebook/browser/contrib/clipboard/notebookClipboard';
......@@ -75,9 +78,6 @@ import 'vs/workbench/contrib/notebook/browser/diff/notebookDiffActions';
// Output renderers registration
import 'vs/workbench/contrib/notebook/browser/view/output/transforms/richTransform';
import { NotebookModelResolverServiceImpl } from 'vs/workbench/contrib/notebook/common/notebookEditorModelResolverServiceImpl';
import { INotebookKernelService } from 'vs/workbench/contrib/notebook/common/notebookKernelService';
import { NotebookKernelService } from 'vs/workbench/contrib/notebook/common/notebookKernelServiceImpl';
/*--------------------------------------------------------------------------------------------- */
......
......@@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import { Event, Emitter } from 'vs/base/common/event';
import { DisposableStore, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { DisposableStore, dispose, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { ICellRange, INotebookKernel, INotebookTextModel } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { INotebookKernelBindEvent, INotebookKernel2, INotebookKernelService } from 'vs/workbench/contrib/notebook/common/notebookKernelService';
import { score } from 'vs/workbench/contrib/notebook/common/notebookSelector';
......@@ -14,6 +14,7 @@ import { LifecyclePhase } from 'vs/workbench/services/lifecycle/common/lifecycle
import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService';
import { URI } from 'vs/base/common/uri';
import { ResourceMap } from 'vs/base/common/map';
import { INotebookEditorService } from 'vs/workbench/contrib/notebook/browser/notebookEditorService';
export class NotebookKernelService implements INotebookKernelService {
......@@ -86,10 +87,14 @@ export class NotebookKernelService implements INotebookKernelService {
// a notebook has one kernel, a kernel has N notebooks
// notebook <-1----N-> kernel
bindNotebookToKernel(notebook: INotebookTextModel, kernel: INotebookKernel2): void {
updateNotebookKernelBinding(notebook: INotebookTextModel, kernel: INotebookKernel2 | undefined): void {
const oldKernel = this._kernelBindings.get(notebook.uri);
if (oldKernel !== kernel) {
this._kernelBindings.set(notebook.uri, kernel);
if (kernel) {
this._kernelBindings.set(notebook.uri, kernel);
} else {
this._kernelBindings.delete(notebook.uri);
}
this._onDidChangeNotebookKernelBinding.fire({ notebook: notebook.uri, oldKernel, newKernel: kernel });
}
}
......@@ -105,14 +110,12 @@ class KernelAdaptorBridge implements IWorkbenchContribution {
constructor(
@INotebookKernelService notebookKernelService: INotebookKernelService,
@INotebookService notebookService: INotebookService,
@INotebookEditorService notebookEditorService: INotebookEditorService
) {
const disposables = new DisposableStore();
const emitter = new Emitter<URI | undefined>();
const kernels = new Map<INotebookKernel2, IDisposable>();
disposables.add(notebookKernelService.onDidAddKernel(kernel => {
......@@ -130,7 +133,7 @@ class KernelAdaptorBridge implements IWorkbenchContribution {
}
}));
// kernel -> provider
const registration = notebookService.registerNotebookKernelProvider({
onDidChangeKernels: emitter.event,
providerExtensionId: 'notAnExtension',
......@@ -141,8 +144,7 @@ class KernelAdaptorBridge implements IWorkbenchContribution {
if (!model) {
return [];
}
const kernels = notebookKernelService.getKernels(model);
return kernels.map((kernel: INotebookKernel2): INotebookKernel => {
return notebookKernelService.getKernels(model).map((kernel: INotebookKernel2): INotebookKernel => {
return {
id: kernel.id,
friendlyId: kernel.id,
......@@ -165,7 +167,34 @@ class KernelAdaptorBridge implements IWorkbenchContribution {
}
});
// kernel binding
const editorListener = new Map<string, IDisposable>();
disposables.add(notebookEditorService.onDidAddNotebookEditor(e => {
const r1 = e.onDidChangeKernel(() => {
if (!e.viewModel) {
return;
}
let kernel: INotebookKernel2 | undefined;
if (e.activeKernel) {
for (const candidate of kernels.keys()) {
if (e.activeKernel.friendlyId === candidate.id) {
kernel = candidate;
break;
}
}
}
notebookKernelService.updateNotebookKernelBinding(e.viewModel.notebookDocument, kernel);
});
editorListener.set(e.getId(), r1);
}));
disposables.add(notebookEditorService.onDidRemoveNotebookEditor(e => {
editorListener.get(e.getId())?.dispose();
editorListener.delete(e.getId());
}));
this.dispose = () => {
dispose(editorListener.values());
disposables.dispose();
emitter.dispose();
registration.dispose();
......
......@@ -66,5 +66,5 @@ export interface INotebookKernelService {
* Bind a notebook document to a kernel. A notebook is only bound to one kernel
* but a kernel can be bound to many notebooks (depending on its configuration)
*/
bindNotebookToKernel(notebook: INotebookTextModel, kernel: INotebookKernel2): void;
updateNotebookKernelBinding(notebook: INotebookTextModel, kernel: INotebookKernel2 | undefined): void;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册