33 * Licensed under the MIT License. See License.txt in the project root for license information.
44 *--------------------------------------------------------------------------------------------*/
55
6+ import { coalesce , distinct } from 'vs/base/common/arrays' ;
7+ import { Schemas } from 'vs/base/common/network' ;
68import { IDisposable , Disposable } from 'vs/base/common/lifecycle' ;
79import { ResourceMap } from 'vs/base/common/map' ;
810import { parse } from 'vs/base/common/marshalling' ;
@@ -24,17 +26,17 @@ import { Registry } from 'vs/platform/registry/common/platform';
2426import { EditorDescriptor , Extensions as EditorExtensions , IEditorRegistry } from 'vs/workbench/browser/editor' ;
2527import { Extensions as WorkbenchExtensions , IWorkbenchContribution , IWorkbenchContributionsRegistry } from 'vs/workbench/common/contributions' ;
2628import { EditorInput , Extensions as EditorInputExtensions , IEditorInput , IEditorInputFactory , IEditorInputFactoryRegistry } from 'vs/workbench/common/editor' ;
29+ import { IBackupFileService } from 'vs/workbench/services/backup/common/backup' ;
2730import { NotebookEditor } from 'vs/workbench/contrib/notebook/browser/notebookEditor' ;
2831import { NotebookEditorInput } from 'vs/workbench/contrib/notebook/browser/notebookEditorInput' ;
2932import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService' ;
3033import { NotebookService } from 'vs/workbench/contrib/notebook/browser/notebookServiceImpl' ;
31- import { CellKind , CellUri } from 'vs/workbench/contrib/notebook/common/notebookCommon' ;
34+ import { CellKind , CellUri , NotebookDocumentBackupData } from 'vs/workbench/contrib/notebook/common/notebookCommon' ;
3235import { NotebookProviderInfo } from 'vs/workbench/contrib/notebook/common/notebookProvider' ;
3336import { IEditorGroup , OpenEditorContext } from 'vs/workbench/services/editor/common/editorGroupsService' ;
3437import { IEditorService , IOpenEditorOverride } from 'vs/workbench/services/editor/common/editorService' ;
3538import { IConfigurationService } from 'vs/platform/configuration/common/configuration' ;
3639import { CustomEditorsAssociations , customEditorsAssociationsSettingId } from 'vs/workbench/services/editor/common/editorAssociationsSetting' ;
37- import { coalesce , distinct } from 'vs/base/common/arrays' ;
3840import { CustomEditorInfo } from 'vs/workbench/contrib/customEditor/common/customEditor' ;
3941import { NotebookEditorOptions } from 'vs/workbench/contrib/notebook/browser/notebookEditorWidget' ;
4042import { INotebookEditor } from 'vs/workbench/contrib/notebook/browser/notebookBrowser' ;
@@ -70,42 +72,73 @@ Registry.as<IEditorRegistry>(EditorExtensions.Editors).registerEditor(
7072 ]
7173) ;
7274
73- Registry . as < IEditorInputFactoryRegistry > ( EditorInputExtensions . EditorInputFactories ) . registerEditorInputFactory (
74- NotebookEditorInput . ID ,
75- class implements IEditorInputFactory {
76- canSerialize ( ) : boolean {
77- return true ;
75+ class NotebookEditorFactory implements IEditorInputFactory {
76+ canSerialize ( ) : boolean {
77+ return true ;
78+ }
79+ serialize ( input : EditorInput ) : string {
80+ assertType ( input instanceof NotebookEditorInput ) ;
81+ return JSON . stringify ( {
82+ resource : input . resource ,
83+ name : input . name ,
84+ viewType : input . viewType ,
85+ group : input . group
86+ } ) ;
87+ }
88+ deserialize ( instantiationService : IInstantiationService , raw : string ) {
89+ type Data = { resource : URI , name : string , viewType : string , group : number } ;
90+ const data = < Data > parse ( raw ) ;
91+ if ( ! data ) {
92+ return undefined ;
7893 }
79- serialize ( input : EditorInput ) : string {
80- assertType ( input instanceof NotebookEditorInput ) ;
81- return JSON . stringify ( {
82- resource : input . resource ,
83- name : input . name ,
84- viewType : input . viewType ,
85- group : input . group
86- } ) ;
94+ const { resource, name, viewType } = data ;
95+ if ( ! data || ! URI . isUri ( resource ) || typeof name !== 'string' || typeof viewType !== 'string' ) {
96+ return undefined ;
8797 }
88- deserialize ( instantiationService : IInstantiationService , raw : string ) {
89- type Data = { resource : URI , name : string , viewType : string , group : number } ;
90- const data = < Data > parse ( raw ) ;
91- if ( ! data ) {
92- return undefined ;
93- }
94- const { resource, name, viewType } = data ;
95- if ( ! data || ! URI . isUri ( resource ) || typeof name !== 'string' || typeof viewType !== 'string' ) {
96- return undefined ;
97- }
9898
99- // if we have two editors open with the same resource (in different editor groups), we should then create two different
100- // editor inputs, instead of `getOrCreate`.
101- const input = NotebookEditorInput . create ( instantiationService , resource , name , viewType ) ;
102- if ( typeof data . group === 'number' ) {
103- input . updateGroup ( data . group ) ;
99+ // if we have two editors open with the same resource (in different editor groups), we should then create two different
100+ // editor inputs, instead of `getOrCreate`.
101+ const input = NotebookEditorInput . create ( instantiationService , resource , name , viewType ) ;
102+ if ( typeof data . group === 'number' ) {
103+ input . updateGroup ( data . group ) ;
104+ }
105+
106+ return input ;
107+ }
108+
109+ static async createCustomEditorInput ( resource : URI , instantiationService : IInstantiationService ) : Promise < NotebookEditorInput > {
110+ return instantiationService . invokeFunction ( async accessor => {
111+ const backupFileService = accessor . get < IBackupFileService > ( IBackupFileService ) ;
112+
113+ const backup = await backupFileService . resolve < NotebookDocumentBackupData > ( resource ) ;
114+ if ( ! backup ?. meta ) {
115+ throw new Error ( `No backup found for Notebook editor: ${ resource } ` ) ;
104116 }
105117
118+ const input = NotebookEditorInput . create ( instantiationService , resource , backup . meta . name , backup . meta . viewType , { startDirty : true } ) ;
106119 return input ;
120+ } ) ;
121+ }
122+
123+ static canResolveBackup ( editorInput : IEditorInput , backupResource : URI ) : boolean {
124+ if ( editorInput instanceof NotebookEditorInput ) {
125+ if ( isEqual ( editorInput . resource . with ( { scheme : Schemas . vscodeNotebook } ) , backupResource ) ) {
126+ return true ;
127+ }
107128 }
129+
130+ return false ;
108131 }
132+ }
133+
134+ Registry . as < IEditorInputFactoryRegistry > ( EditorInputExtensions . EditorInputFactories ) . registerEditorInputFactory (
135+ NotebookEditorInput . ID ,
136+ NotebookEditorFactory
137+ ) ;
138+
139+ Registry . as < IEditorInputFactoryRegistry > ( EditorInputExtensions . EditorInputFactories ) . registerCustomEditorInputFactory (
140+ Schemas . vscodeNotebook ,
141+ NotebookEditorFactory
109142) ;
110143
111144function getFirstNotebookInfo ( notebookService : INotebookService , uri : URI ) : NotebookProviderInfo | undefined {
@@ -227,7 +260,7 @@ export class NotebookContribution extends Disposable implements IWorkbenchContri
227260 // Create a copy of the input.
228261 // Unlike normal editor inputs, we do not want to share custom editor inputs
229262 // between multiple editors / groups.
230- const copiedInput = this . instantiationService . createInstance ( NotebookEditorInput , originalInput . resource , originalInput . name , originalInput . viewType ) ;
263+ const copiedInput = NotebookEditorInput . create ( this . instantiationService , originalInput . resource , originalInput . name , originalInput . viewType ) ;
231264 copiedInput . updateGroup ( group . id ) ;
232265
233266 if ( context === OpenEditorContext . MOVE_EDITOR ) {
0 commit comments