@@ -9,11 +9,12 @@ import { Emitter, Event } from 'vs/base/common/event';
99import { Disposable , DisposableStore , IDisposable } from 'vs/base/common/lifecycle' ;
1010import { ISplice } from 'vs/base/common/sequence' ;
1111import { URI , UriComponents } from 'vs/base/common/uri' ;
12+ import * as UUID from 'vs/base/common/uuid' ;
1213import { IExtensionDescription } from 'vs/platform/extensions/common/extensions' ;
1314import { CellKind , ExtHostNotebookShape , IMainContext , MainContext , MainThreadNotebookShape , NotebookCellOutputsSplice , MainThreadDocumentsShape , INotebookEditorPropertiesChangeData , INotebookDocumentsAndEditorsDelta } from 'vs/workbench/api/common/extHost.protocol' ;
1415import { ExtHostCommands } from 'vs/workbench/api/common/extHostCommands' ;
1516import { ExtHostDocumentsAndEditors } from 'vs/workbench/api/common/extHostDocumentsAndEditors' ;
16- import { CellEditType , CellUri , diff , ICellEditOperation , ICellInsertEdit , INotebookDisplayOrder , INotebookEditData , NotebookCellsChangedEvent , NotebookCellsSplice2 , ICellDeleteEdit , notebookDocumentMetadataDefaults , NotebookCellsChangeType , NotebookDataDto , IOutputRenderRequest , IOutputRenderResponse , IOutputRenderResponseOutputInfo , IOutputRenderResponseCellInfo , IRawOutput , CellOutputKind , IProcessedOutput } from 'vs/workbench/contrib/notebook/common/notebookCommon' ;
17+ import { CellEditType , CellUri , diff , ICellEditOperation , ICellInsertEdit , INotebookDisplayOrder , INotebookEditData , NotebookCellsChangedEvent , NotebookCellsSplice2 , ICellDeleteEdit , notebookDocumentMetadataDefaults , NotebookCellsChangeType , NotebookDataDto , IOutputRenderRequest , IOutputRenderResponse , IOutputRenderResponseOutputInfo , IOutputRenderResponseCellInfo , IRawOutput , CellOutputKind , IProcessedOutput , INotebookKernelInfoDto2 } from 'vs/workbench/contrib/notebook/common/notebookCommon' ;
1718import * as extHostTypes from 'vs/workbench/api/common/extHostTypes' ;
1819import { CancellationToken } from 'vs/base/common/cancellation' ;
1920import { ExtHostDocumentData } from 'vs/workbench/api/common/extHostDocumentData' ;
@@ -24,7 +25,6 @@ import { IExtensionStoragePaths } from 'vs/workbench/api/common/extHostStoragePa
2425import { joinPath } from 'vs/base/common/resources' ;
2526import { Schemas } from 'vs/base/common/network' ;
2627import { hash } from 'vs/base/common/hash' ;
27- import { generateUuid } from 'vs/base/common/uuid' ;
2828import { Cache } from './cache' ;
2929
3030interface IObservable < T > {
@@ -54,7 +54,7 @@ interface INotebookEventEmitter {
5454 emitCellLanguageChange ( event : vscode . NotebookCellLanguageChangeEvent ) : void ;
5555}
5656
57- const addIdToOutput = ( output : IRawOutput , id = generateUuid ( ) ) : IProcessedOutput => output . outputKind === CellOutputKind . Rich
57+ const addIdToOutput = ( output : IRawOutput , id = UUID . generateUuid ( ) ) : IProcessedOutput => output . outputKind === CellOutputKind . Rich
5858 ? ( { ...output , outputId : id } ) : output ;
5959
6060export class ExtHostCell extends Disposable implements vscode . NotebookCell {
@@ -780,10 +780,74 @@ export interface ExtHostNotebookOutputRenderingHandler {
780780 findBestMatchedRenderer ( mimeType : string ) : ExtHostNotebookOutputRenderer [ ] ;
781781}
782782
783+ export class ExtHostNotebookKernelProviderAdapter extends Disposable {
784+ private _kernelToId = new Map < vscode . NotebookKernel , string > ( ) ;
785+ private _idToKernel = new Map < string , vscode . NotebookKernel > ( ) ;
786+ constructor (
787+ private readonly _proxy : MainThreadNotebookShape ,
788+ private readonly _handle : number ,
789+ private readonly _extension : IExtensionDescription ,
790+ private readonly _provider : vscode . NotebookKernelProvider
791+ ) {
792+ super ( ) ;
793+
794+ if ( this . _provider . onDidChangeKernels ) {
795+ this . _register ( this . _provider . onDidChangeKernels ( ( ) => {
796+ this . _proxy . $onNotebookKernelChange ( this . _handle ) ;
797+ } ) ) ;
798+ }
799+ }
800+
801+ async provideKernels ( document : ExtHostNotebookDocument , token : vscode . CancellationToken ) : Promise < INotebookKernelInfoDto2 [ ] > {
802+ const data = await this . _provider . provideKernels ( document , token ) || [ ] ;
803+
804+ const newMap = new Map < vscode . NotebookKernel , string > ( ) ;
805+
806+ const transformedData : INotebookKernelInfoDto2 [ ] = data . map ( kernel => {
807+ let id = this . _kernelToId . get ( kernel ) ;
808+ if ( id === undefined ) {
809+ id = UUID . generateUuid ( ) ;
810+ this . _kernelToId . set ( kernel , id ) ;
811+ }
812+
813+ newMap . set ( kernel , id ) ;
814+
815+ return {
816+ id,
817+ label : kernel . label ,
818+ extension : this . _extension . identifier ,
819+ description : kernel . description ,
820+ isPreferred : kernel . isPreferred ,
821+ preloads : kernel . preloads
822+ } ;
823+ } ) ;
824+
825+ this . _kernelToId = newMap ;
826+
827+ this . _idToKernel . clear ( ) ;
828+ this . _kernelToId . forEach ( ( value , key ) => {
829+ this . _idToKernel . set ( value , key ) ;
830+ } ) ;
831+
832+ return transformedData ;
833+ }
834+
835+ async resolveNotebook ( kernelId : string , document : ExtHostNotebookDocument , webview : vscode . NotebookCommunication , token : CancellationToken ) {
836+ const kernel = this . _idToKernel . get ( kernelId ) ;
837+
838+ if ( kernel && this . _provider . resolveKernel ) {
839+ return this . _provider . resolveKernel ( kernel , document , webview , token ) ;
840+ }
841+ }
842+ }
843+
783844export class ExtHostNotebookController implements ExtHostNotebookShape , ExtHostNotebookOutputRenderingHandler {
845+ private static _notebookKernelProviderHandlePool : number = 0 ;
846+
784847 private readonly _proxy : MainThreadNotebookShape ;
785848 private readonly _notebookContentProviders = new Map < string , { readonly provider : vscode . NotebookContentProvider , readonly extension : IExtensionDescription ; } > ( ) ;
786849 private readonly _notebookKernels = new Map < string , { readonly kernel : vscode . NotebookKernel , readonly extension : IExtensionDescription ; } > ( ) ;
850+ private readonly _notebookKernelProviders = new Map < number , ExtHostNotebookKernelProviderAdapter > ( ) ;
787851 private readonly _documents = new Map < string , ExtHostNotebookDocument > ( ) ;
788852 private readonly _unInitializedDocuments = new Map < string , ExtHostNotebookDocument > ( ) ;
789853 private readonly _editors = new Map < string , { editor : ExtHostNotebookEditor } > ( ) ;
@@ -820,6 +884,10 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
820884 private _onDidCloseNotebookDocument = new Emitter < vscode . NotebookDocument > ( ) ;
821885 onDidCloseNotebookDocument : Event < vscode . NotebookDocument > = this . _onDidCloseNotebookDocument . event ;
822886 visibleNotebookEditors : ExtHostNotebookEditor [ ] = [ ] ;
887+ activeNotebookKernel ?: vscode . NotebookKernel ;
888+
889+ private _onDidChangeActiveNotebookKernel = new Emitter < void > ( ) ;
890+ onDidChangeActiveNotebookKernel = this . _onDidChangeActiveNotebookKernel . event ;
823891 private _onDidChangeVisibleNotebookEditors = new Emitter < vscode . NotebookEditor [ ] > ( ) ;
824892 onDidChangeVisibleNotebookEditors = this . _onDidChangeVisibleNotebookEditors . event ;
825893
@@ -998,6 +1066,51 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
9981066 } ) ;
9991067 }
10001068
1069+ registerNotebookKernelProvider ( extension : IExtensionDescription , selector : vscode . NotebookDocumentFilter , provider : vscode . NotebookKernelProvider ) {
1070+ const handle = ExtHostNotebookController . _notebookKernelProviderHandlePool ++ ;
1071+ const adapter = new ExtHostNotebookKernelProviderAdapter ( this . _proxy , handle , extension , provider ) ;
1072+ this . _notebookKernelProviders . set ( handle , adapter ) ;
1073+ this . _proxy . $registerNotebookKernelProvider ( { id : extension . identifier , location : extension . extensionLocation } , handle , selector ) ;
1074+
1075+ return new extHostTypes . Disposable ( ( ) => {
1076+ adapter . dispose ( ) ;
1077+ this . _notebookKernelProviders . delete ( handle ) ;
1078+ this . _proxy . $unregisterNotebookKernelProvider ( handle ) ;
1079+ } ) ;
1080+ }
1081+
1082+ private _withAdapter < T > ( handle : number , uri : UriComponents , callback : ( adapter : ExtHostNotebookKernelProviderAdapter , document : ExtHostNotebookDocument ) => Promise < T > ) {
1083+ const document = this . _documents . get ( URI . revive ( uri ) . toString ( ) ) ;
1084+
1085+ if ( ! document ) {
1086+ return [ ] ;
1087+ }
1088+
1089+ const provider = this . _notebookKernelProviders . get ( handle ) ;
1090+
1091+ if ( ! provider ) {
1092+ return [ ] ;
1093+ }
1094+
1095+ return callback ( provider , document ) ;
1096+ }
1097+
1098+ async $provideNotebookKernels ( handle : number , uri : UriComponents , token : CancellationToken ) : Promise < INotebookKernelInfoDto2 [ ] > {
1099+ return this . _withAdapter < INotebookKernelInfoDto2 [ ] > ( handle , uri , ( adapter , document ) => {
1100+ return adapter . provideKernels ( document , token ) ;
1101+ } ) ;
1102+ }
1103+
1104+ async $resolveNotebookKernel ( handle : number , editorId : string , uri : UriComponents , kernelId : string , token : CancellationToken ) : Promise < void > {
1105+ await this . _withAdapter < void > ( handle , uri , async ( adapter , document ) => {
1106+ let webComm = this . _webviewComm . get ( editorId ) ;
1107+
1108+ if ( webComm ) {
1109+ await adapter . resolveNotebook ( kernelId , document , webComm . contentProviderComm , token ) ;
1110+ }
1111+ } ) ;
1112+ }
1113+
10011114 registerNotebookKernel ( extension : IExtensionDescription , id : string , selectors : vscode . GlobPattern [ ] , kernel : vscode . NotebookKernel ) : vscode . Disposable {
10021115 if ( this . _notebookKernels . has ( id ) ) {
10031116 throw new Error ( `Notebook kernel for '${ id } ' already registered` ) ;
0 commit comments