From 054ca6c29a7338223dd7493cddda54da9ce3ad2b Mon Sep 17 00:00:00 2001 From: corbob Date: Fri, 12 Feb 2021 19:52:55 -0800 Subject: [PATCH 1/3] Fix when there is no editor window open. --- .../Commands/Public/CmdletInterface.ps1 | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/module/PowerShellEditorServices/Commands/Public/CmdletInterface.ps1 b/module/PowerShellEditorServices/Commands/Public/CmdletInterface.ps1 index 17b36165c..d8b2ffcf2 100644 --- a/module/PowerShellEditorServices/Commands/Public/CmdletInterface.ps1 +++ b/module/PowerShellEditorServices/Commands/Public/CmdletInterface.ps1 @@ -125,7 +125,15 @@ function New-EditorFile { end { # If editorContext is null, then we're in a Temp session and # this cmdlet won't work so return early. - $editorContext = $psEditor.GetEditorContext() + try { + $editorContext = $psEditor.GetEditorContext() + } + catch { + # If there's no editor, this throws an error. Create a new file, and grab the context here. + # This feels really hacky way to do it, but not sure if there's another way to detect editor context... + $psEditor.Workspace.NewFile() + $editorContext = $psEditor.GetEditorContext() + } if (!$editorContext) { return } From f228e26dee96d53ae5015c2cc92952434d10fa87 Mon Sep 17 00:00:00 2001 From: corbob Date: Fri, 12 Feb 2021 19:54:26 -0800 Subject: [PATCH 2/3] Update help to correct filename --- .../Commands/Public/CmdletInterface.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/module/PowerShellEditorServices/Commands/Public/CmdletInterface.ps1 b/module/PowerShellEditorServices/Commands/Public/CmdletInterface.ps1 index d8b2ffcf2..d4d6a37ef 100644 --- a/module/PowerShellEditorServices/Commands/Public/CmdletInterface.ps1 +++ b/module/PowerShellEditorServices/Commands/Public/CmdletInterface.ps1 @@ -86,10 +86,10 @@ function Unregister-EditorCommand { Creates and opens a new foo.ps1 in your editor .EXAMPLE PS > Get-Process | New-EditorFile proc.txt - Creates and opens a new foo.ps1 in your editor with the contents of the call to Get-Process + Creates and opens a new proc.txt in your editor with the contents of the call to Get-Process .EXAMPLE PS > Get-Process | New-EditorFile proc.txt -Force - Creates and opens a new foo.ps1 in your editor with the contents of the call to Get-Process. Overwrites the file if it already exists + Creates and opens a new proc.txt in your editor with the contents of the call to Get-Process. Overwrites the file if it already exists .INPUTS Path an array of files you want to open in your editor From 3b917d65d15ab5d5f4327571d19bace4dc1d6fd4 Mon Sep 17 00:00:00 2001 From: corbob <30301021+corbob@users.noreply.github.com> Date: Tue, 16 Feb 2021 23:51:58 +0000 Subject: [PATCH 3/3] Update module/PowerShellEditorServices/Commands/Public/CmdletInterface.ps1 Co-authored-by: Robert Holt --- .../PowerShellEditorServices/Commands/Public/CmdletInterface.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/module/PowerShellEditorServices/Commands/Public/CmdletInterface.ps1 b/module/PowerShellEditorServices/Commands/Public/CmdletInterface.ps1 index d4d6a37ef..84d612f80 100644 --- a/module/PowerShellEditorServices/Commands/Public/CmdletInterface.ps1 +++ b/module/PowerShellEditorServices/Commands/Public/CmdletInterface.ps1 @@ -134,6 +134,7 @@ function New-EditorFile { $psEditor.Workspace.NewFile() $editorContext = $psEditor.GetEditorContext() } + if (!$editorContext) { return }