Skip to content

Commit 4355ac2

Browse files
authored
Revert settings changes when opting out of Native NB Experiment (#12852)
For #10496
1 parent 2ce96b0 commit 4355ac2

File tree

1 file changed

+45
-6
lines changed

1 file changed

+45
-6
lines changed

src/client/datascience/notebook/integration.ts

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ import {
1212
} from '../../common/application/types';
1313
import { NotebookEditorSupport } from '../../common/experiments/groups';
1414
import { traceError } from '../../common/logger';
15-
import { IDisposableRegistry, IExperimentsManager } from '../../common/types';
15+
import { IDisposableRegistry, IExperimentsManager, IExtensionContext } from '../../common/types';
1616
import { DataScience } from '../../common/utils/localize';
1717
import { noop } from '../../common/utils/misc';
1818
import { JupyterNotebookView } from './constants';
1919
import { NotebookKernel } from './notebookKernel';
2020
import { NotebookOutputRenderer } from './renderer';
2121
import { INotebookContentProvider } from './types';
2222

23+
const EditorAssociationUpdatedKey = 'EditorAssociationUpdatedToUseNotebooks';
24+
2325
/**
2426
* This class basically registers the necessary providers and the like with VSC.
2527
* I.e. this is where we integrate our stuff with VS Code via their extension endpoints.
@@ -36,14 +38,19 @@ export class NotebookIntegration implements IExtensionSingleActivationService {
3638
@inject(NotebookOutputRenderer) private readonly renderer: NotebookOutputRenderer,
3739
@inject(IApplicationEnvironment) private readonly env: IApplicationEnvironment,
3840
@inject(IApplicationShell) private readonly shell: IApplicationShell,
39-
@inject(IWorkspaceService) private readonly workspace: IWorkspaceService
41+
@inject(IWorkspaceService) private readonly workspace: IWorkspaceService,
42+
@inject(IExtensionContext) private readonly extensionContext: IExtensionContext
4043
) {}
4144
public async activate(): Promise<void> {
4245
// This condition is temporary.
4346
// If user belongs to the experiment, then make the necessary changes to package.json.
4447
// Once the API is final, we won't need to modify the package.json.
4548
if (this.experiment.inExperiment(NotebookEditorSupport.nativeNotebookExperiment)) {
4649
await this.enableNotebooks();
50+
} else {
51+
// Possible user was in experiment, then they opted out. In this case we need to revert the changes made to the settings file.
52+
// Again, this is temporary code.
53+
await this.disableNotebooks();
4754
}
4855
if (this.env.channel !== 'insiders') {
4956
return;
@@ -95,23 +102,55 @@ export class NotebookIntegration implements IExtensionSingleActivationService {
95102
return;
96103
}
97104

105+
await this.enableDisableEditorAssociation(true);
106+
}
107+
private async enableDisableEditorAssociation(enable: boolean) {
98108
// This code is temporary.
99109
const settings = this.workspace.getConfiguration('workbench', undefined);
100110
const editorAssociations = settings.get('editorAssociations') as {
101111
viewType: string;
102112
filenamePattern: string;
103113
}[];
104114

115+
// Update the settings.
105116
if (
106-
!Array.isArray(editorAssociations) ||
107-
editorAssociations.length === 0 ||
108-
!editorAssociations.find((item) => item.viewType === JupyterNotebookView)
117+
enable &&
118+
(!Array.isArray(editorAssociations) ||
119+
editorAssociations.length === 0 ||
120+
!editorAssociations.find((item) => item.viewType === JupyterNotebookView))
109121
) {
110122
editorAssociations.push({
111123
viewType: 'jupyter-notebook',
112124
filenamePattern: '*.ipynb'
113125
});
114-
await settings.update('editorAssociations', editorAssociations, ConfigurationTarget.Global);
126+
await Promise.all([
127+
this.extensionContext.globalState.update(EditorAssociationUpdatedKey, true),
128+
settings.update('editorAssociations', editorAssociations, ConfigurationTarget.Global)
129+
]);
130+
}
131+
132+
// Revert the settings.
133+
if (
134+
!enable &&
135+
this.extensionContext.globalState.get<boolean>(EditorAssociationUpdatedKey, false) &&
136+
Array.isArray(editorAssociations) &&
137+
editorAssociations.find((item) => item.viewType === JupyterNotebookView)
138+
) {
139+
const updatedSettings = editorAssociations.filter((item) => item.viewType !== JupyterNotebookView);
140+
await Promise.all([
141+
this.extensionContext.globalState.update(EditorAssociationUpdatedKey, false),
142+
settings.update('editorAssociations', updatedSettings, ConfigurationTarget.Global)
143+
]);
144+
}
145+
}
146+
private async disableNotebooks() {
147+
if (this.env.channel === 'stable') {
148+
return;
149+
}
150+
// If we never modified the settings, then nothing to do.
151+
if (!this.extensionContext.globalState.get<boolean>(EditorAssociationUpdatedKey, false)) {
152+
return;
115153
}
154+
await this.enableDisableEditorAssociation(false);
116155
}
117156
}

0 commit comments

Comments
 (0)