@@ -34,13 +34,40 @@ export class PowerShellNotebooksFeature extends LanguageClientConsumer {
34
34
}
35
35
36
36
public registerNotebookProviders ( ) {
37
+ const options = {
38
+ transientOutputs : true ,
39
+ transientMetadata : {
40
+ inputCollapsed : true ,
41
+ outputCollapsed : true ,
42
+ runState : true ,
43
+ runStartTime : true ,
44
+ executionOrder : true ,
45
+ lastRunDuration : true ,
46
+ statusMessage : true ,
47
+ } ,
48
+ } ;
49
+
50
+ // Until vscode supports using the same view type with different priority,
51
+ // we register 2 of the same viewTypes.
52
+ // This one is used to open *.Notebook.ps1 files which automatically go straight to Notebook mode.
53
+ this . disposables . push ( vscode . notebook . registerNotebookKernelProvider ( {
54
+ viewType : "PowerShellNotebookModeDefault"
55
+ } , this . notebookKernel ) ) ;
56
+
57
+ this . disposables . push ( vscode . notebook . registerNotebookContentProvider (
58
+ "PowerShellNotebookModeDefault" ,
59
+ this . notebookContentProvider ,
60
+ options ) ) ;
61
+
62
+ // This one is used to open *.ps1 files which will be opened in the default text editor first.
37
63
this . disposables . push ( vscode . notebook . registerNotebookKernelProvider ( {
38
- viewType : "PowerShellNotebookMode "
64
+ viewType : "PowerShellNotebookModeOption "
39
65
} , this . notebookKernel ) ) ;
40
66
41
67
this . disposables . push ( vscode . notebook . registerNotebookContentProvider (
42
- "PowerShellNotebookMode" ,
43
- this . notebookContentProvider ) ) ;
68
+ "PowerShellNotebookModeOption" ,
69
+ this . notebookContentProvider ,
70
+ options ) ) ;
44
71
}
45
72
46
73
public dispose ( ) {
@@ -55,8 +82,17 @@ export class PowerShellNotebooksFeature extends LanguageClientConsumer {
55
82
56
83
private static async EnableNotebookMode ( ) {
57
84
const uri = vscode . window . activeTextEditor . document . uri ;
58
- await vscode . commands . executeCommand ( "workbench.action.closeActiveEditor" ) ;
59
- await vscode . commands . executeCommand ( "vscode.openWith" , uri , "PowerShellNotebookMode" ) ;
85
+
86
+ // If the file is an untitled file, then we can't close it.
87
+ if ( ! vscode . window . activeTextEditor . document . isUntitled ) {
88
+ await vscode . commands . executeCommand ( "workbench.action.closeActiveEditor" ) ;
89
+ }
90
+
91
+ if ( uri . fsPath ?. endsWith ( ".Notebook.ps1" ) ) {
92
+ await vscode . commands . executeCommand ( "vscode.openWith" , uri , "PowerShellNotebookModeDefault" ) ;
93
+ } else {
94
+ await vscode . commands . executeCommand ( "vscode.openWith" , uri , "PowerShellNotebookModeOption" ) ;
95
+ }
60
96
}
61
97
62
98
private static async DisableNotebookMode ( ) {
@@ -95,8 +131,12 @@ class PowerShellNotebookContentProvider implements vscode.NotebookContentProvide
95
131
// load from backup if needed.
96
132
const actualUri = context . backupId ? vscode . Uri . parse ( context . backupId ) : uri ;
97
133
this . logger . writeDiagnostic ( `Opening Notebook: ${ uri . toString ( ) } ` ) ;
134
+ const isUntitled = uri . scheme !== "file" ;
98
135
99
- const data = ( await vscode . workspace . fs . readFile ( actualUri ) ) . toString ( ) ;
136
+ // If we have an untitled file, get the contents from vscode instead of the file system.
137
+ const data : string = isUntitled
138
+ ? ( await vscode . workspace . openTextDocument ( actualUri ) ) . getText ( )
139
+ : ( await vscode . workspace . fs . readFile ( actualUri ) ) . toString ( ) ;
100
140
101
141
let lines : string [ ] ;
102
142
// store the line ending in the metadata of the document
@@ -117,6 +157,7 @@ class PowerShellNotebookContentProvider implements vscode.NotebookContentProvide
117
157
metadata : {
118
158
custom : {
119
159
lineEnding,
160
+ isUntitled,
120
161
}
121
162
}
122
163
} ;
@@ -374,6 +415,11 @@ class PowerShellNotebookKernel implements vscode.NotebookKernel, vscode.Notebook
374
415
await this . languageClient . sendRequest ( EvaluateRequestType , {
375
416
expression : cell . document . getText ( ) ,
376
417
} ) ;
418
+
419
+ // Show the integrated console if it isn't already visible and
420
+ // scroll terminal to bottom so new output is visible
421
+ await vscode . commands . executeCommand ( "PowerShell.ShowSessionConsole" , true ) ;
422
+ await vscode . commands . executeCommand ( "workbench.action.terminal.scrollToBottom" ) ;
377
423
}
378
424
379
425
// Since executing a cell is a "fire and forget", there's no time for the user to cancel
0 commit comments