22// Licensed under the MIT License.
33'use strict' ;
44import { inject , injectable } from 'inversify' ;
5- import * as path from 'path ' ;
5+ import { ConfigurationTarget } from 'vscode ' ;
66import { IExtensionSingleActivationService } from '../../activation/types' ;
77import {
88 IApplicationEnvironment ,
99 IApplicationShell ,
10- ICommandManager ,
11- IVSCodeNotebook
10+ IVSCodeNotebook ,
11+ IWorkspaceService
1212} from '../../common/application/types' ;
1313import { NotebookEditorSupport } from '../../common/experiments/groups' ;
1414import { 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' ;
1716import { DataScience } from '../../common/utils/localize' ;
1817import { noop } from '../../common/utils/misc' ;
1918import { JupyterNotebookView } from './constants' ;
@@ -33,36 +32,18 @@ export class NotebookIntegration implements IExtensionSingleActivationService {
3332 @inject ( IExperimentsManager ) private readonly experiment : IExperimentsManager ,
3433 @inject ( IDisposableRegistry ) private readonly disposables : IDisposableRegistry ,
3534 @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 ,
3935 @inject ( NotebookKernel ) private readonly notebookKernel : NotebookKernel ,
4036 @inject ( NotebookOutputRenderer ) private readonly renderer : NotebookOutputRenderer ,
4137 @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
4340 ) { }
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- }
6041 public async activate ( ) : Promise < void > {
6142 // This condition is temporary.
6243 // If user belongs to the experiment, then make the necessary changes to package.json.
6344 // Once the API is final, we won't need to modify the package.json.
6445 if ( this . experiment . inExperiment ( NotebookEditorSupport . nativeNotebookExperiment ) ) {
65- await this . enableNotebooks ( true ) ;
46+ await this . enableNotebooks ( ) ;
6647 }
6748 if ( this . env . channel !== 'insiders' ) {
6849 return ;
@@ -108,62 +89,29 @@ export class NotebookIntegration implements IExtensionSingleActivationService {
10889 }
10990 }
11091 }
111- private async enableNotebooks ( useVSCodeNotebookAsDefaultEditor : boolean ) {
92+ private async enableNotebooks ( ) {
11293 if ( this . env . channel === 'stable' ) {
11394 this . shell . showErrorMessage ( DataScience . previewNotebookOnlySupportedInVSCInsiders ( ) ) . then ( noop , noop ) ;
11495 return ;
11596 }
116- const packageJsonFile = path . join ( this . context . extensionPath , 'package.json' ) ;
117- const content = JSON . parse ( this . fs . readFileSync ( packageJsonFile ) ) ;
11897
11998 // 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+
120105 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 )
124109 ) {
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 ) ;
167115 }
168116 }
169117}
0 commit comments