@@ -9,40 +9,75 @@ import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
99import { Registry } from 'vs/platform/registry/common/platform' ;
1010import { Extensions , IViewContainersRegistry , IViewsRegistry } from 'vs/workbench/common/views' ;
1111import { VIEWLET_ID as debugContainerId } from 'vs/workbench/contrib/debug/common/debug' ;
12- import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey' ;
1312import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors' ;
1413import { NotebookVariablesView } from 'vs/workbench/contrib/notebook/browser/contrib/notebookVariables/notebookVariablesView' ;
15- import { NOTEBOOK_KERNEL } from 'vs/workbench/contrib/notebook/common/notebookContextKeys' ;
1614import { variablesViewIcon } from 'vs/workbench/contrib/notebook/browser/notebookIcons' ;
1715import { IEditorService } from 'vs/workbench/services/editor/common/editorService' ;
18- import { IConfigurationService } from 'vs/platform/configuration/common/configuration' ;
16+ import { IConfigurationChangeEvent , IConfigurationService } from 'vs/platform/configuration/common/configuration' ;
1917import { INotebookExecutionStateService } from 'vs/workbench/contrib/notebook/common/notebookExecutionStateService' ;
2018import { NotebookSetting } from 'vs/workbench/contrib/notebook/common/notebookCommon' ;
19+ import { getNotebookEditorFromEditorPane } from 'vs/workbench/contrib/notebook/browser/notebookBrowser' ;
20+ import { INotebookKernelService } from 'vs/workbench/contrib/notebook/common/notebookKernelService' ;
21+ import { IContextKey , IContextKeyService } from 'vs/platform/contextkey/common/contextkey' ;
22+ import { NOTEBOOK_VARIABLE_VIEW_ENABLED } from 'vs/workbench/contrib/notebook/browser/contrib/notebookVariables/notebookVariableContextKeys' ;
2123
2224
2325export class NotebookVariables extends Disposable implements IWorkbenchContribution {
2426 private listeners : IDisposable [ ] = [ ] ;
27+ private configListener : IDisposable ;
28+ private initialized = false ;
29+
30+ private viewEnabled : IContextKey < boolean > ;
2531
2632 constructor (
33+ @IContextKeyService contextKeyService : IContextKeyService ,
34+ @IConfigurationService private readonly configurationService : IConfigurationService ,
2735 @IEditorService private readonly editorService : IEditorService ,
28- @IConfigurationService configurationService : IConfigurationService ,
29- @INotebookExecutionStateService private readonly notebookExecutionStateService : INotebookExecutionStateService
36+ @INotebookExecutionStateService private readonly notebookExecutionStateService : INotebookExecutionStateService ,
37+ @INotebookKernelService private readonly notebookKernelService : INotebookKernelService
3038 ) {
3139 super ( ) ;
3240
33- this . listeners . push ( this . editorService . onDidEditorsChange ( ( ) => this . handleInitEvent ( configurationService ) ) ) ;
34- this . listeners . push ( this . notebookExecutionStateService . onDidChangeExecution ( ( ) => this . handleInitEvent ( configurationService ) ) ) ;
41+ this . viewEnabled = NOTEBOOK_VARIABLE_VIEW_ENABLED . bindTo ( contextKeyService ) ;
42+
43+ this . listeners . push ( this . editorService . onDidEditorsChange ( ( ) => this . handleInitEvent ( ) ) ) ;
44+ this . listeners . push ( this . notebookExecutionStateService . onDidChangeExecution ( ( ) => this . handleInitEvent ( ) ) ) ;
45+
46+ this . configListener = configurationService . onDidChangeConfiguration ( ( e ) => this . handleConfigChange ( e ) ) ;
47+ }
48+
49+ private handleConfigChange ( e : IConfigurationChangeEvent ) {
50+ if ( e . affectsConfiguration ( NotebookSetting . notebookVariablesView ) ) {
51+ if ( ! this . configurationService . getValue ( NotebookSetting . notebookVariablesView ) ) {
52+ this . viewEnabled . set ( false ) ;
53+ } else if ( this . initialized ) {
54+ this . viewEnabled . set ( true ) ;
55+ } else {
56+ this . handleInitEvent ( ) ;
57+ }
58+ }
3559 }
3660
37- private handleInitEvent ( configurationService : IConfigurationService ) {
38- if ( configurationService . getValue ( NotebookSetting . notebookVariablesView )
61+ private handleInitEvent ( ) {
62+ if ( this . configurationService . getValue ( NotebookSetting . notebookVariablesView )
3963 && this . editorService . activeEditorPane ?. getId ( ) === 'workbench.editor.notebook' ) {
40- if ( this . initializeView ( ) ) {
64+
65+ if ( this . hasVariableProvider ( ) ) {
66+ this . viewEnabled . set ( true ) ;
67+ }
68+
69+ if ( ! this . initialized && this . initializeView ( ) ) {
70+ this . initialized = true ;
4171 this . listeners . forEach ( listener => listener . dispose ( ) ) ;
4272 }
4373 }
4474 }
4575
76+ private hasVariableProvider ( ) {
77+ const notebookDocument = getNotebookEditorFromEditorPane ( this . editorService . activeEditorPane ) ?. getViewModel ( ) ?. notebookDocument ;
78+ return notebookDocument && this . notebookKernelService . getMatchingKernel ( notebookDocument ) . selected ?. hasVariableProvider ;
79+ }
80+
4681 private initializeView ( ) {
4782 const debugViewContainer = Registry . as < IViewContainersRegistry > ( 'workbench.registry.view.containers' ) . get ( debugContainerId ) ;
4883
@@ -51,7 +86,7 @@ export class NotebookVariables extends Disposable implements IWorkbenchContribut
5186 const viewDescriptor = {
5287 id : 'NOTEBOOK_VARIABLES' , name : nls . localize2 ( 'notebookVariables' , "Notebook Variables" ) ,
5388 containerIcon : variablesViewIcon , ctorDescriptor : new SyncDescriptor ( NotebookVariablesView ) ,
54- order : 50 , weight : 5 , canToggleVisibility : true , canMoveView : true , collapsed : true , when : ContextKeyExpr . notEquals ( NOTEBOOK_KERNEL . key , '' ) ,
89+ order : 50 , weight : 5 , canToggleVisibility : true , canMoveView : true , collapsed : true , when : NOTEBOOK_VARIABLE_VIEW_ENABLED ,
5590 } ;
5691
5792 viewsRegistry . registerViews ( [ viewDescriptor ] , debugViewContainer ) ;
@@ -61,4 +96,10 @@ export class NotebookVariables extends Disposable implements IWorkbenchContribut
6196 return false ;
6297 }
6398
99+ override dispose ( ) : void {
100+ super . dispose ( ) ;
101+ this . listeners . forEach ( listener => listener . dispose ( ) ) ;
102+ this . configListener . dispose ( ) ;
103+ }
104+
64105}
0 commit comments