-
Notifications
You must be signed in to change notification settings - Fork 2
/
CompletionResult.ps1
54 lines (44 loc) · 2.1 KB
/
CompletionResult.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
function New-CompletionResult
{
param([Parameter(Position=0, ValueFromPipelineByPropertyName, Mandatory, ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[string]
$CompletionText,
[Parameter(Position=1, ValueFromPipelineByPropertyName)]
[string]
$ToolTip,
[Parameter(Position=2, ValueFromPipelineByPropertyName)]
[string]
$ListItemText,
[System.Management.Automation.CompletionResultType]
$CompletionResultType = [System.Management.Automation.CompletionResultType]::ParameterValue,
[Parameter(Mandatory = $false)]
[switch] $NoQuotes = $false
)
process
{
$toolTipToUse = if ($ToolTip -eq '') { $CompletionText } else { $ToolTip }
$listItemToUse = if ($ListItemText -eq '') { $CompletionText } else { $ListItemText }
# If the caller explicitly requests that quotes
# not be included, via the -NoQuotes parameter,
# then skip adding quotes.
if ($CompletionResultType -eq [System.Management.Automation.CompletionResultType]::ParameterValue -and -not $NoQuotes)
{
# Add single quotes for the caller in case they are needed.
# We use the parser to robustly determine how it will treat
# the argument. If we end up with too many tokens, or if
# the parser found something expandable in the results, we
# know quotes are needed.
$tokens = $null
$null = [System.Management.Automation.Language.Parser]::ParseInput("echo $CompletionText", [ref]$tokens, [ref]$null)
if ($tokens.Length -ne 3 -or
($tokens[1] -is [System.Management.Automation.Language.StringExpandableToken] -and
$tokens[1].Kind -eq [System.Management.Automation.Language.TokenKind]::Generic))
{
$CompletionText = "'$CompletionText'"
}
}
return New-Object System.Management.Automation.CompletionResult `
($CompletionText,$listItemToUse,$CompletionResultType,$toolTipToUse.Trim())
}
}