33 * Licensed under the MIT License. See License.txt in the project root for license information.
44 *--------------------------------------------------------------------------------------------*/
55
6+ import * as nls from 'vs/nls' ;
67import { extHostNamedCustomer } from 'vs/workbench/api/common/extHostCustomers' ;
78import { MainContext , MainThreadNotebookShape , NotebookExtensionDescription , IExtHostContext , ExtHostNotebookShape , ExtHostContext , INotebookDocumentsAndEditorsDelta , INotebookModelAddedData } from '../common/extHost.protocol' ;
89import { Disposable , IDisposable , combinedDisposable } from 'vs/base/common/lifecycle' ;
@@ -17,6 +18,8 @@ import { CancellationToken } from 'vs/base/common/cancellation';
1718import { IAccessibilityService } from 'vs/platform/accessibility/common/accessibility' ;
1819import { IRelativePattern } from 'vs/base/common/glob' ;
1920import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions' ;
21+ import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation' ;
22+ import { IUndoRedoService , UndoRedoElementType } from 'vs/platform/undoRedo/common/undoRedo' ;
2023
2124export class MainThreadNotebookDocument extends Disposable {
2225 private _textModel : NotebookTextModel ;
@@ -31,9 +34,12 @@ export class MainThreadNotebookDocument extends Disposable {
3134 public viewType : string ,
3235 public supportBackup : boolean ,
3336 public uri : URI ,
34- readonly notebookService : INotebookService
37+ @INotebookService readonly notebookService : INotebookService ,
38+ @IUndoRedoService readonly undoRedoService : IUndoRedoService
39+
3540 ) {
3641 super ( ) ;
42+
3743 this . _textModel = new NotebookTextModel ( handle , viewType , supportBackup , uri ) ;
3844 this . _register ( this . _textModel . onDidModelChangeProxy ( e => {
3945 this . _proxy . $acceptModelChanged ( this . uri , e ) ;
@@ -54,6 +60,22 @@ export class MainThreadNotebookDocument extends Disposable {
5460 await this . notebookService . transformSpliceOutputs ( this . textModel , splices ) ;
5561 this . _textModel . $spliceNotebookCellOutputs ( cellHandle , splices ) ;
5662 }
63+
64+ handleEdit ( editId : number , label : string | undefined ) : void {
65+ this . undoRedoService . pushElement ( {
66+ type : UndoRedoElementType . Resource ,
67+ resource : this . _textModel . uri ,
68+ label : label ?? nls . localize ( 'defaultEditLabel' , "Edit" ) ,
69+ undo : async ( ) => {
70+ await this . _proxy . $undoNotebook ( this . _textModel . viewType , this . _textModel . uri , editId , this . _textModel . isDirty ) ;
71+ } ,
72+ redo : async ( ) => {
73+ await this . _proxy . $redoNotebook ( this . _textModel . viewType , this . _textModel . uri , editId , this . _textModel . isDirty ) ;
74+ } ,
75+ } ) ;
76+ this . _textModel . setDirty ( true ) ;
77+ }
78+
5779 dispose ( ) {
5880 this . _textModel . dispose ( ) ;
5981 super . dispose ( ) ;
@@ -179,7 +201,8 @@ export class MainThreadNotebooks extends Disposable implements MainThreadNoteboo
179201 @INotebookService private _notebookService : INotebookService ,
180202 @IConfigurationService private readonly configurationService : IConfigurationService ,
181203 @IEditorService private readonly editorService : IEditorService ,
182- @IAccessibilityService private readonly accessibilityService : IAccessibilityService
204+ @IAccessibilityService private readonly accessibilityService : IAccessibilityService ,
205+ @IInstantiationService private readonly _instantiationService : IInstantiationService
183206
184207 ) {
185208 super ( ) ;
@@ -388,7 +411,7 @@ export class MainThreadNotebooks extends Disposable implements MainThreadNoteboo
388411 }
389412
390413 async $registerNotebookProvider ( extension : NotebookExtensionDescription , viewType : string , supportBackup : boolean , kernel : INotebookKernelInfoDto | undefined ) : Promise < void > {
391- let controller = new MainThreadNotebookController ( this . _proxy , this , viewType , supportBackup , kernel , this . _notebookService ) ;
414+ let controller = new MainThreadNotebookController ( this . _proxy , this , viewType , supportBackup , kernel , this . _notebookService , this . _instantiationService ) ;
392415 this . _notebookProviders . set ( viewType , controller ) ;
393416 this . _notebookService . registerNotebookController ( viewType , extension , controller ) ;
394417 return ;
@@ -467,6 +490,16 @@ export class MainThreadNotebooks extends Disposable implements MainThreadNoteboo
467490
468491 return false ;
469492 }
493+
494+ $onDidEdit ( resource : UriComponents , viewType : string , editId : number , label : string | undefined ) : void {
495+ let controller = this . _notebookProviders . get ( viewType ) ;
496+ controller ?. handleEdit ( resource , editId , label ) ;
497+ }
498+
499+ $onContentChange ( resource : UriComponents , viewType : string ) : void {
500+ let controller = this . _notebookProviders . get ( viewType ) ;
501+ controller ?. handleNotebookChange ( resource ) ;
502+ }
470503}
471504
472505export class MainThreadNotebookController implements IMainNotebookController {
@@ -480,6 +513,7 @@ export class MainThreadNotebookController implements IMainNotebookController {
480513 private _supportBackup : boolean ,
481514 readonly kernel : INotebookKernelInfoDto | undefined ,
482515 readonly notebookService : INotebookService ,
516+ readonly _instantiationService : IInstantiationService
483517
484518 ) {
485519 }
@@ -504,7 +538,7 @@ export class MainThreadNotebookController implements IMainNotebookController {
504538 return mainthreadNotebook . textModel ;
505539 }
506540
507- let document = new MainThreadNotebookDocument ( this . _proxy , MainThreadNotebookController . documentHandle ++ , viewType , this . _supportBackup , uri , this . notebookService ) ;
541+ let document = this . _instantiationService . createInstance ( MainThreadNotebookDocument , this . _proxy , MainThreadNotebookController . documentHandle ++ , viewType , this . _supportBackup , uri ) ;
508542 this . _mapping . set ( document . uri . toString ( ) , document ) ;
509543
510544 if ( backup ) {
@@ -635,6 +669,11 @@ export class MainThreadNotebookController implements IMainNotebookController {
635669 document ?. textModel . handleUnknownChange ( ) ;
636670 }
637671
672+ handleEdit ( resource : UriComponents , editId : number , label : string | undefined ) : void {
673+ let document = this . _mapping . get ( URI . from ( resource ) . toString ( ) ) ;
674+ document ?. handleEdit ( editId , label ) ;
675+ }
676+
638677 updateLanguages ( resource : UriComponents , languages : string [ ] ) {
639678 let document = this . _mapping . get ( URI . from ( resource ) . toString ( ) ) ;
640679 document ?. textModel . updateLanguages ( languages ) ;
0 commit comments