66import { IAsyncDataSource } from 'vs/base/browser/ui/tree/tree' ;
77import { CancellationToken } from 'vs/base/common/cancellation' ;
88import { 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' ;
1010
1111export interface INotebookScope {
1212 type : 'root' ;
13- readonly notebook : NotebookTextModel | undefined ;
13+ readonly notebook : NotebookTextModel ;
1414}
1515
1616export interface INotebookVariableElement {
@@ -19,13 +19,13 @@ export interface INotebookVariableElement {
1919 readonly name : string ;
2020 readonly value : string ;
2121 readonly indexedChildrenCount : number ;
22+ readonly indexStart ?: number ;
2223 readonly hasNamedChildren : boolean ;
24+ readonly notebook : NotebookTextModel ;
2325}
2426
2527export class NotebookVariableDataSource implements IAsyncDataSource < INotebookScope , INotebookVariableElement > {
2628
27- private notebook : NotebookTextModel | undefined = undefined ;
28-
2929 constructor ( private readonly notebookKernelService : INotebookKernelService ) { }
3030
3131 hasChildren ( element : INotebookScope | INotebookVariableElement ) : boolean {
@@ -34,33 +34,26 @@ export class NotebookVariableDataSource implements IAsyncDataSource<INotebookSco
3434
3535 async getChildren ( element : INotebookScope | INotebookVariableElement ) : Promise < Array < INotebookVariableElement > > {
3636 if ( element . type === 'root' ) {
37- this . notebook = element . notebook ;
38- return this . getRootVariables ( ) ;
37+ return this . getRootVariables ( element . notebook ) ;
3938 } else {
4039 return this . getVariables ( element ) ;
4140 }
4241 }
4342
4443 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 ;
4945 if ( selectedKernel && selectedKernel . hasVariableProvider ) {
5046
5147 let children : INotebookVariableElement [ ] = [ ] ;
5248 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 ) ;
5450 const childNodes = await variables
55- . map ( variable => { return this . createVariableElement ( variable ) ; } )
51+ . map ( variable => { return this . createVariableElement ( variable , parent . notebook ) ; } )
5652 . toPromise ( ) ;
5753 children = children . concat ( childNodes ) ;
5854 }
5955 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 ) ;
6457 children = children . concat ( childNodes ) ;
6558 }
6659
@@ -69,25 +62,58 @@ export class NotebookVariableDataSource implements IAsyncDataSource<INotebookSco
6962 return [ ] ;
7063 }
7164
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+
7597 }
98+ return childNodes ;
99+ }
76100
77- const selectedKernel = this . notebookKernelService . getMatchingKernel ( this . notebook ) . selected ;
101+ async getRootVariables ( notebook : NotebookTextModel ) : Promise < INotebookVariableElement [ ] > {
102+ const selectedKernel = this . notebookKernelService . getMatchingKernel ( notebook ) . selected ;
78103 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 ) ;
80105 return await variables
81- . map ( variable => { return this . createVariableElement ( variable ) ; } )
106+ . map ( variable => { return this . createVariableElement ( variable , notebook ) ; } )
82107 . toPromise ( ) ;
83108 }
84109
85110 return [ ] ;
86111 }
87112
88- private createVariableElement ( variable : VariablesResult ) : INotebookVariableElement {
113+ private createVariableElement ( variable : VariablesResult , notebook : NotebookTextModel ) : INotebookVariableElement {
89114 return {
90115 type : 'variable' ,
116+ notebook,
91117 ...variable
92118 } ;
93119 }
0 commit comments