Public/Expand-TypeImplementation.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using namespace Microsoft.PowerShell.EditorServices.Extensions
using namespace System.Management.Automation.Language

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

    [EditorCommand(DisplayName='Expand Closest Type to Implementation')]
    [CmdletBinding()]
    param(
        [Parameter(Position=0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [ValidateNotNullOrEmpty()]
        [type[]]
        $Type
    )
    begin {
        $renderer = [TypeRenderer]::new()
        $group = @'
class(FullName, Name, DeclaredMethods) ::= <<
class New<Name> : <FullName> {
    <DeclaredMethods:methods(); separator={<\n><\n>}>
}
 
>>
methods(m) ::= <<
<m.ReturnType> <if(m.IsStatic)>static <endif><m.Name> (<m.Parameters:params(); separator=", ">) {
    throw [NotImplementedException]::new()
}
>>
params(p) ::= "<p.ParameterType> $<p.Name>"
'@

        $group    = New-StringTemplateGroup -Definition $group
        $instance = $group.GetType().GetProperty('Instance', 60).GetValue($group)

        $instance.RegisterRenderer([type], $renderer)
    }
    process {
        $typeList
        if ($Type) {
            $cursorPosition = $psEditor.GetEditorContext().CursorPosition
            # HACK: Temporary workaround until https://github.com/PowerShell/PowerShellEditorServices/pull/541
            #$targetExtent = $psEditor.GetEditorContext().CursorPosition | ConvertTo-ScriptExtent
            $targetExtent = [Microsoft.PowerShell.EditorServices.FullScriptExtent]::new(
                $psEditor.GetEditorContext().CurrentFile,
                [Microsoft.PowerShell.EditorServices.BufferRange]::new(
                    $cursorPosition.Line,
                    $cursorPosition.Column,
                    $cursorPosition.Line,
                    $cursorPosition.Column))
        } else {
            $ast = Find-Ast -AtCursor |
                Find-Ast -Family -First -IncludeStartingAst { $PSItem.TypeName }

            $targetExtent = $ast.Extent

            $resolvedType = $ast.TypeName.Name -as [type]

            if (-not $resolvedType) {
                # Type resolution scope is this function, so we need to pull the namespaces from
                # the current file and test against that.
                $using = Find-Ast { $_ -is [UsingStatementAst] -and
                                    $_.UsingStatementKind -eq 'Namespace' }

                foreach ($namespace in $using.Name) {
                    if ($resolvedType = ('{0}.{1}' -f $using.Name, $ast.TypeName.Name) -as [type]) {
                        break
                    }
                }
            }
            $Type = $resolvedType
        }
        $result = foreach ($aType in $Type) {
            Invoke-StringTemplate -Group $group -Name class -Parameters ($aType)
        }
        Set-ScriptExtent -Extent $targetExtent -Text $result
    }
}