forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshiftEnterBanner.ts
More file actions
133 lines (116 loc) · 4.56 KB
/
shiftEnterBanner.ts
File metadata and controls
133 lines (116 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import { ConfigurationTarget } from 'vscode';
import { IApplicationShell } from '../common/application/types';
import '../common/extensions';
import { IConfigurationService, IPersistentStateFactory, IPythonExtensionBanner } from '../common/types';
import * as localize from '../common/utils/localize';
import { captureTelemetry, sendTelemetryEvent } from '../telemetry';
import { Telemetry } from './constants';
import { IJupyterExecution } from './types';
export enum InteractiveShiftEnterStateKeys {
ShowBanner = 'InteractiveShiftEnterBanner'
}
enum InteractiveShiftEnterLabelIndex {
Yes,
No
}
// Create a banner to ask users if they want to send shift-enter to the interactive window or not
@injectable()
export class InteractiveShiftEnterBanner implements IPythonExtensionBanner {
private initialized?: boolean;
private disabledInCurrentSession: boolean = false;
private bannerMessage: string = localize.InteractiveShiftEnterBanner.bannerMessage();
private bannerLabels: string[] = [localize.Common.bannerLabelYes(), localize.Common.bannerLabelNo()];
constructor(
@inject(IApplicationShell) private appShell: IApplicationShell,
@inject(IPersistentStateFactory) private persistentState: IPersistentStateFactory,
@inject(IJupyterExecution) private jupyterExecution: IJupyterExecution,
@inject(IConfigurationService) private configuration: IConfigurationService
) {
this.initialize();
}
public initialize() {
if (this.initialized) {
return;
}
this.initialized = true;
if (!this.enabled) {
return;
}
}
public get enabled(): boolean {
return this.persistentState.createGlobalPersistentState<boolean>(
InteractiveShiftEnterStateKeys.ShowBanner,
true
).value;
}
public async showBanner(): Promise<void> {
if (!this.enabled) {
return;
}
const show = await this.shouldShowBanner();
if (!show) {
return;
}
// This check is independent from shouldShowBanner, that just checks the persistent state.
// The Jupyter check should only happen once and should disable the banner if it fails (don't reprompt and don't recheck)
const jupyterFound = await this.jupyterExecution.isNotebookSupported();
if (!jupyterFound) {
await this.disableBanner();
return;
}
sendTelemetryEvent(Telemetry.ShiftEnterBannerShown);
const response = await this.appShell.showInformationMessage(this.bannerMessage, ...this.bannerLabels);
switch (response) {
case this.bannerLabels[InteractiveShiftEnterLabelIndex.Yes]: {
await this.enableInteractiveShiftEnter();
break;
}
case this.bannerLabels[InteractiveShiftEnterLabelIndex.No]: {
await this.disableInteractiveShiftEnter();
break;
}
default: {
// Disable for the current session.
this.disabledInCurrentSession = true;
}
}
}
public async shouldShowBanner(): Promise<boolean> {
const settings = this.configuration.getSettings();
return Promise.resolve(
this.enabled &&
!this.disabledInCurrentSession &&
!settings.datascience.sendSelectionToInteractiveWindow &&
settings.datascience.enabled
);
}
@captureTelemetry(Telemetry.DisableInteractiveShiftEnter)
public async disableInteractiveShiftEnter(): Promise<void> {
await this.configuration.updateSetting(
'dataScience.sendSelectionToInteractiveWindow',
false,
undefined,
ConfigurationTarget.Global
);
await this.disableBanner();
}
@captureTelemetry(Telemetry.EnableInteractiveShiftEnter)
public async enableInteractiveShiftEnter(): Promise<void> {
await this.configuration.updateSetting(
'dataScience.sendSelectionToInteractiveWindow',
true,
undefined,
ConfigurationTarget.Global
);
await this.disableBanner();
}
private async disableBanner(): Promise<void> {
await this.persistentState
.createGlobalPersistentState<boolean>(InteractiveShiftEnterStateKeys.ShowBanner, false)
.updateValue(false);
}
}