Skip to content

Commit

Permalink
Added AsNewFile switch to Out-CurrentFile (#869)
Browse files Browse the repository at this point in the history
* 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 <finked@hotmail.com>
  • Loading branch information
dfinke authored and TylerLeonhardt committed Feb 19, 2019
1 parent db09f56 commit 1a86d4b
Showing 1 changed file with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,32 @@ function Out-CurrentFile {
#>
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline, Mandatory=$true)]
[Switch]$AsNewFile,

[Parameter(ValueFromPipeline, Mandatory = $true)]
$InputObject
)

Begin { $objectsToWrite = @() }
Process { $objectsToWrite += $InputObject }
End {

# If requested, create a new file
if ($AsNewFile) {
$psEditor.Workspace.NewFile()
}

$outputString = "@`"`r`n{0}`r`n`"@" -f ($objectsToWrite|out-string).Trim()

try {
# If there is no file open
$psEditor.GetEditorContext()
}
catch {
# create a new one
$psEditor.Workspace.NewFile()
}

$psEditor.GetEditorContext().CurrentFile.InsertText($outputString)
}
}

0 comments on commit 1a86d4b

Please sign in to comment.