@@ -15,28 +15,26 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
1515 private languageClient : LanguageClient ;
1616
1717 private _onDidChangeNotebook = new vscode . EventEmitter < vscode . NotebookDocumentEditEvent > ( ) ;
18- onDidChangeNotebook : vscode . Event < vscode . NotebookDocumentEditEvent > = this . _onDidChangeNotebook . event ;
19- kernel ?: vscode . NotebookKernel ;
18+ public onDidChangeNotebook : vscode . Event < vscode . NotebookDocumentEditEvent > = this . _onDidChangeNotebook . event ;
19+ public kernel ?: vscode . NotebookKernel ;
2020
21- constructor ( ) {
21+ public label : string = 'PowerShell' ;
22+ public preloads ?: vscode . Uri [ ] ;
23+
24+ public constructor ( ) {
25+ // VS Code Notebook API uses this property for handling cell execution.
2226 this . kernel = this ;
23- this . showNotebookModeCommand = vscode . commands . registerCommand ( "PowerShell.ShowNotebookMode" , async ( ) => {
24- const uri = vscode . window . activeTextEditor . document . uri ;
25- await vscode . commands . executeCommand ( 'workbench.action.closeActiveEditor' ) ;
26- await vscode . commands . executeCommand ( "vscode.openWith" , uri , "PowerShellNotebookMode" ) ;
27- } ) ;
2827
29- this . hideNotebookModeCommand = vscode . commands . registerCommand ( "PowerShell.HideNotebookMode" , async ( ) => {
30- const uri = vscode . notebook . activeNotebookEditor . document . uri ;
31- await vscode . commands . executeCommand ( 'workbench.action.closeActiveEditor' ) ;
32- await vscode . commands . executeCommand ( "vscode.openWith" , uri , "default" ) ;
33- } ) ;
34- }
28+ this . showNotebookModeCommand = vscode . commands . registerCommand (
29+ "PowerShell.ShowNotebookMode" ,
30+ PowerShellNotebooksFeature . showNotebookMode ) ;
3531
36- label : string = 'PowerShell' ;
37- preloads ?: vscode . Uri [ ] ;
32+ this . hideNotebookModeCommand = vscode . commands . registerCommand (
33+ "PowerShell.HideNotebookMode" ,
34+ PowerShellNotebooksFeature . hideNotebookMode ) ;
35+ }
3836
39- async openNotebook ( uri : vscode . Uri , context : vscode . NotebookDocumentOpenContext ) : Promise < vscode . NotebookData > {
37+ public async openNotebook ( uri : vscode . Uri , context : vscode . NotebookDocumentOpenContext ) : Promise < vscode . NotebookData > {
4038 // load from backup if needed.
4139 const actualUri = context . backupId ? vscode . Uri . parse ( context . backupId ) : uri ;
4240
@@ -49,43 +47,49 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
4947 metadata : { }
5048 }
5149
52- let curr : string [ ] = [ ] ;
50+ let currentCellSource : string [ ] = [ ] ;
5351 let cellKind : vscode . CellKind | undefined ;
5452 let insideBlockComment : boolean = false ;
5553
54+ // Iterate through all lines in a document (aka ps1 file) and group the lines
55+ // into cells (markdown or code) that will be rendered in Notebook mode.
5656 // tslint:disable-next-line: prefer-for-of
5757 for ( let i = 0 ; i < lines . length ; i ++ ) {
5858 // Handle block comments
5959 if ( insideBlockComment ) {
6060 if ( lines [ i ] === "#>" ) {
61+ // We've reached the end of a block comment,
62+ // push a markdown cell.
6163 insideBlockComment = false ;
6264
6365 notebookData . cells . push ( {
6466 cellKind : vscode . CellKind . Markdown ,
6567 language : "markdown" ,
6668 outputs : [ ] ,
67- source : curr . join ( "\n" ) ,
69+ source : currentCellSource . join ( "\n" ) ,
6870 metadata : {
6971 custom : {
7072 commentType : CommentType . BlockComment
7173 }
7274 }
7375 } ) ;
7476
75- curr = [ ] ;
77+ currentCellSource = [ ] ;
7678 cellKind = undefined ;
7779 continue ;
78- } else {
79- curr . push ( lines [ i ] ) ;
80- continue ;
8180 }
81+
82+ // If we're still in a block comment, push the line and continue.
83+ currentCellSource . push ( lines [ i ] ) ;
84+ continue ;
8285 } else if ( lines [ i ] === "<#" ) {
83- // Insert what we saw leading up to this.
86+ // If we found the start of a block comment,
87+ // insert what we saw leading up to this.
8488 notebookData . cells . push ( {
8589 cellKind : cellKind ! ,
8690 language : cellKind === vscode . CellKind . Markdown ? 'markdown' : 'powershell' ,
8791 outputs : [ ] ,
88- source : curr . join ( "\n" ) ,
92+ source : currentCellSource . join ( "\n" ) ,
8993 metadata : {
9094 custom : {
9195 commentType : cellKind === vscode . CellKind . Markdown ? CommentType . LineComment : CommentType . Disabled ,
@@ -94,7 +98,7 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
9498 } ) ;
9599
96100 // reset state because we're starting a new Markdown cell.
97- curr = [ ] ;
101+ currentCellSource = [ ] ;
98102 cellKind = vscode . CellKind . Markdown ;
99103 insideBlockComment = true ;
100104 continue ;
@@ -104,17 +108,17 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
104108 // If a line starts with # it's a comment
105109 const kind : vscode . CellKind = lines [ i ] . startsWith ( "#" ) ? vscode . CellKind . Markdown : vscode . CellKind . Code ;
106110
107- // If this line is a continuation of the previous cell type, then add this line to curr .
111+ // If this line is a continuation of the previous cell type, then add this line to the current cell source .
108112 if ( kind === cellKind ) {
109- curr . push ( kind === vscode . CellKind . Markdown && ! insideBlockComment ? lines [ i ] . replace ( / ^ \# \s * / , '' ) : lines [ i ] ) ;
113+ currentCellSource . push ( kind === vscode . CellKind . Markdown && ! insideBlockComment ? lines [ i ] . replace ( / ^ \# \s * / , '' ) : lines [ i ] ) ;
110114 } else {
111- // If cellKind is not undifined, then we can add the cell we've just computed to the editBuilder .
115+ // If cellKind is not undifined, then we can add the cell we've just computed.
112116 if ( cellKind !== undefined ) {
113117 notebookData . cells . push ( {
114118 cellKind : cellKind ! ,
115119 language : cellKind === vscode . CellKind . Markdown ? 'markdown' : 'powershell' ,
116120 outputs : [ ] ,
117- source : curr . join ( "\n" ) ,
121+ source : currentCellSource . join ( "\n" ) ,
118122 metadata : {
119123 custom : {
120124 commentType : cellKind === vscode . CellKind . Markdown ? CommentType . LineComment : CommentType . Disabled ,
@@ -124,18 +128,21 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
124128 }
125129
126130 // set initial new cell state
127- curr = [ ] ;
131+ currentCellSource = [ ] ;
128132 cellKind = kind ;
129- curr . push ( kind === vscode . CellKind . Markdown ? lines [ i ] . replace ( / ^ \# \s * / , '' ) : lines [ i ] ) ;
133+ currentCellSource . push ( kind === vscode . CellKind . Markdown ? lines [ i ] . replace ( / ^ \# \s * / , '' ) : lines [ i ] ) ;
130134 }
131135 }
132136
133- if ( curr . length ) {
137+ // If we have some leftover lines that have not been added (for example,
138+ // when there is only the _start_ of a block comment but not an _end_.)
139+ // add the appropriate cell.
140+ if ( currentCellSource . length ) {
134141 notebookData . cells . push ( {
135142 cellKind : cellKind ! ,
136143 language : cellKind === vscode . CellKind . Markdown ? 'markdown' : 'powershell' ,
137144 outputs : [ ] ,
138- source : curr . join ( "\n" ) ,
145+ source : currentCellSource . join ( "\n" ) ,
139146 metadata : {
140147 custom : {
141148 commentType : cellKind === vscode . CellKind . Markdown ? CommentType . LineComment : CommentType . Disabled ,
@@ -147,19 +154,20 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
147154 return notebookData ;
148155 }
149156
150- resolveNotebook ( document : vscode . NotebookDocument , webview : { readonly onDidReceiveMessage : vscode . Event < any > ; postMessage ( message : any ) : Thenable < boolean > ; asWebviewUri ( localResource : vscode . Uri ) : vscode . Uri ; } ) : Promise < void > {
157+ public resolveNotebook ( document : vscode . NotebookDocument , webview : { readonly onDidReceiveMessage : vscode . Event < any > ; postMessage ( message : any ) : Thenable < boolean > ; asWebviewUri ( localResource : vscode . Uri ) : vscode . Uri ; } ) : Promise < void > {
158+ // We don't need to do anything here because our Notebooks are backed by files.
151159 return ;
152160 }
153161
154- saveNotebook ( document : vscode . NotebookDocument , cancellation : vscode . CancellationToken ) : Promise < void > {
162+ public saveNotebook ( document : vscode . NotebookDocument , cancellation : vscode . CancellationToken ) : Promise < void > {
155163 return this . _save ( document , document . uri , cancellation ) ;
156164 }
157165
158- saveNotebookAs ( targetResource : vscode . Uri , document : vscode . NotebookDocument , cancellation : vscode . CancellationToken ) : Promise < void > {
166+ public saveNotebookAs ( targetResource : vscode . Uri , document : vscode . NotebookDocument , cancellation : vscode . CancellationToken ) : Promise < void > {
159167 return this . _save ( document , targetResource , cancellation ) ;
160168 }
161169
162- async backupNotebook ( document : vscode . NotebookDocument , context : vscode . NotebookDocumentBackupContext , cancellation : vscode . CancellationToken ) : Promise < vscode . NotebookDocumentBackup > {
170+ public async backupNotebook ( document : vscode . NotebookDocument , context : vscode . NotebookDocumentBackupContext , cancellation : vscode . CancellationToken ) : Promise < vscode . NotebookDocumentBackup > {
163171 await this . _save ( document , context . destination , cancellation ) ;
164172
165173 return {
@@ -179,9 +187,9 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
179187 this . languageClient = languageClient ;
180188 }
181189
182- async _save ( document : vscode . NotebookDocument , targetResource : vscode . Uri , _token : vscode . CancellationToken ) : Promise < void > {
190+ private async _save ( document : vscode . NotebookDocument , targetResource : vscode . Uri , _token : vscode . CancellationToken ) : Promise < void > {
183191 const retArr : string [ ] = [ ] ;
184- document . cells . forEach ( ( cell ) => {
192+ for ( const cell of document . cells ) {
185193 if ( cell . cellKind === vscode . CellKind . Code ) {
186194 retArr . push ( ...cell . document . getText ( ) . split ( / \r | \n | \r \n / ) ) ;
187195 } else {
@@ -197,18 +205,33 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
197205 retArr . push ( ...cell . document . getText ( ) . split ( / \r | \n | \r \n / ) . map ( line => `# ${ line } ` ) ) ;
198206 }
199207 }
200- } ) ;
208+ }
201209
202210 await vscode . workspace . fs . writeFile ( targetResource , new TextEncoder ( ) . encode ( retArr . join ( '\n' ) ) ) ;
203211 }
204212
205- async executeAllCells ( document : vscode . NotebookDocument , token : vscode . CancellationToken ) : Promise < void > {
213+ private static async showNotebookMode ( ) {
214+ const uri = vscode . window . activeTextEditor . document . uri ;
215+ await vscode . commands . executeCommand ( 'workbench.action.closeActiveEditor' ) ;
216+ await vscode . commands . executeCommand ( "vscode.openWith" , uri , "PowerShellNotebookMode" ) ;
217+ }
218+
219+ private static async hideNotebookMode ( ) {
220+ const uri = vscode . notebook . activeNotebookEditor . document . uri ;
221+ await vscode . commands . executeCommand ( 'workbench.action.closeActiveEditor' ) ;
222+ await vscode . commands . executeCommand ( "vscode.openWith" , uri , "default" ) ;
223+ }
224+
225+ /*
226+ `vscode.NotebookKernel` implementations
227+ */
228+ public async executeAllCells ( document : vscode . NotebookDocument , token : vscode . CancellationToken ) : Promise < void > {
206229 for ( const cell of document . cells ) {
207230 await this . executeCell ( document , cell , token ) ;
208231 }
209232 }
210233
211- async executeCell ( document : vscode . NotebookDocument , cell : vscode . NotebookCell | undefined , token : vscode . CancellationToken ) : Promise < void > {
234+ public async executeCell ( document : vscode . NotebookDocument , cell : vscode . NotebookCell | undefined , token : vscode . CancellationToken ) : Promise < void > {
212235 if ( token . isCancellationRequested ) {
213236 return ;
214237 }
0 commit comments