Last active
August 2, 2019 23:28
-
-
Save powercode/2942c2ecf5176b326d9226783ea6f6a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using namespace System.Management.Automation | |
using namespace System.Collections.Generic | |
class ArgumentCompleterBase : IArgumentCompleter { | |
hidden [List[CompletionResult]] $Results = [List[CompletionResult]]::new() | |
[string] $WordToComplete | |
[Language.CommandAst] $commandAst | |
# override in child class | |
[void] AddCompletionsFor([string] $commandName, [string] $parameterName, [Collections.IDictionary] $fakeBoundParameters) {} | |
[IEnumerable[CompletionResult]] | |
CompleteArgument([string] $commandName, [string] $parameterName, [string] $wordToComplete, | |
[Language.CommandAst] $commandAst, | |
[Collections.IDictionary] $fakeBoundParameters) { | |
$this.WordToComplete = $wordToComplete | |
$this.CommandAst = $commandAst | |
$this.AddCompletionsFor($commandName, $parameterName, $fakeBoundParameters) | |
return $this.Results | |
} | |
<# | |
Adds a completion result to the result set | |
#> | |
[void] CompleteWith([CompletionResult] $completionResult) { | |
$this.Results.Add($completionResult) | |
} | |
[void] CompleteWith([string] $text) { | |
$this.CompleteWith($text, $text, $text, [CompletionResultType]::ParameterValue) | |
} | |
[void] CompleteWith([string] $text, [string] $listItemText) { | |
$this.CompleteWith($text, $listItemText, $text, [CompletionResultType]::ParameterValue) | |
} | |
[void] CompleteWith([string] $text, [string] $listItemText, [string] $toolTip) { | |
$this.CompleteWith($text, $listItemText, $tooltip, [CompletionResultType]::ParameterValue) | |
} | |
[void] CompleteWith([string] $text, [string] $listItemText, [string] $toolTip, [CompletionResultType] $resultType) { | |
$quotedText = [ArgumentCompleterBase]::QuoteTextWithSpace($text) | |
if ([string]::IsNullOrEmpty($listItemText)) { | |
$listItemText = $text | |
} | |
if ([string]::IsNullOrEmpty($toolTip)) { | |
$toolTip = $text | |
} | |
$cr = [CompletionResult]::new($quotedText, $listItemText, $resultType, $toolTip) | |
$this.Results.Add($cr) | |
} | |
<# | |
Adds a completion result to the result set if $text starts with WordToComplete | |
#> | |
[void] CompleteWithIfTextStartsWithWordToComplete([string] $text) { | |
$this.CompleteWithIfTextStartsWithWordToComplete($text, $text, $text, [CompletionResultType]::ParameterValue) | |
} | |
[void] CompleteWithIfTextStartsWithWordToComplete([string] $text, [string] $listItemText) { | |
$this.CompleteWithIfTextStartsWithWordToComplete($text, $listItemText, $text, [CompletionResultType]::ParameterValue) | |
} | |
[void] CompleteWithIfTextStartsWithWordToComplete([string] $text, [string] $listItemText, [string] $toolTip) { | |
$this.CompleteWithIfTextStartsWithWordToComplete($text, $listItemText, $tooltip, [CompletionResultType]::ParameterValue) | |
} | |
[void] CompleteWithIfTextStartsWithWordToComplete([string] $text, [string] $listItemText, [string] $toolTip, [CompletionResultType] $resultType) { | |
if ($this.StartsWithWordToComplete($text)) { | |
$this.CompleteWith($text, $listItemText, $toolTip, $resultType) | |
} | |
} | |
<# | |
Adds a completion result to the result set if $text contains WordToComplete | |
#> | |
[void] CompleteWithIfTextContainsWordToComplete([string] $text) { | |
$this.CompleteWithIfTextContainsWordToComplete($text, $text, $text, [CompletionResultType]::ParameterValue) | |
} | |
[void] CompleteWithIfTextContainsWordToComplete([string] $text, [string] $listItemText) { | |
$this.CompleteWithIfTextContainsWordToComplete($text, $listItemText, $text, [CompletionResultType]::ParameterValue) | |
} | |
[void] CompleteWithIfTextContainsWordToComplete([string] $text, [string] $listItemText, [string] $toolTip) { | |
$this.CompleteWithIfTextContainsWordToComplete($text, $listItemText, $tooltip, [CompletionResultType]::ParameterValue) | |
} | |
[void] CompleteWithIfTextContainsWordToComplete([string] $text, [string] $listItemText, [string] $toolTip, [CompletionResultType] $resultType) { | |
if ($this.ContainsWordToComplete($text)) { | |
$this.CompleteWith($text, $listItemText, $toolTip, $resultType) | |
} | |
} | |
<# | |
Adds a completion result to the result set if $text, $listItemText or $tooptip contains WordToComplete | |
#> | |
[void] CompleteWithIfAnyContainsWordToComplete([string] $text) { | |
$this.CompleteWithIfAnyContainsWordToComplete($text, $text, $text, [CompletionResultType]::ParameterValue) | |
} | |
[void] CompleteWithIfAnyContainsWordToComplete([string] $text, [string] $listItemText) { | |
$this.CompleteWithIfAnyContainsWordToComplete($text, $listItemText, $text, [CompletionResultType]::ParameterValue) | |
} | |
[void] CompleteWithIfAnyContainsWordToComplete([string] $text, [string] $listItemText, [string] $toolTip) { | |
$this.CompleteWithIfAnyContainsWordToComplete($text, $listItemText, $tooltip, [CompletionResultType]::ParameterValue) | |
} | |
[void] CompleteWithIfAnyContainsWordToComplete([string] $text, [string] $listItemText, [string] $toolTip, [CompletionResultType] $resultType) { | |
if ($this.ContainsWordToComplete($text) -or $this.ContainsWordToComplete($resultType) -or $this.ContainsWordToComplete($toolTip)) { | |
$this.CompleteWith($text, $listItemText, $toolTip, $resultType) | |
} | |
} | |
<# | |
Adds a completion result to the result set if $text, $listItemText or $tooptip starts with WordToComplete | |
#> | |
[void] CompleteWithIfAnyStartsWithWordToComplete([string] $text) { | |
$this.CompleteWithIfAnyStartsWithWordToComplete($text, $text, $text, [CompletionResultType]::ParameterValue) | |
} | |
[void] CompleteWithIfAnyStartsWithWordToComplete([string] $text, [string] $listItemText) { | |
$this.CompleteWithIfAnyStartsWithWordToComplete($text, $listItemText, $text, [CompletionResultType]::ParameterValue) | |
} | |
[void] CompleteWithIfAnyStartsWithWordToComplete([string] $text, [string] $listItemText, [string] $toolTip) { | |
$this.CompleteWithIfAnyStartsWithWordToComplete($text, $listItemText, $tooltip, [CompletionResultType]::ParameterValue) | |
} | |
[void] CompleteWithIfAnyStartsWithWordToComplete([string] $text, [string] $listItemText, [string] $toolTip, [CompletionResultType] $resultType) { | |
if ($this.StartsWithWordToComplete($text) -or $this.StartsWithWordToComplete($resultType) -or $this.StartsWithWordToComplete($toolTip)) { | |
$this.CompleteWith($text, $listItemText, $toolTip, $resultType) | |
} | |
} | |
[bool] StartsWithWordToComplete([string] $text) { | |
return $text.StartsWith($this.WordToComplete, [StringComparison]::CurrentCultureIgnoreCase) | |
} | |
[bool] StartsWithWordToComplete([string] $text, [StringComparison] $comparison) { | |
return $text.StartsWith($this.WordToComplete, $comparison) | |
} | |
[bool] ContainsWordToComplete([string] $text) { | |
return $text.IndexOf($this.WordToComplete, [StringComparison]::CurrentCultureIgnoreCase) -ne -1 | |
} | |
[bool] ContainsWordToComplete([string] $text, [StringComparison] $comparison) { | |
return $text.IndexOf($this.WordToComplete, $comparison) -ne -1 | |
} | |
hidden static [string] QuoteTextWithSpace([string] $text) { | |
if ($text.IndexOf(" ") -ne -1) { | |
return """$text""" | |
} | |
return $text | |
} | |
} | |
class CustomArgumentCompleter : ArgumentCompleterBase { | |
# override | |
[void] AddCompletionsFor([string] $commandName, [string] $parameterName, [Collections.IDictionary] $fakeBoundParameters) { | |
switch ("${commandName}:$parameterName") { | |
# handle completion based on both command and parameter name | |
"Verb-Noun:ParamName" { | |
$this.CompleteVerbNounParam($fakeBoundParameters) | |
break | |
} | |
# | |
# Add one switch clases for each parameter with generic completion | |
# | |
default { | |
# handle generic parameter completion | |
switch ($parameterName) { | |
"ParameterName" { | |
$this.CompleteParameterName($fakeBoundParameters) | |
break | |
} | |
# | |
# Add one switch clases for each parameter with generic completion | |
# | |
} | |
} | |
} | |
} | |
[void] CompleteVerbNounParam([Collections.IDictionary] $fakeBoundParameter) { | |
# complete with "completiontext" if wordToComplete is anywhere in "completiontext" | |
#$this.CompleteWithIfTextContainsWordToComplete("completiontext") | |
} | |
[void] CompleteParameterName([Collections.IDictionary] $fakeBoundParameter) { | |
# complete some ParameterNames that starts with wordToComplete | |
#$this.CompleteWithIfStartsWithWordToComplete("Some ParameterName") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment