Microsoft.PowerShell.Core.ArgumentCompleters.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364

#
# .SYNOPSIS
#
# Complete the -Parameter argument to Get-Help
#
function HelpParameterNameCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $helpCommandName = $fakeBoundParameter['Name']

    if ($helpCommandName)
    {
        $parameters = (Get-Command -Name $helpCommandName).Parameters
        $parameters.Keys |
            Where-Object { $_ -like "$wordToComplete*" } |
            Sort-Object |
            ForEach-Object {
                # The tooltip will look similar to what shows up
                # in the syntax, e.g. [[-Name <string>]
                $md = $parameters.$_
                $positional = $false
                $mandatory = $true
                $isSwitch = $md.SwitchParameter
                foreach ($attr in $md.Attributes)
                {
                    if ($attr -is [Parameter])
                    {
                        if ($attr.Position -ge 0) { $positional = $true }
                        if (!$attr.Mandatory) { $mandatory = $false }
                    }
                }
                $tooltip = "-$_"
                if ($positional) { $tooltip = "[$tooltip]" }
                if (!$isSwitch) { $tooltip += " <$($md.ParameterType)>" }
                if (!$mandatory) { $tooltip = "[$tooltip]" }

                New-CompletionResult $_ $tooltip
            }
    }
}


#
# .SYNOPSIS
#
# Complete snapin name arguments
#
function GetSnapinCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    Get-PSSnapin $wordToComplete* |
        Sort-Object -Property Name |
        ForEach-Object {
            New-CompletionResult $_.Name $_.Description
        }
}


#
# .SYNOPSIS
#
# Complete snapin name arguments
#
function AddSnapinCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    Get-PSSnapin -Registered -Name "$wordToComplete*" |
        Sort-Object -Property Name |
        ForEach-Object {
            New-CompletionResult $_.Name $_.Description }
}


#
# .SYNOPSIS
#
# Complete verb arguments
#
function VerbCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    Get-Verb "$wordToComplete*" |
        ForEach-Object {
            New-CompletionResult $_.Verb ("Group: " + $_.Group)
        }
}


#
# .SYNOPSIS
#
# Complete Noun argument to Get-Command
#
function NounCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $optionalParam = @{}
    $module = $fakeBoundParameter['Module']
    if ($module)
    {
        $optionalParam.Module = $module
    }

    Get-Command -Noun $wordToComplete* @optionalParam |
        ForEach-Object {($_.Name -split '-',2)[1] } | Sort-Object -Unique | ForEach-Object {
            # TODO - is a decent tooltip possible?
            New-CompletionResult $_
    }
}


