Skip to content

Commit 4fa0346

Browse files
Merge pull request #1411 from corbob/GH-3180
Fix New-EditorFile failing when no Editor window open
2 parents 1ffe6db + 3b917d6 commit 4fa0346

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

module/PowerShellEditorServices/Commands/Public/CmdletInterface.ps1

+12-3
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ function Unregister-EditorCommand {
8686
Creates and opens a new foo.ps1 in your editor
8787
.EXAMPLE
8888
PS > Get-Process | New-EditorFile proc.txt
89-
Creates and opens a new foo.ps1 in your editor with the contents of the call to Get-Process
89+
Creates and opens a new proc.txt in your editor with the contents of the call to Get-Process
9090
.EXAMPLE
9191
PS > Get-Process | New-EditorFile proc.txt -Force
92-
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
92+
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
9393
.INPUTS
9494
Path
9595
an array of files you want to open in your editor
@@ -125,7 +125,16 @@ function New-EditorFile {
125125
end {
126126
# If editorContext is null, then we're in a Temp session and
127127
# this cmdlet won't work so return early.
128-
$editorContext = $psEditor.GetEditorContext()
128+
try {
129+
$editorContext = $psEditor.GetEditorContext()
130+
}
131+
catch {
132+
# If there's no editor, this throws an error. Create a new file, and grab the context here.
133+
# This feels really hacky way to do it, but not sure if there's another way to detect editor context...
134+
$psEditor.Workspace.NewFile()
135+
$editorContext = $psEditor.GetEditorContext()
136+
}
137+
129138
if (!$editorContext) {
130139
return
131140
}

0 commit comments

Comments
 (0)