@@ -9,34 +9,69 @@ import { LanguageClientConsumer } from "../languageClientConsumer";
9
9
import Settings = require( "../settings" ) ;
10
10
import { ILogger } from "../logging" ;
11
11
12
- export class PowerShellNotebooksFeature extends LanguageClientConsumer implements vscode . NotebookContentProvider , vscode . NotebookKernel {
12
+ export class PowerShellNotebooksFeature extends LanguageClientConsumer {
13
13
14
- private readonly showNotebookModeCommand : vscode . Disposable ;
15
- private readonly hideNotebookModeCommand : vscode . Disposable ;
14
+ private readonly disposables : vscode . Disposable [ ] ;
15
+ private readonly notebookContentProvider : vscode . NotebookContentProvider ;
16
+ private readonly notebookKernel : PowerShellNotebookKernel ;
16
17
17
- private _onDidChangeNotebook = new vscode . EventEmitter < vscode . NotebookDocumentEditEvent > ( ) ;
18
- public onDidChangeNotebook : vscode . Event < vscode . NotebookDocumentEditEvent > = this . _onDidChangeNotebook . event ;
19
- public kernel ?: vscode . NotebookKernel ;
18
+ public constructor ( logger : ILogger , skipRegisteringCommands ?: boolean ) {
19
+ super ( ) ;
20
+ this . disposables = [ ] ;
21
+ if ( ! skipRegisteringCommands ) {
22
+ this . disposables . push ( vscode . commands . registerCommand (
23
+ "PowerShell.EnableNotebookMode" ,
24
+ PowerShellNotebooksFeature . EnableNotebookMode ) ) ;
20
25
21
- public label : string = "PowerShell" ;
22
- public preloads ?: vscode . Uri [ ] ;
26
+ this . disposables . push ( vscode . commands . registerCommand (
27
+ "PowerShell.DisableNotebookMode" ,
28
+ PowerShellNotebooksFeature . DisableNotebookMode ) ) ;
29
+ }
23
30
24
- public constructor ( private logger : ILogger , skipRegisteringCommands ?: boolean ) {
25
- super ( ) ;
26
- // VS Code Notebook API uses this property for handling cell execution.
27
- this . kernel = this ;
31
+ this . notebookContentProvider = new PowerShellNotebookContentProvider ( logger ) ;
32
+ this . notebookKernel = new PowerShellNotebookKernel ( ) ;
33
+ }
28
34
29
- if ( ! skipRegisteringCommands ) {
30
- this . showNotebookModeCommand = vscode . commands . registerCommand (
31
- "PowerShell.ShowNotebookMode" ,
32
- PowerShellNotebooksFeature . showNotebookMode ) ;
35
+ public registerNotebookProviders ( ) {
36
+ this . disposables . push ( vscode . notebook . registerNotebookKernelProvider ( {
37
+ viewType : "PowerShellNotebookMode"
38
+ } , this . notebookKernel ) ) ;
39
+
40
+ this . disposables . push ( vscode . notebook . registerNotebookContentProvider (
41
+ "PowerShellNotebookMode" ,
42
+ this . notebookContentProvider ) ) ;
43
+ }
33
44
34
- this . hideNotebookModeCommand = vscode . commands . registerCommand (
35
- "PowerShell.HideNotebookMode" ,
36
- PowerShellNotebooksFeature . hideNotebookMode ) ;
45
+ public dispose ( ) {
46
+ for ( const disposable of this . disposables ) {
47
+ disposable . dispose ( ) ;
37
48
}
38
49
}
39
50
51
+ public setLanguageClient ( languageClient : LanguageClient ) {
52
+ this . notebookKernel . setLanguageClient ( languageClient ) ;
53
+ }
54
+
55
+ private static async EnableNotebookMode ( ) {
56
+ const uri = vscode . window . activeTextEditor . document . uri ;
57
+ await vscode . commands . executeCommand ( "workbench.action.closeActiveEditor" ) ;
58
+ await vscode . commands . executeCommand ( "vscode.openWith" , uri , "PowerShellNotebookMode" ) ;
59
+ }
60
+
61
+ private static async DisableNotebookMode ( ) {
62
+ const uri = vscode . notebook . activeNotebookEditor . document . uri ;
63
+ await vscode . commands . executeCommand ( "workbench.action.closeActiveEditor" ) ;
64
+ await vscode . commands . executeCommand ( "vscode.openWith" , uri , "default" ) ;
65
+ }
66
+ }
67
+
68
+ class PowerShellNotebookContentProvider implements vscode . NotebookContentProvider {
69
+ private _onDidChangeNotebook = new vscode . EventEmitter < vscode . NotebookDocumentEditEvent > ( ) ;
70
+ public onDidChangeNotebook : vscode . Event < vscode . NotebookDocumentEditEvent > = this . _onDidChangeNotebook . event ;
71
+
72
+ public constructor ( private logger : ILogger ) {
73
+ }
74
+
40
75
public async openNotebook ( uri : vscode . Uri , context : vscode . NotebookDocumentOpenContext ) : Promise < vscode . NotebookData > {
41
76
// load from backup if needed.
42
77
const actualUri = context . backupId ? vscode . Uri . parse ( context . backupId ) : uri ;
@@ -186,11 +221,6 @@ export class PowerShellNotebooksFeature extends LanguageClientConsumer implement
186
221
} ;
187
222
}
188
223
189
- public dispose ( ) {
190
- this . showNotebookModeCommand . dispose ( ) ;
191
- this . hideNotebookModeCommand . dispose ( ) ;
192
- }
193
-
194
224
private async _save ( document : vscode . NotebookDocument , targetResource : vscode . Uri , _token : vscode . CancellationToken ) : Promise < void > {
195
225
this . logger . writeDiagnostic ( `Saving Notebook: ${ targetResource . toString ( ) } ` ) ;
196
226
@@ -215,35 +245,51 @@ export class PowerShellNotebooksFeature extends LanguageClientConsumer implement
215
245
216
246
await vscode . workspace . fs . writeFile ( targetResource , new TextEncoder ( ) . encode ( retArr . join ( "\n" ) ) ) ;
217
247
}
248
+ }
218
249
219
- private static async showNotebookMode ( ) {
220
- const uri = vscode . window . activeTextEditor . document . uri ;
221
- await vscode . commands . executeCommand ( "workbench.action.closeActiveEditor" ) ;
222
- await vscode . commands . executeCommand ( "vscode.openWith" , uri , "PowerShellNotebookMode" ) ;
223
- }
250
+ class PowerShellNotebookKernel implements vscode . NotebookKernel , vscode . NotebookKernelProvider {
251
+ public id ?: string ;
252
+ public label : string = "PowerShell" ;
253
+ public description ?: string = "The PowerShell Notebook Mode kernel that runs commands in the PowerShell Integrated Console." ;
254
+ public isPreferred ?: boolean ;
255
+ public preloads ?: vscode . Uri [ ] ;
224
256
225
- private static async hideNotebookMode ( ) {
226
- const uri = vscode . notebook . activeNotebookEditor . document . uri ;
227
- await vscode . commands . executeCommand ( "workbench.action.closeActiveEditor" ) ;
228
- await vscode . commands . executeCommand ( "vscode.openWith" , uri , "default" ) ;
229
- }
257
+ private languageClient : LanguageClient ;
230
258
231
- /*
232
- `vscode.NotebookKernel` implementations
233
- */
234
- public async executeAllCells ( document : vscode . NotebookDocument , token : vscode . CancellationToken ) : Promise < void > {
259
+ public async executeAllCells ( document : vscode . NotebookDocument ) : Promise < void > {
235
260
for ( const cell of document . cells ) {
236
- await this . executeCell ( document , cell , token ) ;
261
+ if ( cell . cellKind === vscode . CellKind . Code ) {
262
+ await this . executeCell ( document , cell ) ;
263
+ }
237
264
}
238
265
}
239
266
240
- public async executeCell ( document : vscode . NotebookDocument , cell : vscode . NotebookCell | undefined , token : vscode . CancellationToken ) : Promise < void > {
241
- if ( token . isCancellationRequested ) {
242
- return ;
243
- }
244
-
267
+ public async executeCell ( document : vscode . NotebookDocument , cell : vscode . NotebookCell | undefined ) : Promise < void > {
245
268
await this . languageClient . sendRequest ( EvaluateRequestType , {
246
269
expression : cell . document . getText ( ) ,
247
270
} ) ;
248
271
}
272
+
273
+ // Since executing a cell is a "fire and forget", there's no time for the user to cancel
274
+ // any of the executing cells. We can bring this in after PSES has a better API for executing code.
275
+ public cancelCellExecution ( document : vscode . NotebookDocument , cell : vscode . NotebookCell ) : void {
276
+ return ;
277
+ }
278
+
279
+ // Since executing a cell is a "fire and forget", there's no time for the user to cancel
280
+ // any of the executing cells. We can bring this in after PSES has a better API for executing code.
281
+ public cancelAllCellsExecution ( document : vscode . NotebookDocument ) : void {
282
+ return ;
283
+ }
284
+
285
+ public setLanguageClient ( languageClient : LanguageClient ) {
286
+ this . languageClient = languageClient ;
287
+ }
288
+
289
+ /*
290
+ vscode.NotebookKernelProvider implementation
291
+ */
292
+ public provideKernels ( document : vscode . NotebookDocument , token : vscode . CancellationToken ) : vscode . ProviderResult < vscode . NotebookKernel [ ] > {
293
+ return [ this ] ;
294
+ }
249
295
}
0 commit comments