#
# .SYNOPSIS
#
# Complete session configuration name arguments
#
function PSSessionConfigurationNameCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    # These completions are pretty slow because they go through remoting to get the
    # remote configuration names.
    if ($fakeBoundParameter['ComputerName'])
    {
        Invoke-Command -ComputerName $fakeBoundParameter['ComputerName'] `
            { Get-PSSessionConfiguration -Name $using:wordToComplete* } |
            Sort-Object -Property Name |
            ForEach-Object {
                New-CompletionResult $_.Name ($_ | Out-String)
            }
    }
}


#
# .SYNOPSIS
#
# Completes -Version for Set-StrictMode
#
function SetStrictMode_VersionCompleter
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    '1.0', '2.0', '3.0', 'latest' | where { $_ -like "$wordToComplete*" } |
    ForEach-Object {
        New-CompletionResult $_ "Version $_"
    }
}


# .SYNOPSIS
#
# Complete the -Module argument to Save/Update-Help cmdlets
#
function HelpModuleCompleter
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    Microsoft.PowerShell.Core\Get-Module -ListAvailable -Name "$wordToComplete*" | Sort-Object Name | ForEach-Object {
        $tooltip = "Description: {0}`nModuleType: {1}`nPath: {2}" -f $_.Description, $_.ModuleType, $_.Path
        New-CompletionResult $_.Name $tooltip
    }
}


#
# .SYNOPSIS
#
# Completes the -Scope argument to the *-Variable, *-Alias, *-PSDrive
#
function ScopeParameterCompleter
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    echo Global Local Script Private | Where-Object {$_ -like "$wordToComplete*"} | ForEach-Object {
        New-CompletionResult $_ "Scope '$_'"
    }
}


#
# .SYNOPSIS
#
# Tab-completes names of help topics, also conceptual (about_*).
#
function HelpNameCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    # First - commands... but we need to leave All and Application out...
    $commands = @([System.Management.Automation.CompletionCompleters]::CompleteCommand(
        $wordToComplete,
        '*', # Any module
        $([enum]::GetNames(
            [System.Management.Automation.CommandTypes]
        ) | where { $_ -notin 'All', 'Application' })
    ))

    # Then - about_*.
    # About for main PS first...
    $psHomeHelpFiles = @(Get-ChildItem -Path $PSHOME\*\*.help.txt)

    # And for any other modules...
    $modulesHelpFiles = @(Get-Module | where ModuleBase -ne $PSHOME) |
        Get-ChildItem -Path { $_.ModuleBase } -Filter *.help.txt -Recurse

    $abouts = $psHomeHelpFiles + $modulesHelpFiles |
        Where-Object { $_.Name -like "$wordToComplete*" } |
        Sort-Object -Property Name |
        ForEach-Object {
            $text = $_.Name -replace '\.help\.txt'
            if ((Get-Content -Raw -Path $_.FullName) -replace '\s+', ' ' -match
                'SHORT DESCRIPTION ([\s\S]*?) LONG') {
                $toolTip = $Matches[1]
            } else {
                $toolTip = $text
            }
            New-CompletionResult $text $toolTip
        }

    # And last but not least - providers
    $providers = Get-PSProvider |
        Where-Object {
            $_.Name -like "$wordToComplete*" -and
            $_.HelpFile } |
        Sort-Object -Property Name |
        ForEach-Object {
            $toolTip = "Name: {0} Drives: {1}" -f $_.Name, $($_.Drives -join ", ")
            New-CompletionResult $_.Name $toolTip
        }

    # combine all to get mix of different types rather than FIFO with providers/ abouts at the end...
    $commands + $abouts + $providers | Sort-Object -Property ListItemText
}


# .SYNOPSIS
#
# Complete the -Name argument to Import-Module
#
function ImportModuleNameCompleter
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    if ($wordToComplete -and $wordToComplete.Contains('\'))
    {
        # Looks like a path.
        #
        # Let's complete directories first, then files that look like they could be modules.
        #
        # TODO: might be nice to not wipe out relative paths with full paths

        # Directories:
        Microsoft.PowerShell.Management\Get-ChildItem -Path "$wordToComplete*" -Directory | Sort-Object Name | ForEach-Object {
            New-CompletionResult ($_.FullName + '\')
        }

        # Files:
        Microsoft.PowerShell.Management\Get-ChildItem -Path "$wordToComplete*" -File -Include '*.ps*1', '*.dll' | Sort-Object Name | ForEach-Object {
            New-CompletionResult $_.FullName
        }
    }
    else
    {
        # Standard module name (for stuff already in $Env:PsModulePath).

        Microsoft.PowerShell.Core\Get-Module -ListAvailable -Name "$wordToComplete*" | Sort-Object Name | ForEach-Object {
            $tooltip = "Description: {0}`nModuleType: {1}`nPath: {2}" -f $_.Description,$_.ModuleType,$_.Path
            New-CompletionResult $_.Name $tooltip
        }
    }
}


Register-ArgumentCompleter `
    -Command ('help','Get-Help') `
    -Parameter 'Parameter' `
    -Description 'Complete parameter names for get-help, for example: Get-Help -Name Get-ChildItem -Parameter <TAB>' `
    -ScriptBlock $function:HelpParameterNameCompletion


Register-ArgumentCompleter `
    -Command 'Get-PSSnapin' `
    -Parameter 'Name' `
    -Description 'Complete loaded snapins for: Get-PSSnapin -Name <TAB>' `
    -ScriptBlock $function:GetSnapinCompletion


Register-ArgumentCompleter `
    -Command 'Add-PSSnapin' `
    -Parameter 'Name' `
    -Description 'Complete registered snapins for: Add-PSSnapin -Name <TAB>' `
    -ScriptBlock $function:AddSnapinCompletion


Register-ArgumentCompleter `
    -Command 'Get-Command' `
    -Parameter 'Verb' `
    -Description 'Complete valid verbs for: Get-Command -Verb <TAB>' `
    -ScriptBlock $function:VerbCompletion


Register-ArgumentCompleter `
    -Command 'Get-Command' `
    -Parameter 'Noun' `
    -Description 'Complete nouns for: Get-Command -Noun <TAB>' `
    -ScriptBlock $function:NounCompletion


Register-ArgumentCompleter `
    -Command ('Connect-PSSession', 'Enter-PSSession',
                   'Get-PSSession', 'Invoke-Command',
                   'New-PSSession', 'Receive-PSSession') `
    -Parameter 'ConfigurationName' `
    -Description @'
Complete session configuration names for various remoting commands if possible.
Completion will fail if -ComputerName is not specified or if credentials are required to connect.
Examples:
    Invoke-Command localhost -ConfigurationName <TAB>
    New-PSSession -cn 127.0.0.1 -ConfigurationName <TAB>
'@
 `
    -ScriptBlock $function:PSSessionConfigurationNameCompletion


Register-ArgumentCompleter `
    -Command 'Set-Strictmode' `
    -Parameter 'Version' `
    -Description 'Completes Version parameter for Set-StrictMode, for example: Set-StrictMode -Version <TAB>' `
    -ScriptBlock $function:SetStrictMode_VersionCompleter


Register-ArgumentCompleter `
    -Command ('Save-Help','Update-Help') `
    -Parameter 'Module' `
    -Description 'Completes Module parameter for Save/Update-Help commands, for example: Save-Help -Module <TAB>' `
    -ScriptBlock $function:HelpModuleCompleter


Register-ArgumentCompleter `
    -Command ('Clear-Variable','Export-Alias','Get-Alias','Get-PSDrive','Get-Variable','Import-Alias','Import-Module','New-Alias','New-PSDrive','New-Variable','Remove-PSDrive','Remove-Variable','Set-Alias','Set-Variable') `
    -Parameter 'Scope' `
    -Description 'Completes the Scope argument for *-Variable, *-Alias, *-PSDrive. For example: Get-Variable -Scope <TAB>' `
    -ScriptBlock $function:ScopeParameterCompleter


Register-ArgumentCompleter `
    -Command ('help','Get-Help') `
    -Parameter 'Name' `
    -Description 'Tab completes names of help articles, for example: Get-Help -Name <TAB>' `
    -ScriptBlock $function:HelpNameCompletion


Register-ArgumentCompleter `
    -Command 'Import-Module' `
    -Parameter 'Name' `
    -Description 'Complete the -Name argument to Import-Module, for example: Import-Module -Name <TAB>' `
    -ScriptBlock $function:ImportModuleNameCompleter