Skip to content

Commit d6e8a23

Browse files
dfinkeTylerLeonhardt
authored andcommitted
Added AsNewFile switch to Out-CurrentFile (#869)
* Added `AsNewFile` switch, plus, if a file is not open, it creates a new one * Update module/PowerShellEditorServices/Commands/Public/Out-CurrentFile.ps1 Co-Authored-By: dfinke <[email protected]>
1 parent 262cb80 commit d6e8a23

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

Diff for: module/PowerShellEditorServices/Commands/Public/Out-CurrentFile.ps1

+19-1
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,32 @@ function Out-CurrentFile {
99
#>
1010
[CmdletBinding()]
1111
param(
12-
[Parameter(ValueFromPipeline, Mandatory=$true)]
12+
[Switch]$AsNewFile,
13+
14+
[Parameter(ValueFromPipeline, Mandatory = $true)]
1315
$InputObject
1416
)
1517

1618
Begin { $objectsToWrite = @() }
1719
Process { $objectsToWrite += $InputObject }
1820
End {
21+
22+
# If requested, create a new file
23+
if ($AsNewFile) {
24+
$psEditor.Workspace.NewFile()
25+
}
26+
1927
$outputString = "@`"`r`n{0}`r`n`"@" -f ($objectsToWrite|out-string).Trim()
28+
29+
try {
30+
# If there is no file open
31+
$psEditor.GetEditorContext()
32+
}
33+
catch {
34+
# create a new one
35+
$psEditor.Workspace.NewFile()
36+
}
37+
2038
$psEditor.GetEditorContext().CurrentFile.InsertText($outputString)
2139
}
2240
}

0 commit comments

Comments
 (0)