18
18
* Licensed under the MIT License. See License.txt in the project root for license information.
19
19
*--------------------------------------------------------------------------------------------*/
20
20
21
- import { CancellationToken } from '@theia/plugin' ;
22
21
import {
23
- CellExecuteUpdateDto , NotebookKernelDto , NotebookKernelsExt , NotebookKernelsMain , NotebookKernelSourceActionDto , NotebookOutputDto , PLUGIN_RPC_CONTEXT
22
+ CellExecuteUpdateDto , NotebookKernelDto , NotebookKernelsExt , NotebookKernelsMain ,
23
+ NotebookKernelSourceActionDto , NotebookOutputDto , PluginModel , PluginPackage , PLUGIN_RPC_CONTEXT
24
24
} from '../../common' ;
25
25
import { RPCProtocol } from '../../common/rpc-protocol' ;
26
26
import { UriComponents } from '../../common/uri-components' ;
27
- import * as theia from '@theia/plugin' ;
28
- import { CancellationTokenSource , Disposable , DisposableCollection , Emitter } from '@theia/core' ;
27
+ import { CancellationTokenSource , Disposable , DisposableCollection , Emitter , Path } from '@theia/core' ;
29
28
import { Cell } from './notebook-document' ;
30
29
import { NotebooksExtImpl } from './notebooks' ;
31
30
import { NotebookCellOutputConverter , NotebookCellOutputItem , NotebookKernelSourceAction } from '../type-converters' ;
32
31
import { timeout , Deferred } from '@theia/core/lib/common/promise-util' ;
33
32
import { CellExecutionUpdateType , NotebookCellExecutionState } from '@theia/notebook/lib/common' ;
34
33
import { CommandRegistryImpl } from '../command-registry' ;
35
34
import { NotebookCellOutput , NotebookRendererScript , URI } from '../types-impl' ;
35
+ import { toUriComponents } from '../../main/browser/hierarchy/hierarchy-types-converters' ;
36
+ import type * as theia from '@theia/plugin' ;
36
37
37
38
interface KernelData {
38
39
extensionId : string ;
@@ -62,28 +63,28 @@ export class NotebookKernelsExtImpl implements NotebookKernelsExt {
62
63
constructor (
63
64
rpc : RPCProtocol ,
64
65
private readonly notebooks : NotebooksExtImpl ,
65
- private readonly commands : CommandRegistryImpl
66
+ private readonly commands : CommandRegistryImpl ,
66
67
) {
67
68
this . proxy = rpc . getProxy ( PLUGIN_RPC_CONTEXT . NOTEBOOK_KERNELS_MAIN ) ;
68
69
}
69
70
70
71
private currentHandle = 0 ;
71
72
72
- createNotebookController ( extensionId : string , id : string , viewType : string , label : string , handler ?: ( cells : theia . NotebookCell [ ] ,
73
+ createNotebookController ( extension : PluginModel , id : string , viewType : string , label : string , handler ?: ( cells : theia . NotebookCell [ ] ,
73
74
notebook : theia . NotebookDocument , controller : theia . NotebookController ) => void | Thenable < void > , rendererScripts ?: NotebookRendererScript [ ] ) : theia . NotebookController {
74
75
75
76
for ( const kernelData of this . kernelData . values ( ) ) {
76
- if ( kernelData . controller . id === id && extensionId === kernelData . extensionId ) {
77
+ if ( kernelData . controller . id === id && extension . id === kernelData . extensionId ) {
77
78
throw new Error ( `notebook controller with id '${ id } ' ALREADY exist` ) ;
78
79
}
79
80
}
80
81
81
82
const handle = this . currentHandle ++ ;
82
83
const that = this ;
83
84
84
- console . debug ( `NotebookController[${ handle } ], CREATED by ${ extensionId } , ${ id } ` ) ;
85
+ console . debug ( `NotebookController[${ handle } ], CREATED by ${ extension . id } , ${ id } ` ) ;
85
86
86
- const defaultExecuteHandler = ( ) => console . warn ( `NO execute handler from notebook controller '${ data . id } ' of extension: '${ extensionId } '` ) ;
87
+ const defaultExecuteHandler = ( ) => console . warn ( `NO execute handler from notebook controller '${ data . id } ' of extension: '${ extension . id } '` ) ;
87
88
88
89
let isDisposed = false ;
89
90
const commandDisposables = new DisposableCollection ( ) ;
@@ -92,10 +93,12 @@ export class NotebookKernelsExtImpl implements NotebookKernelsExt {
92
93
const onDidReceiveMessage = new Emitter < { editor : theia . NotebookEditor ; message : unknown } > ( ) ;
93
94
94
95
const data : NotebookKernelDto = {
95
- id : createKernelId ( extensionId , id ) ,
96
+ id : createKernelId ( extension . id , id ) ,
96
97
notebookType : viewType ,
97
- extensionId : extensionId ,
98
- label : label || extensionId ,
98
+ extensionId : extension . id ,
99
+ extensionLocation : toUriComponents ( extension . packageUri ) ,
100
+ label : label || extension . id ,
101
+ preloads : rendererScripts ?. map ( preload => ( { uri : toUriComponents ( preload . uri . toString ( ) ) , provides : preload . provides } ) ) ?? [ ]
99
102
} ;
100
103
101
104
//
@@ -131,12 +134,11 @@ export class NotebookKernelsExtImpl implements NotebookKernelsExt {
131
134
get id ( ) : string { return id ; } ,
132
135
get notebookType ( ) : string { return data . notebookType ; } ,
133
136
onDidChangeSelectedNotebooks : onDidChangeSelection . event ,
134
- onDidReceiveMessage : onDidReceiveMessage . event ,
135
137
get label ( ) : string {
136
138
return data . label ;
137
139
} ,
138
140
set label ( value ) {
139
- data . label = value ?? extensionId ;
141
+ data . label = value ?? extension . id ;
140
142
update ( ) ;
141
143
} ,
142
144
get detail ( ) : string {
@@ -168,11 +170,7 @@ export class NotebookKernelsExtImpl implements NotebookKernelsExt {
168
170
update ( ) ;
169
171
} ,
170
172
get rendererScripts ( ) : NotebookRendererScript [ ] {
171
- return data . rendererScripts ?? [ ] ;
172
- } ,
173
- set rendererScripts ( value ) {
174
- data . rendererScripts = value ;
175
- update ( ) ;
173
+ return data . preloads ?. map ( preload => ( new NotebookRendererScript ( URI . from ( preload . uri ) , preload . provides ) ) ) ?? [ ] ;
176
174
} ,
177
175
get executeHandler ( ) : ( cells : theia . NotebookCell [ ] , notebook : theia . NotebookDocument , controller : theia . NotebookController ) => void | Thenable < void > {
178
176
return executeHandler ;
@@ -197,7 +195,7 @@ export class NotebookKernelsExtImpl implements NotebookKernelsExt {
197
195
Array . from ( associatedNotebooks . keys ( ) ) . map ( u => u . toString ( ) ) ) ;
198
196
throw new Error ( `notebook controller is NOT associated to notebook: ${ cell . notebook . uri . toString ( ) } ` ) ;
199
197
}
200
- return that . createNotebookCellExecution ( cell , createKernelId ( extensionId , this . id ) ) ;
198
+ return that . createNotebookCellExecution ( cell , createKernelId ( extension . id , this . id ) ) ;
201
199
} ,
202
200
dispose : ( ) => {
203
201
if ( ! isDisposed ) {
@@ -213,16 +211,18 @@ export class NotebookKernelsExtImpl implements NotebookKernelsExt {
213
211
updateNotebookAffinity ( notebook , priority ) : void {
214
212
that . proxy . $updateNotebookPriority ( handle , notebook . uri , priority ) ;
215
213
} ,
214
+ onDidReceiveMessage : onDidReceiveMessage . event ,
216
215
async postMessage ( message : unknown , editor ?: theia . NotebookEditor ) : Promise < boolean > {
217
- return Promise . resolve ( true ) ; // TODO needs implementation
216
+ return that . proxy . $postMessage ( handle , 'notebook:' + editor ?. notebook . uri . toString ( ) , message ) ;
218
217
} ,
219
218
asWebviewUri ( localResource : theia . Uri ) : theia . Uri {
220
- throw new Error ( 'Method not implemented.' ) ;
219
+ const basePath = PluginPackage . toPluginUrl ( extension , '' ) ;
220
+ return URI . from ( { path : new Path ( basePath ) . join ( localResource . path ) . toString ( ) , scheme : 'https' } ) ;
221
221
}
222
222
} ;
223
223
224
224
this . kernelData . set ( handle , {
225
- extensionId : extensionId ,
225
+ extensionId : extension . id ,
226
226
controller,
227
227
onDidReceiveMessage,
228
228
onDidChangeSelection,
@@ -376,7 +376,7 @@ export class NotebookKernelsExtImpl implements NotebookKernelsExt {
376
376
// Proposed Api though seems needed by jupyter for telemetry
377
377
}
378
378
379
- async $provideKernelSourceActions ( handle : number , token : CancellationToken ) : Promise < NotebookKernelSourceActionDto [ ] > {
379
+ async $provideKernelSourceActions ( handle : number , token : theia . CancellationToken ) : Promise < NotebookKernelSourceActionDto [ ] > {
380
380
const provider = this . kernelSourceActionProviders . get ( handle ) ;
381
381
if ( provider ) {
382
382
const disposables = new DisposableCollection ( ) ;
@@ -496,7 +496,7 @@ class NotebookCellExecutionTask implements Disposable {
496
496
asApiObject ( ) : theia . NotebookCellExecution {
497
497
const that = this ;
498
498
const result : theia . NotebookCellExecution = {
499
- get token ( ) : CancellationToken { return that . tokenSource . token ; } ,
499
+ get token ( ) : theia . CancellationToken { return that . tokenSource . token ; } ,
500
500
get cell ( ) : theia . NotebookCell { return that . cell . apiCell ; } ,
501
501
get executionOrder ( ) : number | undefined { return that . executionOrder ; } ,
502
502
set executionOrder ( v : number | undefined ) {
0 commit comments