Public/Expand-Expression.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using namespace Microsoft.PowerShell.EditorServices.Extensions
using namespace System.Management.Automation
using namespace System.Management.Automation.Language

function Expand-Expression {
    <#
    .EXTERNALHELP EditorServicesCommandSuite-help.xml
    #>

    [EditorCommand(DisplayName='Expand Selection Text to Output')]
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [ValidateNotNullOrEmpty()]
        [Alias('Extent')]
        [System.Management.Automation.Language.IScriptExtent[]]
        $InputObject = ($psEditor.GetEditorContext().SelectedRange | ConvertTo-ScriptExtent)
    )
    process {
        foreach ($object in $InputObject) {
            if ([string]::IsNullOrWhiteSpace($object.Text)) {
                $message = $Strings.ExpandEmptyExtent -f $object.StartOffset, $object.File
                ThrowError -Exception ([InvalidOperationException]::new($message)) `
                           -Id        ExpandEmptyExtent `
                           -Category  InvalidOperation `
                           -Target    $object `
                           -Show
            }
            $parseErrors = $null
            $null = [Parser]::ParseInput(
                <# input: #> $object.Text,
                <# tokens: #> [ref]$null,
                <# errors: #> [ref]$parseErrors
            )
            if ($parseErrors) {
                ThrowError -Exception ([ParseException]::new($parseErrors)) `
                           -Id        ExpandExpressionParseError `
                           -Category  InvalidArgument `
                           -Target    $object `
                           -Show
            }
            try {
                $output = & ([scriptblock]::Create($object.Text)) | Out-String
            } catch {
                ThrowError -ErrorRecord $PSItem -Show
            }

            Set-ScriptExtent -Extent $object -Text $output
        }
    }
}