Skip to content

Commit

Permalink
Adding Reflection Formatter (Fixes #15)
Browse files Browse the repository at this point in the history
  • Loading branch information
James Brundage committed Jul 20, 2023
1 parent 23807ca commit 6ae0841
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 0 deletions.
64 changes: 64 additions & 0 deletions Formatting/Reflection/System.Type.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
Write-FormatView -TypeName System.Type -Property FullName, BaseType, IsPublic, IsSerializable -AutoSize

Write-FormatView -TypeName System.Type -Action {
Write-FormatViewExpression -Newline
Write-FormatViewExpression -ScriptBlock { '-' * ($Host.UI.RawUI.BufferSize.Width - 1) }
Write-FormatViewExpression -ScriptBlock { ' ' * 1 }
Write-FormatViewExpression -ForegroundColor 'Verbose' -ScriptBlock { $_ } -ControlName TypeNameControl
Write-FormatViewExpression -ScriptBlock { ' ' * 1 }
Write-FormatViewExpression -If { $_.BaseType -and -not $_.IsValueType } -ScriptBlock {
':'
}
Write-FormatViewExpression -If { $_.BaseType -and -not $_.IsValueType -and $_.BaseType -ne [Object] } -Property BaseType -ControlName TypeBase
Write-FormatViewExpression -If { $_.GetInterfaces() } -ScriptBlock { $_.GetInterfaces() | Sort-Object Name} -Enumerate -ControlName TypeBase
} -GroupLabel 'Type Summary' -GroupByScript { '| Format-Custom -View System.Type.Full for more'} -Name System.Type.Summary

Write-FormatView -TypeName System.Type -Action {
Write-FormatViewExpression -Newline
Write-FormatViewExpression -ScriptBlock { '-' * ($Host.UI.RawUI.BufferSize.Width - 1) }
Write-FormatViewExpression -ScriptBlock { ' ' * 1 }
Write-FormatViewExpression -ForegroundColor 'Verbose' -ScriptBlock { $_ } -ControlName TypeNameControl
Write-FormatViewExpression -ScriptBlock { ' ' * 1 }
Write-FormatViewExpression -If { $_.BaseType -and -not $_.IsValueType -and $_.BaseType -ne [Object] } -ScriptBlock {
':'
}
Write-FormatViewExpression -If { $_.BaseType -and -not $_.IsValueType -and $_.BaseType -ne [Object] } -Property BaseType -ControlName TypeBase
Write-FormatViewExpression -If { $_.GetInterfaces() } -ScriptBlock { $_.GetInterfaces() | Sort-Object Name} -Enumerate -ControlName TypeBase

Write-FormatViewExpression -If { $_.GetConstructors('Instance,Public') } -ScriptBlock {
[Environment]::NewLine + ('#' * 3) + ' Constructors:'
}
Write-FormatViewExpression -If { $_.GetConstructors('Instance,Public') } -ScriptBlock {
$_.GetConstructors('Instance,Public')
} -Enumerate -ControlName TypeMethodControl
Write-FormatViewExpression -If { $_.GetEvents('Instance,Public') } -ScriptBlock {
[Environment]::NewLine + ('#' * 3) + ' Events:'
}
Write-FormatViewExpression -If { $_.GetEvents('Instance,Public') } -ScriptBlock {
$_.GetEvents('Instance,Public') | Sort-Object Name
} -Enumerate -ControlName TypeEventControl
Write-FormatViewExpression -If { $_.GetProperties('Static,Public') } -ScriptBlock {
[Environment]::NewLine + ('#' * 3) + ' Static Properties:'
}
Write-FormatViewExpression -If { $_.GetProperties('Static,Public')} -ScriptBlock {
$_.GetProperties('Static,Public') | Sort-Object Name
} -Enumerate -ControlName TypePropertyControl
Write-FormatViewExpression -If { $_.GetProperties('Instance,Public') } -ScriptBlock {
[Environment]::NewLine + ('#' * 3) + ' Properties:'
}
Write-FormatViewExpression -If { $_.GetProperties('Instance,Public')} -ScriptBlock {
$_.GetProperties('Instance,Public') | Sort-Object Name
} -Enumerate -ControlName TypePropertyControl
Write-FormatViewExpression -If { $_.GetMethods('Static,Public') } -ScriptBlock {
[Environment]::NewLine + ('#' * 3) + ' Static Methods:'
}
Write-FormatViewExpression -If { $_.GetMethods('Static,Public') } -ScriptBlock {
$_.GetMethods('Static,Public') | Sort-Object Name | Where-Object { -not $_.IsSpecialName }
} -Enumerate -ControlName TypeMethodControl
Write-FormatViewExpression -If { $_.GetMethods('Instance,Public') } -ScriptBlock {
[Environment]::NewLine + ('#' * 3) + ' Methods:'
}
Write-FormatViewExpression -If { $_.GetMethods('Instance,Public') } -ScriptBlock {
$_.GetMethods('Instance,Public') | Sort-Object Name | Where-Object { -not $_.IsSpecialName }
} -Enumerate -ControlName TypeMethodControl
} -Name System.Type.Full
5 changes: 5 additions & 0 deletions Formatting/Reflection/TypeBaseControl.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Write-FormatView -Action {
Write-FormatViewExpression -Newline
Write-FormatViewExpression -ScriptBlock { ' *' }
Write-FormatViewExpression -ControlName TypeNameControl -ScriptBlock {$_ } -Style 'Foreground.Cyan', 'Bold'
} -TypeName TypeBase -Name TypeBase -AsControl
15 changes: 15 additions & 0 deletions Formatting/Reflection/TypeEventControl.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Write-FormatView -Action {
Write-FormatViewExpression -If { -not $script:DisplayingMember } -ScriptBlock { [Environment]::NewLine }
Write-FormatViewExpression -ScriptBlock { ' * ' }
Write-FormatViewExpression -Property Name -Style 'Formatting.Warning'
Write-FormatViewExpression -Text '('
Write-FormatViewExpression -If { $_.EventHandlerType.GetMethod('Invoke') } -ScriptBlock {
$MethodParameters = @($_.EventHandlerType.GetMethod('Invoke').GetParameters())
foreach ($n in 0..($MethodParameters.Count - 1)) {
$o =[PSObject]::new($MethodParameters[$n])
$o.psobject.properties.add([PSNoteProperty]::new('N', $N))
$o
}
} -ControlName TypeMethodParameterControl -Enumerate
Write-FormatViewExpression -Text ')'
} -TypeName TypeEventControl -Name TypeEventControl -AsControl
6 changes: 6 additions & 0 deletions Formatting/Reflection/TypeFieldControl.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Write-FormatControl -Name TypeFieldControl -Action {
Write-FormatViewExpression -If { -not $script:DisplayingMember } -ScriptBlock { [Environment]::NewLine }
Write-FormatViewExpression -ScriptBlock { ' * '}
Write-FormatViewExpression -Property FieldType -ControlName TypeNameControl -Style 'Foreground.Cyan', 'Bold'
Write-FormatViewExpression -Property Name -Style 'Formatting.Warning'
}
12 changes: 12 additions & 0 deletions Formatting/Reflection/TypeMemberControl.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Write-FormatControl -Name TypeMemberControl -Action {
Write-FormatViewExpression -If {
'RuntimeMethodInfo', 'RuntimeConstructorInfo' -contains $_.GetType().Name
} -ScriptBlock {$_ } -ControlName TypeMethodControl
Write-FormatViewExpression -If { $_.GetType().Name -eq 'RuntimePropertyInfo' } -ScriptBlock {$_ } -ControlName TypePropertyControl
Write-FormatViewExpression -If {

'MdFieldInfo', 'RtFieldInfo' -contains $_.GetType().Name
} -ScriptBlock {$_ } -ControlName TypeFieldControl
Write-FormatViewExpression -If { $_.GetType().Name -eq 'RuntimeEventInfo' } -ScriptBlock {$_ } -ControlName TypeEventControl
}

19 changes: 19 additions & 0 deletions Formatting/Reflection/TypeMethodControl.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Write-FormatView -Action {
Write-FormatViewExpression -If { -not $script:DisplayingMember } -ScriptBlock { [Environment]::NewLine }
Write-FormatViewExpression -ScriptBlock { ' *' }
Write-FormatViewExpression -If { $_.IsStatic } -ScriptBlock { ' static ' }
Write-FormatViewExpression -If {$_.IsConstructor } -ScriptBlock { $_.DeclaringType } -ControlName TypeNameControl -Style 'Foreground.Cyan','Bold'
Write-FormatViewExpression -If { -not $_.IsConstructor -and $_.ReturnType } -ScriptBlock { $_.ReturnType } -ControlName TypeNameControl -Style 'Foreground.Cyan','Bold'
Write-FormatViewExpression -If { -not $_.IsConstructor } -ScriptBlock { ' ' + $_.Name } -Style 'Formatting.Warning'
Write-FormatViewExpression -ScriptBlock { ' (' }
Write-FormatViewExpression -ScriptBlock {
$MethodParameters = @($_.GetParameters())
foreach ($n in 0..($MethodParameters.Count - 1)) {
$o =[PSObject]::new($MethodParameters[$n])
$o.psobject.properties.add([PSNoteProperty]::new('N', $N))
$o
}
} -ControlName TypeMethodParameterControl -Enumerate
Write-FormatViewExpression -ScriptBlock { ')' }

} -TypeName TypeMethodControl -Name TypeMethodControl -AsControl
6 changes: 6 additions & 0 deletions Formatting/Reflection/TypeMethodParameterControl.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Write-FormatView -Action {
Write-FormatViewExpression -If { $_.N -gt 0} -ScriptBlock { ', ' }
Write-FormatViewExpression -Property ParameterType -ControlName TypeNameControl -Style 'Formatting.Verbose'
Write-FormatViewExpression -ScriptBlock { '$' + $_.Name } -Style 'Formatting.Warning'
Write-FormatViewExpression -ScriptBlock { ' ' }
} -TypeName TypeMethodParameterControl -Name TypeMethodParameterControl -AsControl
17 changes: 17 additions & 0 deletions Formatting/Reflection/TypeNameControl.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Write-FormatView -AsControl -Name TypeNameControl -Action {
Write-FormatViewExpression -Text ' ['
Write-FormatViewExpression -ScriptBlock {
if ($_.FullName) {
$_.Fullname -replace '`.+', '' -replace '^System\.', ''
} else {
$_.Name -replace '`.+', '' -replace '^System\.', ''
}
}

Write-FormatViewExpression -If {
$_.IsGenericType
} -ControlName TypeNameControl -ScriptBlock {
$_.GenericTypeArguments
} -Enumerate
Write-FormatViewExpression -Text ']'
} -TypeName TypeNameControl
12 changes: 12 additions & 0 deletions Formatting/Reflection/TypePropertyControl.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Write-FormatView -Action {
Write-FormatViewExpression -If { -not $script:DisplayingMember } -ScriptBlock { [Environment]::NewLine }
Write-FormatViewExpression -ScriptBlock { ' * '}
Write-FormatViewExpression -Property PropertyType -ControlName TypeNameControl -Style 'Foreground.Cyan','Bold'
Write-FormatViewExpression -Property Name -Style 'Formatting.Warning'
Write-FormatViewExpression -ScriptBlock {
' {' +
$(if ($_.CanRead) {'get;'}) +
$(if ($_.CanWrite) {'set;'})+
'}'
}
} -TypeName TypePropertyControl -Name TypePropertyControl -AsControl

0 comments on commit 6ae0841

Please sign in to comment.