Skip to content

Commit 8fa8458

Browse files
authored
show variable view only when applicable (#204789)
* only initialize view if there is a variable provider available * use context key to show/hide the view
1 parent 820bfcb commit 8fa8458

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { RawContextKey } from 'vs/platform/contextkey/common/contextkey';
7+
8+
export const NOTEBOOK_VARIABLE_VIEW_ENABLED = new RawContextKey<boolean>('notebookVariableViewEnabled', false);

src/vs/workbench/contrib/notebook/browser/contrib/notebookVariables/notebookVariables.ts

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,75 @@ import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
99
import { Registry } from 'vs/platform/registry/common/platform';
1010
import { Extensions, IViewContainersRegistry, IViewsRegistry } from 'vs/workbench/common/views';
1111
import { VIEWLET_ID as debugContainerId } from 'vs/workbench/contrib/debug/common/debug';
12-
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
1312
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
1413
import { NotebookVariablesView } from 'vs/workbench/contrib/notebook/browser/contrib/notebookVariables/notebookVariablesView';
15-
import { NOTEBOOK_KERNEL } from 'vs/workbench/contrib/notebook/common/notebookContextKeys';
1614
import { variablesViewIcon } from 'vs/workbench/contrib/notebook/browser/notebookIcons';
1715
import { 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';
1917
import { INotebookExecutionStateService } from 'vs/workbench/contrib/notebook/common/notebookExecutionStateService';
2018
import { 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

2325
export 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
}

src/vs/workbench/contrib/notebook/browser/contrib/notebookVariables/notebookVariablesDataSource.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class NotebookVariableDataSource implements IAsyncDataSource<INotebookSco
5353
}
5454
}
5555

56-
async getVariables(parent: INotebookVariableElement): Promise<INotebookVariableElement[]> {
56+
private async getVariables(parent: INotebookVariableElement): Promise<INotebookVariableElement[]> {
5757
const selectedKernel = this.notebookKernelService.getMatchingKernel(parent.notebook).selected;
5858
if (selectedKernel && selectedKernel.hasVariableProvider) {
5959

@@ -75,7 +75,7 @@ export class NotebookVariableDataSource implements IAsyncDataSource<INotebookSco
7575
return [];
7676
}
7777

78-
async getIndexedChildren(parent: INotebookVariableElement, kernel: INotebookKernel) {
78+
private async getIndexedChildren(parent: INotebookVariableElement, kernel: INotebookKernel) {
7979
const childNodes: INotebookVariableElement[] = [];
8080

8181
if (parent.indexedChildrenCount > variablePageSize) {
@@ -128,7 +128,7 @@ export class NotebookVariableDataSource implements IAsyncDataSource<INotebookSco
128128
return childNodes;
129129
}
130130

131-
async getRootVariables(notebook: NotebookTextModel): Promise<INotebookVariableElement[]> {
131+
private async getRootVariables(notebook: NotebookTextModel): Promise<INotebookVariableElement[]> {
132132
const selectedKernel = this.notebookKernelService.getMatchingKernel(notebook).selected;
133133
if (selectedKernel && selectedKernel.hasVariableProvider) {
134134
const variables = selectedKernel.provideVariables(notebook.uri, undefined, 'named', 0, this.cancellationTokenSource.token);

0 commit comments

Comments
 (0)