2
2
// Licensed under the MIT License.
3
3
'use strict' ;
4
4
import { inject , injectable } from 'inversify' ;
5
- import * as path from 'path ' ;
5
+ import { ConfigurationTarget } from 'vscode ' ;
6
6
import { IExtensionSingleActivationService } from '../../activation/types' ;
7
7
import {
8
8
IApplicationEnvironment ,
9
9
IApplicationShell ,
10
- ICommandManager ,
11
- IVSCodeNotebook
10
+ IVSCodeNotebook ,
11
+ IWorkspaceService
12
12
} from '../../common/application/types' ;
13
13
import { NotebookEditorSupport } from '../../common/experiments/groups' ;
14
14
import { traceError } from '../../common/logger' ;
15
- import { IFileSystem } from '../../common/platform/types' ;
16
- import { IDisposableRegistry , IExperimentsManager , IExtensionContext } from '../../common/types' ;
15
+ import { IDisposableRegistry , IExperimentsManager } from '../../common/types' ;
17
16
import { DataScience } from '../../common/utils/localize' ;
18
17
import { noop } from '../../common/utils/misc' ;
19
18
import { JupyterNotebookView } from './constants' ;
@@ -33,36 +32,18 @@ export class NotebookIntegration implements IExtensionSingleActivationService {
33
32
@inject ( IExperimentsManager ) private readonly experiment : IExperimentsManager ,
34
33
@inject ( IDisposableRegistry ) private readonly disposables : IDisposableRegistry ,
35
34
@inject ( INotebookContentProvider ) private readonly notebookContentProvider : INotebookContentProvider ,
36
- @inject ( IExtensionContext ) private readonly context : IExtensionContext ,
37
- @inject ( IFileSystem ) private readonly fs : IFileSystem ,
38
- @inject ( ICommandManager ) private readonly commandManager : ICommandManager ,
39
35
@inject ( NotebookKernel ) private readonly notebookKernel : NotebookKernel ,
40
36
@inject ( NotebookOutputRenderer ) private readonly renderer : NotebookOutputRenderer ,
41
37
@inject ( IApplicationEnvironment ) private readonly env : IApplicationEnvironment ,
42
- @inject ( IApplicationShell ) private readonly shell : IApplicationShell
38
+ @inject ( IApplicationShell ) private readonly shell : IApplicationShell ,
39
+ @inject ( IWorkspaceService ) private readonly workspace : IWorkspaceService
43
40
) { }
44
- public get isEnabled ( ) {
45
- const packageJsonFile = path . join ( this . context . extensionPath , 'package.json' ) ;
46
- const content = JSON . parse ( this . fs . readFileSync ( packageJsonFile ) ) ;
47
-
48
- // This code is temporary.
49
- return (
50
- content . enableProposedApi &&
51
- Array . isArray ( content . contributes . notebookOutputRenderer ) &&
52
- ( content . contributes . notebookOutputRenderer as [ ] ) . length > 0 &&
53
- Array . isArray ( content . contributes . notebookProvider ) &&
54
- ( content . contributes . notebookProvider as [ ] ) . length > 0
55
- ) ;
56
- }
57
- public async enableSideBySideUsage ( ) {
58
- await this . enableNotebooks ( false ) ;
59
- }
60
41
public async activate ( ) : Promise < void > {
61
42
// This condition is temporary.
62
43
// If user belongs to the experiment, then make the necessary changes to package.json.
63
44
// Once the API is final, we won't need to modify the package.json.
64
45
if ( this . experiment . inExperiment ( NotebookEditorSupport . nativeNotebookExperiment ) ) {
65
- await this . enableNotebooks ( true ) ;
46
+ await this . enableNotebooks ( ) ;
66
47
}
67
48
if ( this . env . channel !== 'insiders' ) {
68
49
return ;
@@ -108,62 +89,29 @@ export class NotebookIntegration implements IExtensionSingleActivationService {
108
89
}
109
90
}
110
91
}
111
- private async enableNotebooks ( useVSCodeNotebookAsDefaultEditor : boolean ) {
92
+ private async enableNotebooks ( ) {
112
93
if ( this . env . channel === 'stable' ) {
113
94
this . shell . showErrorMessage ( DataScience . previewNotebookOnlySupportedInVSCInsiders ( ) ) . then ( noop , noop ) ;
114
95
return ;
115
96
}
116
- const packageJsonFile = path . join ( this . context . extensionPath , 'package.json' ) ;
117
- const content = JSON . parse ( this . fs . readFileSync ( packageJsonFile ) ) ;
118
97
119
98
// This code is temporary.
99
+ const settings = this . workspace . getConfiguration ( 'workbench' , undefined ) ;
100
+ const editorAssociations = settings . get ( 'editorAssociations' ) as {
101
+ viewType : string ;
102
+ filenamePattern : string ;
103
+ } [ ] ;
104
+
120
105
if (
121
- ! content . enableProposedApi ||
122
- ! Array . isArray ( content . contributes . notebookOutputRenderer ) ||
123
- ! Array . isArray ( content . contributes . notebookProvider )
106
+ ! Array . isArray ( editorAssociations ) ||
107
+ editorAssociations . length === 0 ||
108
+ ! editorAssociations . find ( ( item ) => item . viewType === JupyterNotebookView )
124
109
) {
125
- content . enableProposedApi = true ;
126
- content . contributes . notebookOutputRenderer = [
127
- {
128
- viewType : 'jupyter-notebook-renderer' ,
129
- displayName : 'Jupyter Notebook Renderer' ,
130
- mimeTypes : [
131
- 'application/geo+json' ,
132
- 'application/vdom.v1+json' ,
133
- 'application/vnd.dataresource+json' ,
134
- 'application/vnd.plotly.v1+json' ,
135
- 'application/vnd.vega.v2+json' ,
136
- 'application/vnd.vega.v3+json' ,
137
- 'application/vnd.vega.v4+json' ,
138
- 'application/vnd.vega.v5+json' ,
139
- 'application/vnd.vegalite.v1+json' ,
140
- 'application/vnd.vegalite.v2+json' ,
141
- 'application/vnd.vegalite.v3+json' ,
142
- 'application/vnd.vegalite.v4+json' ,
143
- 'application/x-nteract-model-debug+json' ,
144
- 'image/gif' ,
145
- 'text/latex' ,
146
- 'text/vnd.plotly.v1+html'
147
- ]
148
- }
149
- ] ;
150
- content . contributes . notebookProvider = [
151
- {
152
- viewType : JupyterNotebookView ,
153
- displayName : 'Jupyter Notebook (preview)' ,
154
- selector : [
155
- {
156
- filenamePattern : '*.ipynb'
157
- }
158
- ] ,
159
- priority : useVSCodeNotebookAsDefaultEditor ? 'default' : 'option'
160
- }
161
- ] ;
162
-
163
- await this . fs . writeFile ( packageJsonFile , JSON . stringify ( content , undefined , 4 ) ) ;
164
- await this . commandManager
165
- . executeCommand ( 'python.reloadVSCode' , DataScience . reloadVSCodeNotebookEditor ( ) )
166
- . then ( noop , noop ) ;
110
+ editorAssociations . push ( {
111
+ viewType : 'jupyter-notebook' ,
112
+ filenamePattern : '*.ipynb'
113
+ } ) ;
114
+ await settings . update ( 'editorAssociations' , editorAssociations , ConfigurationTarget . Global ) ;
167
115
}
168
116
}
169
117
}
0 commit comments