forked from PowerShell/PSReadLine
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGenerateFunctionHelp.ps1
More file actions
212 lines (174 loc) · 6.5 KB
/
GenerateFunctionHelp.ps1
File metadata and controls
212 lines (174 loc) · 6.5 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
param(
$Configuration = 'Release',
[Parameter(Mandatory)]
$OutFile
)
$errorActionPreference = "Stop"
$ourAssembly = "$PSScriptRoot\PSReadLine\bin\$Configuration\Microsoft.PowerShell.PSReadLine2.dll"
$t ='Microsoft.PowerShell.PSConsoleReadLine' -as [type]
if ($null -ne $t -and $t.Assembly.Location -ne $ourAssembly)
{
# Make sure we're runnning in a non-interactive session by relaunching
powershell -NoProfile -NonInteractive -File $PSCommandPath $Configuration $OutFile
exit $LASTEXITCODE
}
try {
Import-Module "$PSScriptRoot\bin\$Configuration\PSReadLine\PSReadLine.psd1"
$helpContent = [xml](Get-Content "$PSScriptRoot\PSReadLine\bin\$Configuration\Microsoft.PowerShell.PSReadLine.xml")
Set-PSReadLineOption -EditMode Windows
$cmdKeyBindings = Get-PSReadLineKeyHandler -Bound
Set-PSReadLineOption -EditMode Emacs
$emacsKeyBindings = Get-PSReadLineKeyHandler -Bound
Set-PSReadLineOption -EditMode Vi
$viKeyBindings = Get-PSReadLineKeyHandler -Bound
$bindableFunctionsFromReflection = ((Get-Command Set-PSReadLineKeyHandler).Parameters['Function'].Attributes).ValidValues
[enum]::GetNames([Microsoft.PowerShell.KeyHandlerGroup]) |
ForEach-Object { '{0},{1}' -f $_,[Microsoft.PowerShell.KeyHandler]::GetGroupingDescription($_) } |
ForEach-Object { $groupingDescriptions = @{} } { $k,$v = $_ -split ',',2; $groupingDescriptions[$k] = $v }
class BindableFunction
{
[string]$Function
[string]$Description
[string[]]$CmdBindings = @()
[string[]]$EmacsBindings = @()
[string[]]$ViInsBindings = @()
[string[]]$ViCmdBindings = @()
# Can't use the following type because it's not loaded at parse time
<#[Microsoft.PowerShell.KeyHandlerGroup]#>[object]$Group
}
$allFunctions = @{}
foreach ($fn in $bindableFunctionsFromReflection)
{
$bindableFn = [BindableFunction]::new()
$bindableFn.Function = $fn
$bindableFn.Group = [Microsoft.PowerShell.PSConsoleReadLine]::GetDisplayGrouping($fn)
$allFunctions[$fn] = $bindableFn
}
foreach ($member in $helpContent.doc.members.member)
{
$re = [regex]'^M:Microsoft\.PowerShell\.PSConsoleReadLine\.([a-zA-Z]+)\(System\.Nullable\{System\.ConsoleKeyInfo\},System.Object\)$'
$name = $member.GetAttribute('name')
if ($name -match $re)
{
$fn = $matches[1]
$bindableFn = $allFunctions[$fn]
if ($null -eq $bindableFn) { continue }
$summary = $member.summary
if ($summary -is [System.Xml.XmlElement])
{
# Special case to handle a cref, this is not generic code
$parts = foreach ($c in $summary.ChildNodes)
{
if ($c -is [System.Xml.XmlText])
{
$c.InnerText
}
elseif ($c -is [System.Xml.XmlElement])
{
$null = $c.cref -match $re
$matches[1]
}
}
$summary = $parts -join ''
}
$bindableFn.Description = $summary.Trim()
}
}
foreach ($binding in $cmdKeyBindings)
{
$allFunctions[$binding.Function].CmdBindings += $binding.Key
}
foreach ($binding in $emacsKeyBindings)
{
$allFunctions[$binding.Function].EmacsBindings += $binding.Key
}
foreach ($binding in $viKeyBindings)
{
$mode = if ($binding.Key[0] -eq '<') { 'ViCmdBindings' } else { 'ViInsBindings' }
# Some vi functions are private but are stil reported by Get-PSReadLineKeyHandler
$bindableFn = $allFunctions[$binding.Function]
if ($null -ne $bindableFn)
{
$bindableFn.$mode += $binding.Key
}
}
$indent = " "
$maxWidth = 90
$allFunctions.Values |
Group-Object -Property Group |
Sort-Object {[Microsoft.PowerShell.KeyHandlerGroup]$_.Name} |
ForEach-Object {
$label = $groupingDescriptions[$_.Name]
" {0}`n {1}`n" -f $label, ('-' * $label.Length)
$_.Group | Sort-Object Function | ForEach-Object {
$desc = $_.Description -replace "`n",'' -replace " +"," "
if ($desc.Length - $indent.Length -gt $maxWidth)
{
# Wrap the text
$sb = [System.Text.StringBuilder]::new()
$col = $indent.Length
$words = $desc -split ' '
foreach ($word in $words)
{
if ($col + $word.Length -gt $maxWidth)
{
$null = $sb.Append("`n$indent")
$col = $indent.Length
}
$col += ($word.Length + 1)
$null = $sb.Append($word)
$null = $sb.Append(" ")
}
$null = $sb.Remove($sb.Length - 1, 1)
$desc = $sb.ToString()
}
$sb = [System.Text.StringBuilder]::new()
$null = if ($_.CmdBindings.Length -gt 0) {
$sb.Append($indent)
$sb.Append("Cmd: <")
$sb.Append($_.CmdBindings -join '>, <')
$sb.Append('>')
}
$null = if ($_.EmacsBindings.Length -gt 0) {
if ($sb.Length -gt 0) { $sb.AppendLine() }
$sb.Append($indent)
$sb.Append("Emacs: <")
$sb.Append($_.EmacsBindings -join '>, <')
$sb.Append('>')
}
$null = if ($_.ViInsBindings.Length -gt 0) {
if ($sb.Length -gt 0) { $sb.AppendLine() }
$sb.Append($indent)
$sb.Append("Vi insert mode: <")
$sb.Append($_.ViInsBindings -join '>, <')
$sb.Append('>')
}
$null = if ($_.ViCmdBindings.Length -gt 0) {
if ($sb.Length -gt 0) { $sb.AppendLine() }
$sb.Append($indent)
$sb.Append("Vi command mode: ")
$sb.Append($_.ViCmdBindings -join ', ')
}
if ($sb.Length -eq 0)
{
$bindings = "${indent}Function is unbound."
}
else {
$bindings = $sb.ToString()
}
" {0}:`n`n{1}{2}`n`n{3}`n" -f $_.Function, $indent, $desc, $bindings
}
} | Out-File -LiteralPath $OutFile -Encoding ascii
} catch
{
& {
[Microsoft.PowerShell.PSConsoleReadLine].Assembly.Location | Out-String
$_ | Out-String
$_.Exception | Out-String
$_.Exception.StackTrace | Out-String
$_.Exception.InnerException | Out-String
$_.Exception.InnerException.StackTrace | Out-String
} | Out-Host
exit 1
}
exit 0