@@ -34,13 +34,40 @@ export class PowerShellNotebooksFeature extends LanguageClientConsumer {
3434 }
3535
3636 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.
3763 this . disposables . push ( vscode . notebook . registerNotebookKernelProvider ( {
38- viewType : "PowerShellNotebookMode "
64+ viewType : "PowerShellNotebookModeOption "
3965 } , this . notebookKernel ) ) ;
4066
4167 this . disposables . push ( vscode . notebook . registerNotebookContentProvider (
42- "PowerShellNotebookMode" ,
43- this . notebookContentProvider ) ) ;
68+ "PowerShellNotebookModeOption" ,
69+ this . notebookContentProvider ,
70+ options ) ) ;
4471 }
4572
4673 public dispose ( ) {
@@ -55,8 +82,17 @@ export class PowerShellNotebooksFeature extends LanguageClientConsumer {
5582
5683 private static async EnableNotebookMode ( ) {
5784 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+ }
6096 }
6197
6298 private static async DisableNotebookMode ( ) {
@@ -95,8 +131,12 @@ class PowerShellNotebookContentProvider implements vscode.NotebookContentProvide
95131 // load from backup if needed.
96132 const actualUri = context . backupId ? vscode . Uri . parse ( context . backupId ) : uri ;
97133 this . logger . writeDiagnostic ( `Opening Notebook: ${ uri . toString ( ) } ` ) ;
134+ const isUntitled = uri . scheme !== "file" ;
98135
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 ( ) ;
100140
101141 let lines : string [ ] ;
102142 // store the line ending in the metadata of the document
@@ -117,6 +157,7 @@ class PowerShellNotebookContentProvider implements vscode.NotebookContentProvide
117157 metadata : {
118158 custom : {
119159 lineEnding,
160+ isUntitled,
120161 }
121162 }
122163 } ;
@@ -374,6 +415,11 @@ class PowerShellNotebookKernel implements vscode.NotebookKernel, vscode.Notebook
374415 await this . languageClient . sendRequest ( EvaluateRequestType , {
375416 expression : cell . document . getText ( ) ,
376417 } ) ;
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" ) ;
377423 }
378424
379425 // Since executing a cell is a "fire and forget", there's no time for the user to cancel
0 commit comments