22// Licensed under the MIT License.
33
44import { inject , injectable } from 'inversify' ;
5- import {
6- Disposable ,
7- Event ,
8- EventEmitter ,
9- GlobPattern ,
5+ import { Disposable , Event , EventEmitter , GlobPattern , TextDocument , window } from 'vscode' ;
6+ import type {
107 notebook ,
118 NotebookContentProvider ,
129 NotebookDocument ,
1310 NotebookEditor ,
1411 NotebookKernel ,
1512 NotebookOutputRenderer ,
16- NotebookOutputSelector ,
17- TextDocument ,
18- window
19- } from 'vscode' ;
13+ NotebookOutputSelector
14+ } from 'vscode-proposed' ;
2015import { UseProposedApi } from '../constants' ;
2116import { IDisposableRegistry } from '../types' ;
2217import {
@@ -36,13 +31,13 @@ export class VSCodeNotebook implements IVSCodeNotebook {
3631 | NotebookCellLanguageChangeEvent
3732 > ( ) ;
3833 public get onDidOpenNotebookDocument ( ) : Event < NotebookDocument > {
39- return notebook . onDidOpenNotebookDocument ;
34+ return this . notebook . onDidOpenNotebookDocument ;
4035 }
4136 public get onDidCloseNotebookDocument ( ) : Event < NotebookDocument > {
42- return notebook . onDidCloseNotebookDocument ;
37+ return this . notebook . onDidCloseNotebookDocument ;
4338 }
4439 public get notebookEditors ( ) {
45- return notebook . visibleNotebookEditors ;
40+ return this . notebook . visibleNotebookEditors ;
4641 }
4742 public get onDidChangeNotebookDocument ( ) : Event <
4843 | NotebookCellsChangeEvent
@@ -64,36 +59,44 @@ export class VSCodeNotebook implements IVSCodeNotebook {
6459 }
6560 // Temporary, currently VSC API doesn't work well.
6661 // `notebook.activeNotebookEditor` is not reset when opening another file.
67- if ( ! notebook . activeNotebookEditor ) {
62+ if ( ! this . notebook . activeNotebookEditor ) {
6863 return ;
6964 }
7065 // If we have a text editor opened and it is not a cell, then we know for certain a notebook is not open.
7166 if ( window . activeTextEditor && ! this . isCell ( window . activeTextEditor . document ) ) {
7267 return ;
7368 }
7469 // Temporary until VSC API stabilizes.
75- if ( Array . isArray ( notebook . visibleNotebookEditors ) ) {
76- return notebook . visibleNotebookEditors . find ( ( item ) => item . active && item . visible ) ;
70+ if ( Array . isArray ( this . notebook . visibleNotebookEditors ) ) {
71+ return this . notebook . visibleNotebookEditors . find ( ( item ) => item . active && item . visible ) ;
7772 }
78- return notebook . activeNotebookEditor ;
73+ return this . notebook . activeNotebookEditor ;
7974 }
8075 private addedEventHandlers ?: boolean ;
76+ private _notebook ?: typeof notebook ;
77+ private get notebook ( ) {
78+ if ( ! this . _notebook ) {
79+ // tslint:disable-next-line: no-require-imports
80+ this . _notebook = require ( 'vscode' ) . notebook ;
81+ }
82+ return this . _notebook ! ;
83+ }
8184 constructor (
8285 @inject ( UseProposedApi ) private readonly useProposedApi : boolean ,
8386 @inject ( IDisposableRegistry ) private readonly disposables : IDisposableRegistry
8487 ) { }
8588 public registerNotebookContentProvider ( notebookType : string , provider : NotebookContentProvider ) : Disposable {
86- return notebook . registerNotebookContentProvider ( notebookType , provider ) ;
89+ return this . notebook . registerNotebookContentProvider ( notebookType , provider ) ;
8790 }
8891 public registerNotebookKernel ( id : string , selectors : GlobPattern [ ] , kernel : NotebookKernel ) : Disposable {
89- return notebook . registerNotebookKernel ( id , selectors , kernel ) ;
92+ return this . notebook . registerNotebookKernel ( id , selectors , kernel ) ;
9093 }
9194 public registerNotebookOutputRenderer (
9295 id : string ,
9396 outputSelector : NotebookOutputSelector ,
9497 renderer : NotebookOutputRenderer
9598 ) : Disposable {
96- return notebook . registerNotebookOutputRenderer ( id , outputSelector , renderer ) ;
99+ return this . notebook . registerNotebookOutputRenderer ( id , outputSelector , renderer ) ;
97100 }
98101 public isCell ( textDocument : TextDocument ) {
99102 return (
@@ -109,16 +112,16 @@ export class VSCodeNotebook implements IVSCodeNotebook {
109112 }
110113 this . disposables . push (
111114 ...[
112- notebook . onDidChangeCellLanguage ( ( e ) =>
115+ this . notebook . onDidChangeCellLanguage ( ( e ) =>
113116 this . _onDidChangeNotebookDocument . fire ( { ...e , type : 'changeCellLanguage' } )
114117 ) ,
115- notebook . onDidChangeCellOutputs ( ( e ) =>
118+ this . notebook . onDidChangeCellOutputs ( ( e ) =>
116119 this . _onDidChangeNotebookDocument . fire ( { ...e , type : 'changeCellOutputs' } )
117120 ) ,
118- notebook . onDidChangeNotebookCells ( ( e ) =>
121+ this . notebook . onDidChangeNotebookCells ( ( e ) =>
119122 this . _onDidChangeNotebookDocument . fire ( { ...e , type : 'changeCells' } )
120123 ) ,
121- notebook . onDidMoveNotebookCell ( ( e ) =>
124+ this . notebook . onDidMoveNotebookCell ( ( e ) =>
122125 this . _onDidChangeNotebookDocument . fire ( { ...e , type : 'moveCell' } )
123126 )
124127 ]
0 commit comments