Skip to content

Fix New-EditorFile failing when no Editor window open #1411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -125,7 +125,16 @@ 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...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely appears like it would work, so I don't mind the approach. I would like to know if there's a way to test if an editor context exists (if so we can avoid exception handling), and if creating a new file on the workspace is the best way to generate a new editor context. If this is the only approach, then I feel like our interface is lacking because as someone new to the code, I would have expected something like:

  • GetEditorContext() (which exists)
  • HasEditorContext() (essentially says if the above would throw)
  • NewEditorContext() (to create one, instead of relying what I presume is a side-effect of Workspace.NewFile())

$psEditor.Workspace.NewFile()
$editorContext = $psEditor.GetEditorContext()
}

if (!$editorContext) {
return
}
Expand Down