Skip to content

Commit 696f7fd

Browse files
authored
Adding snippet for class based argument completion
1 parent 4914f65 commit 696f7fd

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

snippets/PowerShell.json

+171
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,177 @@
638638
],
639639
"description": "Parameter declaration snippet for a LiteralPath parameter"
640640
},
641+
"Advanced-Argument-Completer" : {
642+
"prefix" : "advanced-argument-completer",
643+
"body": [
644+
"using namespace System.Management.Automation",
645+
"using namespace System.Collections.Generic",
646+
"",
647+
"class CustomArgumentCompleter : IArgumentCompleter {",
648+
"",
649+
"\t[List[CompletionResult]] \\$Results = [List[CompletionResult]]::new()",
650+
"\t[string] \\$WordToComplete",
651+
"",
652+
"\t[IEnumerable[CompletionResult]]",
653+
"\tCompleteArgument([string] \\$commandName, [string] \\$parameterName, [string] \\$wordToComplete,",
654+
"\t\t[Language.CommandAst] \\$commandAst,",
655+
"\t\t[Collections.IDictionary] \\$fakeBoundParameters) {",
656+
"\t\t\\$this.WordToComplete = \\$wordToComplete",
657+
"",
658+
"\t\tswitch (\"$commandName-$parameterName\") {",
659+
"\t\t\t# handle completion based on both command and parameter name",
660+
"\t\t\t\"Get-Account-Client\" {",
661+
"\t\t\t\t\\$this.CompleteAccountClient(\\$fakeBoundParameters)",
662+
"\t\t\t\tbreak",
663+
"\t\t\t}",
664+
"\t\t\tdefault {",
665+
"\t\t\t\t# handle generic parameter completion",
666+
"\t\t\t\tswitch (\\$parameterName) {",
667+
"\t\t\t\t\t\"Client\" {",
668+
"\t\t\t\t\t\t\\$this.CompleteClient(\\$fakeBoundParameters)",
669+
"\t\t\t\t\t\tbreak",
670+
"\t\t\t\t\t}",
671+
"\t\t\t\t}",
672+
"\t\t\t}",
673+
"\t\t}",
674+
"\t\treturn \\$this.Results",
675+
"\t}",
676+
"",
677+
"\t# sample completers",
678+
"\t[void] CompleteAccountClient([Collections.IDictionary] \\$fakeBoundParameter) {",
679+
"\t\t# Get-Client knows how to get clients",
680+
"\t\t\\$clients = [client]::GetAll()",
681+
"\t\tforeach (\\$client in \\$clients) {",
682+
"\t\t\tif (\\$client.Name.StartsWith(\"S\")) {",
683+
"\t\t\t\tcontinue",
684+
"\t\t\t}",
685+
"\t\t\t\\$this.AddCompletionIfContainsWordToComplete(\\$client.Name)",
686+
"\t\t}",
687+
"\t}",
688+
"",
689+
"\t[void] CompleteClient([Collections.IDictionary] \\$fakeBoundParameter) {",
690+
"\t\t# Get-Client knows how to get clients",
691+
"\t\t\\$clients = [Client]::GetAll()",
692+
"\t\tforeach (\\$client in \\$clients) {",
693+
"\t\t\t\\$this.AddCompletionIfStartsWithWordToComplete(\\$client.Name, \\$client.Name, [CompletionResultType]::ParameterValue, \\$client.Description)",
694+
"\t\t}",
695+
"\t}",
696+
"",
697+
"\t# Helpers used in common completion situations",
698+
"#region Helpers",
699+
"",
700+
"\t[void] AddCompletionIfStartsWithWordToComplete([string] \\$text) {",
701+
"\t\tif (\\$this.StartsWithWordToComplete(\\$text)) {",
702+
"\t\t\t\\$this.AddCompletion(\\$text)",
703+
"\t\t}",
704+
"\t}",
705+
"",
706+
"\t[void] AddCompletionIfStartsWithWordToComplete([string] \\$text, [string] \\$toolTip) {",
707+
"\t\tif (\\$this.StartsWithWordToComplete(\\$text)) {",
708+
"\t\t\t\\$this.AddCompletion(\\$text, \\$text, [CompletionResultType]::ParameterValue, \\$toolTip)",
709+
"\t\t}",
710+
"\t}",
711+
"",
712+
"\t[void] AddCompletionIfStartsWithWordToComplete([string] \\$text, [string] \\$listItemText, [CompletionResultType] \\$resultType, [string] \\$toolTip) {",
713+
"\t\tif (\\$this.StartsWithWordToComplete(\\$text)) {",
714+
"\t\t\t\\$this.AddCompletion(\\$text, \\$listItemText, \\$resultType, \\$toolTip)",
715+
"\t\t}",
716+
"\t}",
717+
"",
718+
"\t[void] AddCompletionIfContainsWordToComplete([string] \\$text) {",
719+
"\t\tif (\\$this.ContainsWordToComplete(\\$text)) {",
720+
"\t\t\t\\$this.AddCompletion(\\$text)",
721+
"\t\t}",
722+
"\t}",
723+
"",
724+
"\t[void] AddCompletionIfContainsWordToComplete([string] \\$text, [string] \\$toolTip) {",
725+
"\t\tif (\\$this.ContainsWordToComplete(\\$text)) {",
726+
"\t\t\t\\$this.AddCompletion(\\$text, \\$text, [CompletionResultType]::ParameterValue, \\$toolTip)",
727+
"\t\t}",
728+
"\t}",
729+
"",
730+
"\t[void] AddCompletionIfContainsWordToComplete([string] \\$text, [string] \\$listItemText, [CompletionResultType] \\$resultType, [string] \\$toolTip) {",
731+
"\t\tif (\\$this.ContainsWordToComplete(\\$text)) {",
732+
"\t\t\t\\$this.AddCompletion(\\$text, \\$listItemText, \\$resultType, \\$toolTip)",
733+
"\t\t}",
734+
"\t}",
735+
"",
736+
"\t[void] AddCompletion([string] \\$text) {",
737+
"\t\t\\$quoted = [CustomArgumentCompleter]::QuoteTextWithSpace(\\$text)",
738+
"\t\t\\$res = [CompletionResult]::new(\\$quoted, \\$text, [CompletionResultType]::ParameterValue, \\$text)",
739+
"\t\t\\$this.Results.Add(\\$res)",
740+
"\t}",
741+
"",
742+
"\t[void] AddCompletion([string] \\$text, [string] \\$listItemText, [CompletionResultType] \\$resultType, [string] \\$toolTip) {",
743+
"\t\t\\$quoted = [CustomArgumentCompleter]::QuoteTextWithSpace(\\$text)",
744+
"\t\t\\$res = [CompletionResult]::new(\\$quoted, \\$listItemText, [CompletionResultType]::ParameterValue, \\$toolTip)",
745+
"\t\t\\$this.Results.Add(\\$res)",
746+
"\t}",
747+
"",
748+
"\tstatic [string] QuoteTextWithSpace([string] \\$text) {",
749+
"\t\tif (\\$text.IndexOf(\" \") -ne -1) {",
750+
"\t\t\treturn \"\"\"$text\"\"\"",
751+
"\t\t}",
752+
"\t\treturn \\$text",
753+
"\t}",
754+
"",
755+
"\t[bool] ContainsWordToComplete([string] \\$text) {",
756+
"\t\treturn \\$text.IndexOf(\\$this.WordToComplete, [StringComparison]::CurrentCultureIgnoreCase) -ne -1",
757+
"\t}",
758+
"",
759+
"\t[bool] StartsWithWordToComplete([string] \\$text) {",
760+
"\t\treturn \\$text.StartsWith(\\$this.WordToComplete, [StringComparison]::CurrentCultureIgnoreCase)",
761+
"\t}",
762+
"#endregion ",
763+
"}",
764+
"",
765+
"#region exampleusage",
766+
"class Client {",
767+
"\t[string] \\$Name",
768+
"\t[string] \\$Description",
769+
"",
770+
"\tstatic [Client[]] GetAll() {",
771+
"\t\t\\$clients = foreach (\\$name in 'Steve', 'Joey', 'Jeffrey') { ",
772+
"\t\t\t[Client] @{ Name = \\$name; Description = \"Descrption of \\$name\" } ",
773+
"\t\t}",
774+
"\t\treturn \\$clients",
775+
"\t}",
776+
"}",
777+
"",
778+
"class Account{",
779+
"\t[string] \\$Description",
780+
"}",
781+
"",
782+
"function Get-Client {",
783+
"\t[OutputType([Client])]",
784+
"\tparam(",
785+
"\t\t[ArgumentCompleter([CustomArgumentCompleter])]",
786+
"\t\t[Parameter(Mandatory)]",
787+
"\t\t[string] \\$Client",
788+
"\t)",
789+
"\tforeach (\\$name in 'Steve', 'Joey', 'Jeffrey') { ",
790+
"\t\tif (\\$name -eq \\$Client) {",
791+
"\t\t\t[Client] @{ Name = \\$name; Description = \"Descrption of \\$name\" } ",
792+
"\t\t}",
793+
"\t}",
794+
"}",
795+
"",
796+
"function Get-Account {",
797+
"\t[OutputType([Account])]",
798+
"\tparam(",
799+
"\t\t[Parameter(Mandatory)]",
800+
"\t\t[ArgumentCompleter([CustomArgumentCompleter])]",
801+
"\t\t[string] \\$Client",
802+
"\t)",
803+
"\t[Account] @{",
804+
"\t\tDescription = \"Account for \\$Client\"",
805+
"\t}",
806+
"}",
807+
"",
808+
"#endregion"
809+
],
810+
"description": "class based argument completer"
811+
},
641812
"DSC Ensure Enum": {
642813
"prefix": "DSC Ensure enum",
643814
"body": [

0 commit comments

Comments
 (0)