Scripts/TabExpansion.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
#region Custom Argument Completors
#Global variables are required for this functionality (Invoke-ScriptAnalyzer)
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalVars", "global:options")]
param()

#region Job ID
$completion_ID = {
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    Get-RSJob | Sort-Object -Property Id | Where-Object { $_.Id -like "$wordToComplete*" } |ForEach-Object {
        New-Object System.Management.Automation.CompletionResult $_.Id, $_.Id, 'ParameterValue', ('{0} ({1})' -f $_.Description, $_.ID) 
    }
}
#endregion Job ID
#region Job Name
$completion_Name = {
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    Get-RSJob | Sort-Object -Property Name | Where-Object { $_.Name -like "$wordToComplete*" } |ForEach-Object {
        New-Object System.Management.Automation.CompletionResult $_.Name, $_.Name, 'ParameterValue', ('{0} ({1})' -f $_.Description, $_.ID) 
    }
}
#endregion Job Name
If (-not (Get-Variable -Scope Global | Where-Object {$_.Name -eq "options"})) { 
    $global:options = @{
        CustomArgumentCompleters = @{}
        NativeArgumentCompleters = @{}
    }
}

$global:options['CustomArgumentCompleters']['Get-RSJob:Id'] = $completion_ID
$global:options['CustomArgumentCompleters']['Get-RSJob:Name'] = $completion_Name
$global:options['CustomArgumentCompleters']['Remove-RSJob:Id'] = $completion_ID
$global:options['CustomArgumentCompleters']['Remove-RSJob:Name'] = $completion_Name
$global:options['CustomArgumentCompleters']['Stop-RSJob:Id'] = $completion_ID
$global:options['CustomArgumentCompleters']['Stop-RSJob:Name'] = $completion_Name
$global:options['CustomArgumentCompleters']['Receive-RSJob:Id'] = $completion_ID
$global:options['CustomArgumentCompleters']['Receive-RSJob:Name'] = $completion_Name

if (Get-Item function:tabexpansion[2]) {
    $function:tabexpansion2 = $function:tabexpansion2 -replace 'End\r\n{','End { if ($null -ne $options) { $options += $global:options} else {$options = $global:options}'
}
#endregion Custom Argument Completors