@@ -12,14 +12,16 @@ import {
12
12
} from '../../common/application/types' ;
13
13
import { NotebookEditorSupport } from '../../common/experiments/groups' ;
14
14
import { traceError } from '../../common/logger' ;
15
- import { IDisposableRegistry , IExperimentsManager } from '../../common/types' ;
15
+ import { IDisposableRegistry , IExperimentsManager , IExtensionContext } from '../../common/types' ;
16
16
import { DataScience } from '../../common/utils/localize' ;
17
17
import { noop } from '../../common/utils/misc' ;
18
18
import { JupyterNotebookView } from './constants' ;
19
19
import { NotebookKernel } from './notebookKernel' ;
20
20
import { NotebookOutputRenderer } from './renderer' ;
21
21
import { INotebookContentProvider } from './types' ;
22
22
23
+ const EditorAssociationUpdatedKey = 'EditorAssociationUpdatedToUseNotebooks' ;
24
+
23
25
/**
24
26
* This class basically registers the necessary providers and the like with VSC.
25
27
* 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 {
36
38
@inject ( NotebookOutputRenderer ) private readonly renderer : NotebookOutputRenderer ,
37
39
@inject ( IApplicationEnvironment ) private readonly env : IApplicationEnvironment ,
38
40
@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
40
43
) { }
41
44
public async activate ( ) : Promise < void > {
42
45
// This condition is temporary.
43
46
// If user belongs to the experiment, then make the necessary changes to package.json.
44
47
// Once the API is final, we won't need to modify the package.json.
45
48
if ( this . experiment . inExperiment ( NotebookEditorSupport . nativeNotebookExperiment ) ) {
46
49
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 ( ) ;
47
54
}
48
55
if ( this . env . channel !== 'insiders' ) {
49
56
return ;
@@ -95,23 +102,55 @@ export class NotebookIntegration implements IExtensionSingleActivationService {
95
102
return ;
96
103
}
97
104
105
+ await this . enableDisableEditorAssociation ( true ) ;
106
+ }
107
+ private async enableDisableEditorAssociation ( enable : boolean ) {
98
108
// This code is temporary.
99
109
const settings = this . workspace . getConfiguration ( 'workbench' , undefined ) ;
100
110
const editorAssociations = settings . get ( 'editorAssociations' ) as {
101
111
viewType : string ;
102
112
filenamePattern : string ;
103
113
} [ ] ;
104
114
115
+ // Update the settings.
105
116
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 ) )
109
121
) {
110
122
editorAssociations . push ( {
111
123
viewType : 'jupyter-notebook' ,
112
124
filenamePattern : '*.ipynb'
113
125
} ) ;
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 ;
115
153
}
154
+ await this . enableDisableEditorAssociation ( false ) ;
116
155
}
117
156
}
0 commit comments