6
6
import { IAsyncDataSource } from 'vs/base/browser/ui/tree/tree' ;
7
7
import { CancellationToken } from 'vs/base/common/cancellation' ;
8
8
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel' ;
9
- import { INotebookKernelService , VariablesResult } from 'vs/workbench/contrib/notebook/common/notebookKernelService' ;
9
+ import { INotebookKernel , INotebookKernelService , VariablesResult } from 'vs/workbench/contrib/notebook/common/notebookKernelService' ;
10
10
11
11
export interface INotebookScope {
12
12
type : 'root' ;
13
- readonly notebook : NotebookTextModel | undefined ;
13
+ readonly notebook : NotebookTextModel ;
14
14
}
15
15
16
16
export interface INotebookVariableElement {
@@ -19,13 +19,13 @@ export interface INotebookVariableElement {
19
19
readonly name : string ;
20
20
readonly value : string ;
21
21
readonly indexedChildrenCount : number ;
22
+ readonly indexStart ?: number ;
22
23
readonly hasNamedChildren : boolean ;
24
+ readonly notebook : NotebookTextModel ;
23
25
}
24
26
25
27
export class NotebookVariableDataSource implements IAsyncDataSource < INotebookScope , INotebookVariableElement > {
26
28
27
- private notebook : NotebookTextModel | undefined = undefined ;
28
-
29
29
constructor ( private readonly notebookKernelService : INotebookKernelService ) { }
30
30
31
31
hasChildren ( element : INotebookScope | INotebookVariableElement ) : boolean {
@@ -34,33 +34,26 @@ export class NotebookVariableDataSource implements IAsyncDataSource<INotebookSco
34
34
35
35
async getChildren ( element : INotebookScope | INotebookVariableElement ) : Promise < Array < INotebookVariableElement > > {
36
36
if ( element . type === 'root' ) {
37
- this . notebook = element . notebook ;
38
- return this . getRootVariables ( ) ;
37
+ return this . getRootVariables ( element . notebook ) ;
39
38
} else {
40
39
return this . getVariables ( element ) ;
41
40
}
42
41
}
43
42
44
43
async getVariables ( parent : INotebookVariableElement ) : Promise < INotebookVariableElement [ ] > {
45
- if ( ! this . notebook ) {
46
- return [ ] ;
47
- }
48
- const selectedKernel = this . notebookKernelService . getMatchingKernel ( this . notebook ) . selected ;
44
+ const selectedKernel = this . notebookKernelService . getMatchingKernel ( parent . notebook ) . selected ;
49
45
if ( selectedKernel && selectedKernel . hasVariableProvider ) {
50
46
51
47
let children : INotebookVariableElement [ ] = [ ] ;
52
48
if ( parent . hasNamedChildren ) {
53
- const variables = selectedKernel . provideVariables ( this . notebook . uri , parent . id , 'named' , 0 , CancellationToken . None ) ;
49
+ const variables = selectedKernel . provideVariables ( parent . notebook . uri , parent . id , 'named' , 0 , CancellationToken . None ) ;
54
50
const childNodes = await variables
55
- . map ( variable => { return this . createVariableElement ( variable ) ; } )
51
+ . map ( variable => { return this . createVariableElement ( variable , parent . notebook ) ; } )
56
52
. toPromise ( ) ;
57
53
children = children . concat ( childNodes ) ;
58
54
}
59
55
if ( parent . indexedChildrenCount > 0 ) {
60
- const variables = selectedKernel . provideVariables ( this . notebook . uri , parent . id , 'indexed' , 0 , CancellationToken . None ) ;
61
- const childNodes = await variables
62
- . map ( variable => { return this . createVariableElement ( variable ) ; } )
63
- . toPromise ( ) ;
56
+ const childNodes = await this . getIndexedChildren ( parent , selectedKernel ) ;
64
57
children = children . concat ( childNodes ) ;
65
58
}
66
59
@@ -69,25 +62,58 @@ export class NotebookVariableDataSource implements IAsyncDataSource<INotebookSco
69
62
return [ ] ;
70
63
}
71
64
72
- async getRootVariables ( ) : Promise < INotebookVariableElement [ ] > {
73
- if ( ! this . notebook ) {
74
- return [ ] ;
65
+ async getIndexedChildren ( parent : INotebookVariableElement , kernel : INotebookKernel ) {
66
+ const childNodes : INotebookVariableElement [ ] = [ ] ;
67
+
68
+ if ( parent . indexedChildrenCount > 100 ) {
69
+ for ( let start = 0 ; start < parent . indexedChildrenCount ; start += 100 ) {
70
+ let end = start + 100 ;
71
+ if ( end > parent . indexedChildrenCount ) {
72
+ end = parent . indexedChildrenCount ;
73
+ }
74
+
75
+ childNodes . push ( {
76
+ type : 'variable' ,
77
+ notebook : parent . notebook ,
78
+ id : parent . id ,
79
+ name : `[${ start } ..${ end - 1 } ]` ,
80
+ value : '' ,
81
+ indexedChildrenCount : end - start ,
82
+ indexStart : start ,
83
+ hasNamedChildren : false
84
+ } ) ;
85
+ }
86
+ }
87
+ else if ( parent . indexedChildrenCount > 0 ) {
88
+ const variables = kernel . provideVariables ( parent . notebook . uri , parent . id , 'indexed' , parent . indexStart ?? 0 , CancellationToken . None ) ;
89
+
90
+ for await ( const variable of variables ) {
91
+ childNodes . push ( this . createVariableElement ( variable , parent . notebook ) ) ;
92
+ if ( childNodes . length >= 100 ) {
93
+ break ;
94
+ }
95
+ }
96
+
75
97
}
98
+ return childNodes ;
99
+ }
76
100
77
- const selectedKernel = this . notebookKernelService . getMatchingKernel ( this . notebook ) . selected ;
101
+ async getRootVariables ( notebook : NotebookTextModel ) : Promise < INotebookVariableElement [ ] > {
102
+ const selectedKernel = this . notebookKernelService . getMatchingKernel ( notebook ) . selected ;
78
103
if ( selectedKernel && selectedKernel . hasVariableProvider ) {
79
- const variables = selectedKernel . provideVariables ( this . notebook . uri , undefined , 'named' , 0 , CancellationToken . None ) ;
104
+ const variables = selectedKernel . provideVariables ( notebook . uri , undefined , 'named' , 0 , CancellationToken . None ) ;
80
105
return await variables
81
- . map ( variable => { return this . createVariableElement ( variable ) ; } )
106
+ . map ( variable => { return this . createVariableElement ( variable , notebook ) ; } )
82
107
. toPromise ( ) ;
83
108
}
84
109
85
110
return [ ] ;
86
111
}
87
112
88
- private createVariableElement ( variable : VariablesResult ) : INotebookVariableElement {
113
+ private createVariableElement ( variable : VariablesResult , notebook : NotebookTextModel ) : INotebookVariableElement {
89
114
return {
90
115
type : 'variable' ,
116
+ notebook,
91
117
...variable
92
118
} ;
93
119
}
0 commit comments