@@ -15,28 +15,26 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
15
15
private languageClient : LanguageClient ;
16
16
17
17
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 ;
20
20
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.
22
26
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
- } ) ;
28
27
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 ) ;
35
31
36
- label : string = 'PowerShell' ;
37
- preloads ?: vscode . Uri [ ] ;
32
+ this . hideNotebookModeCommand = vscode . commands . registerCommand (
33
+ "PowerShell.HideNotebookMode" ,
34
+ PowerShellNotebooksFeature . hideNotebookMode ) ;
35
+ }
38
36
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 > {
40
38
// load from backup if needed.
41
39
const actualUri = context . backupId ? vscode . Uri . parse ( context . backupId ) : uri ;
42
40
@@ -49,43 +47,49 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
49
47
metadata : { }
50
48
}
51
49
52
- let curr : string [ ] = [ ] ;
50
+ let currentCellSource : string [ ] = [ ] ;
53
51
let cellKind : vscode . CellKind | undefined ;
54
52
let insideBlockComment : boolean = false ;
55
53
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.
56
56
// tslint:disable-next-line: prefer-for-of
57
57
for ( let i = 0 ; i < lines . length ; i ++ ) {
58
58
// Handle block comments
59
59
if ( insideBlockComment ) {
60
60
if ( lines [ i ] === "#>" ) {
61
+ // We've reached the end of a block comment,
62
+ // push a markdown cell.
61
63
insideBlockComment = false ;
62
64
63
65
notebookData . cells . push ( {
64
66
cellKind : vscode . CellKind . Markdown ,
65
67
language : "markdown" ,
66
68
outputs : [ ] ,
67
- source : curr . join ( "\n" ) ,
69
+ source : currentCellSource . join ( "\n" ) ,
68
70
metadata : {
69
71
custom : {
70
72
commentType : CommentType . BlockComment
71
73
}
72
74
}
73
75
} ) ;
74
76
75
- curr = [ ] ;
77
+ currentCellSource = [ ] ;
76
78
cellKind = undefined ;
77
79
continue ;
78
- } else {
79
- curr . push ( lines [ i ] ) ;
80
- continue ;
81
80
}
81
+
82
+ // If we're still in a block comment, push the line and continue.
83
+ currentCellSource . push ( lines [ i ] ) ;
84
+ continue ;
82
85
} 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.
84
88
notebookData . cells . push ( {
85
89
cellKind : cellKind ! ,
86
90
language : cellKind === vscode . CellKind . Markdown ? 'markdown' : 'powershell' ,
87
91
outputs : [ ] ,
88
- source : curr . join ( "\n" ) ,
92
+ source : currentCellSource . join ( "\n" ) ,
89
93
metadata : {
90
94
custom : {
91
95
commentType : cellKind === vscode . CellKind . Markdown ? CommentType . LineComment : CommentType . Disabled ,
@@ -94,7 +98,7 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
94
98
} ) ;
95
99
96
100
// reset state because we're starting a new Markdown cell.
97
- curr = [ ] ;
101
+ currentCellSource = [ ] ;
98
102
cellKind = vscode . CellKind . Markdown ;
99
103
insideBlockComment = true ;
100
104
continue ;
@@ -104,17 +108,17 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
104
108
// If a line starts with # it's a comment
105
109
const kind : vscode . CellKind = lines [ i ] . startsWith ( "#" ) ? vscode . CellKind . Markdown : vscode . CellKind . Code ;
106
110
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 .
108
112
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 ] ) ;
110
114
} 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.
112
116
if ( cellKind !== undefined ) {
113
117
notebookData . cells . push ( {
114
118
cellKind : cellKind ! ,
115
119
language : cellKind === vscode . CellKind . Markdown ? 'markdown' : 'powershell' ,
116
120
outputs : [ ] ,
117
- source : curr . join ( "\n" ) ,
121
+ source : currentCellSource . join ( "\n" ) ,
118
122
metadata : {
119
123
custom : {
120
124
commentType : cellKind === vscode . CellKind . Markdown ? CommentType . LineComment : CommentType . Disabled ,
@@ -124,18 +128,21 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
124
128
}
125
129
126
130
// set initial new cell state
127
- curr = [ ] ;
131
+ currentCellSource = [ ] ;
128
132
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 ] ) ;
130
134
}
131
135
}
132
136
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 ) {
134
141
notebookData . cells . push ( {
135
142
cellKind : cellKind ! ,
136
143
language : cellKind === vscode . CellKind . Markdown ? 'markdown' : 'powershell' ,
137
144
outputs : [ ] ,
138
- source : curr . join ( "\n" ) ,
145
+ source : currentCellSource . join ( "\n" ) ,
139
146
metadata : {
140
147
custom : {
141
148
commentType : cellKind === vscode . CellKind . Markdown ? CommentType . LineComment : CommentType . Disabled ,
@@ -147,19 +154,20 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
147
154
return notebookData ;
148
155
}
149
156
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.
151
159
return ;
152
160
}
153
161
154
- saveNotebook ( document : vscode . NotebookDocument , cancellation : vscode . CancellationToken ) : Promise < void > {
162
+ public saveNotebook ( document : vscode . NotebookDocument , cancellation : vscode . CancellationToken ) : Promise < void > {
155
163
return this . _save ( document , document . uri , cancellation ) ;
156
164
}
157
165
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 > {
159
167
return this . _save ( document , targetResource , cancellation ) ;
160
168
}
161
169
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 > {
163
171
await this . _save ( document , context . destination , cancellation ) ;
164
172
165
173
return {
@@ -179,9 +187,9 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
179
187
this . languageClient = languageClient ;
180
188
}
181
189
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 > {
183
191
const retArr : string [ ] = [ ] ;
184
- document . cells . forEach ( ( cell ) => {
192
+ for ( const cell of document . cells ) {
185
193
if ( cell . cellKind === vscode . CellKind . Code ) {
186
194
retArr . push ( ...cell . document . getText ( ) . split ( / \r | \n | \r \n / ) ) ;
187
195
} else {
@@ -197,18 +205,33 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
197
205
retArr . push ( ...cell . document . getText ( ) . split ( / \r | \n | \r \n / ) . map ( line => `# ${ line } ` ) ) ;
198
206
}
199
207
}
200
- } ) ;
208
+ }
201
209
202
210
await vscode . workspace . fs . writeFile ( targetResource , new TextEncoder ( ) . encode ( retArr . join ( '\n' ) ) ) ;
203
211
}
204
212
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 > {
206
229
for ( const cell of document . cells ) {
207
230
await this . executeCell ( document , cell , token ) ;
208
231
}
209
232
}
210
233
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 > {
212
235
if ( token . isCancellationRequested ) {
213
236
return ;
214
237
}
0 commit comments