-
Notifications
You must be signed in to change notification settings - Fork 33
/
TabExpansionPlusPlus.psm1
997 lines (881 loc) · 33.2 KB
/
TabExpansionPlusPlus.psm1
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
#############################################################################
#
# TabExpansionPlusPlus
#
#
# Save off the previous tab completion so it can be restored if this module
# is removed.
$oldTabExpansion = $function:TabExpansion
$oldTabExpansion2 = $function:TabExpansion2
[bool]$updatedTypeData = $false
$MyInvocation.MyCommand.ScriptBlock.Module.OnRemove =
{
if ($null -ne $oldTabExpansion)
{
Set-Item function:\TabExpansion $oldTabExpansion
}
if ($null -ne $oldTabExpansion2)
{
Set-Item function:\TabExpansion2 $oldTabExpansion2
}
}
#region Exported utility functions for completers
#############################################################################
#
# Helper function to create a new completion results
#
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())
}
}
#############################################################################
#
# .SYNOPSIS
#
# This is a simple wrapper of Get-Command gets commands with a given
# parameter ignoring commands that use the parameter name as an alias.
#
function Get-CommandWithParameter
{
[CmdletBinding(DefaultParameterSetName='AllCommandSet')]
param(
[Parameter(ParameterSetName='AllCommandSet', Position=0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string[]]
${Name},
[Parameter(ParameterSetName='CmdletSet', ValueFromPipelineByPropertyName)]
[string[]]
${Verb},
[Parameter(ParameterSetName='CmdletSet', ValueFromPipelineByPropertyName)]
[string[]]
${Noun},
[Parameter(ValueFromPipelineByPropertyName)]
[string[]]
${Module},
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory)]
[string]
${ParameterName})
begin
{
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Get-Command', [System.Management.Automation.CommandTypes]::Cmdlet)
$scriptCmd = { & $wrappedCmd @PSBoundParameters | Where-Object { $_.Parameters[$ParameterName] -ne $null } }
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
}
process
{
$steppablePipeline.Process($_)
}
end
{
$steppablePipeline.End()
}
}
#############################################################################
#
function Set-CompletionPrivateData
{
param(
[ValidateNotNullOrEmpty()]
[string]
$Key,
[object]
$Value,
[ValidateNotNullOrEmpty()]
[int]
$ExpirationSeconds = 604800
)
$Cache = [PSCustomObject]@{
Value = $Value
ExpirationTime = (Get-Date).AddSeconds($ExpirationSeconds)
}
$completionPrivateData[$key] = $Cache
}
#############################################################################
#
function Get-CompletionPrivateData
{
param(
[ValidateNotNullOrEmpty()]
[string]
$Key)
if(!$Key)
{ return $completionPrivateData }
$cacheValue = $completionPrivateData[$key]
if ((Get-Date) -lt $cacheValue.ExpirationTime) {
return $cacheValue.Value
}
}
#############################################################################
#
function Get-CompletionWithExtension
{
param([string] $lastWord,
[string[]] $extensions)
[System.Management.Automation.CompletionCompleters]::CompleteFilename($lastWord) |
Where-Object {
# Use ListItemText because it won't be quoted, CompletionText might be
[System.IO.Path]::GetExtension($_.ListItemText) -in $extensions
}
}
#############################################################################
#
function New-CommandTree
{
[CmdletBinding(DefaultParameterSetName='Default')]
param(
[Parameter(Position=0, Mandatory, ParameterSetName='Default')]
[Parameter(Position=0, Mandatory, ParameterSetName='Argument')]
[ValidateNotNullOrEmpty()]
[string]
$Completion,
[Parameter(Position=1, Mandatory, ParameterSetName='Default')]
[Parameter(Position=1, Mandatory, ParameterSetName='Argument')]
[string]
$Tooltip,
[Parameter(ParameterSetName='Argument')]
[switch]
$Argument,
[Parameter(Position=2, ParameterSetName='Default')]
[Parameter(Position=1, ParameterSetName='ScriptBlockSet')]
[scriptblock]
$SubCommands,
[Parameter(Position=0, Mandatory, ParameterSetName='ScriptBlockSet')]
[scriptblock]
$CompletionGenerator
)
$actualSubCommands = $null
if ($null -ne $SubCommands)
{
$actualSubCommands = [NativeCommandTreeNode[]](& $SubCommands)
}
switch ($PSCmdlet.ParameterSetName)
{
'Default' {
New-Object NativeCommandTreeNode $Completion,$Tooltip,$actualSubCommands
break
}
'Argument' {
New-Object NativeCommandTreeNode $Completion,$Tooltip,$true
}
'ScriptBlockSet' {
New-Object NativeCommandTreeNode $CompletionGenerator,$actualSubCommands
break
}
}
}
#############################################################################
#
function Get-CommandTreeCompletion
{
param($wordToComplete, $commandAst, [NativeCommandTreeNode[]]$CommandTree)
$commandElements = $commandAst.CommandElements
# Skip the first command element - it's the command name
# Iterate through the remaining elements, stopping early
# if we find the element that matches $wordToComplete.
for ($i = 1; $i -lt $commandElements.Count; $i++)
{
if (!($commandElements[$i] -is [System.Management.Automation.Language.StringConstantExpressionAst]))
{
# Ignore arguments that are expressions. In some rare cases this
# could cause strange completions because the context is incorrect, e.g.:
# $c = 'advfirewall'
# netsh $c firewall
# Here we would be in advfirewall firewall context, but we'd complete as
# though we were in firewall context.
continue
}
if ($commandElements[$i].Value -eq $wordToComplete)
{
$CommandTree = $CommandTree |
Where-Object { $_.Command -like "$wordToComplete*" -or $_.CompletionGenerator -ne $null }
break
}
foreach ($subCommand in $CommandTree)
{
if ($subCommand.Command -eq $commandElements[$i].Value)
{
if (!$subCommand.Argument)
{
$CommandTree = $subCommand.SubCommands
}
break
}
}
}
if ($null -ne $CommandTree)
{
$CommandTree | ForEach-Object {
if ($_.Command)
{
$toolTip = if ($_.Tooltip) { $_.Tooltip } else { $_.Command }
New-CompletionResult -CompletionText $_.Command -ToolTip $toolTip
}
else
{
& $_.CompletionGenerator $wordToComplete $commandAst
}
}
}
}
#endregion Exported utility functions for completers
#region Exported functions
#############################################################################
#
# .SYNOPSIS
# Register a ScriptBlock to perform argument completion for a
# given command or parameter.
#
# .DESCRIPTION
# Argument completion can be extended without needing to do any
# parsing in many cases. By registering a handler for specific
# commands and/or parameters, PowerShell will call the handler
# when appropriate.
#
# There are 2 kinds of extensions - native and PowerShell. Native
# refers to commands external to PowerShell, e.g. net.exe. PowerShell
# completion covers any functions, scripts, or cmdlets where PowerShell
# can determine the correct parameter being completed.
#
# When registering a native handler, you must specify the CommandName
# parameter. The CommandName is typically specified without any path
# or extension. If specifying a path and/or an extension, completion
# will only work when the command is specified that way when requesting
# completion.
#
# When registering a PowerShell handler, you must specify the
# ParameterName parameter. The CommandName is optional - PowerShell will
# first try to find a handler based on the command and parameter, but
# if none is found, then it will try just the parameter name. This way,
# you could specify a handler for all commands that have a specific
# parameter.
#
# A handler needs to return instances of
# System.Management.Automation.CompletionResult.
#
# A native handler is passed 2 parameters:
#
# param($wordToComplete, $commandAst)
#
# $wordToComplete - The argument being completed, possibly an empty string
# $commandAst - The ast of the command being completed.
#
# A PowerShell handler is passed 5 parameters:
#
# param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
#
# $commandName - The command name
# $parameterName - The parameter name
# $wordToComplete - The argument being completed, possibly an empty string
# $commandAst - The parsed representation of the command being completed.
# $fakeBoundParameter - Like $PSBoundParameters, contains values for some of the parameters.
# Certain values are not included, this does not mean a parameter was
# not specified, just that getting the value could have had unintended
# side effects, so no value was computed.
#
# .PARAMETER ParameterName
# The name of the parameter that the Completion parameter supports.
# This parameter is not supported for native completion and is
# mandatory for script completion.
#
# .PARAMETER CommandName
# The name of the command that the Completion parameter supports.
# This parameter is mandatory for native completion and is optional
# for script completion.
#
# .PARAMETER Completion
# A ScriptBlock that returns instances of CompletionResult. For
# native completion, the script block parameters are
#
# param($wordToComplete, $commandAst)
#
# For script completion, the parameters are:
#
# param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
#
# .PARAMETER Description
# A description of how the completion can be used.
#
function Register-ArgumentCompleter
{
[CmdletBinding(DefaultParameterSetName="PowerShellSet")]
param(
[Parameter(ParameterSetName="NativeSet", Mandatory)]
[Parameter(ParameterSetName="PowerShellSet")]
[string[]]$CommandName = "",
[Parameter(ParameterSetName="PowerShellSet", Mandatory)]
[string]$ParameterName = "",
[Parameter(Mandatory)]
[scriptblock]$ScriptBlock,
[string]$Description,
[Parameter(ParameterSetName="NativeSet")]
[switch]$Native)
$fnDefn = $ScriptBlock.Ast -as [System.Management.Automation.Language.FunctionDefinitionAst]
if (!$Description)
{
# See if the script block is really a function, if so, use the function name.
$Description = if ($fnDefn -ne $null) { $fnDefn.Name } else { "" }
}
if ($MyInvocation.ScriptName -ne (& { $MyInvocation.ScriptName }))
{
# Make an unbound copy of the script block so it has access to TabExpansionPlusPlus when invoked.
# We can skip this step if we created the script block (Register-ArgumentCompleter was
# called internally).
if ($fnDefn -ne $null){
$ScriptBlock = $ScriptBlock.Ast.Body.GetScriptBlock() # Don't reparse, just get a new ScriptBlock.
}
else {
$ScriptBlock = $ScriptBlock.Ast.GetScriptBlock() # Don't reparse, just get a new ScriptBlock.
}
}
foreach ($command in $CommandName)
{
if ($command -and $ParameterName)
{
$command += ":"
}
$key = if ($Native) { 'NativeArgumentCompleters' } else { 'CustomArgumentCompleters' }
$tabExpansionOptions[$key]["${command}${ParameterName}"] = $ScriptBlock
$tabExpansionDescriptions["${command}${ParameterName}$Native"] = $Description
}
}
#############################################################################
#
# .SYNOPSIS
# Tests the registered argument completer
#
# .DESCRIPTION
# Invokes the registered parameteter completer for a specified command to make it easier to test
# a completer
#
# .EXAMPLE
# Test-ArgumentCompleter -CommandName Get-Verb -ParameterName Verb -WordToComplete Sta
#
# Test what would be completed if Get-Verb -Verb Sta<Tab> was typed at the prompt
#
# .EXAMPLE
# Test-ArgumentCompleter -NativeCommand Robocopy -WordToComplete /
#
# Test what would be completed if Robocopy /<Tab> was typed at the prompt
#
function Test-ArgumentCompleter
{
[CmdletBinding(DefaultParametersetName='PS')]
param
(
[Parameter(Mandatory, Position=1, ParameterSetName='PS')]
[string] $CommandName
,
[Parameter(Mandatory, Position=2, ParameterSetName='PS')]
[string] $ParameterName
,
[Parameter(ParameterSetName='PS')]
[System.Management.Automation.Language.CommandAst]
$commandAst
,
[Parameter(ParameterSetName='PS')]
[Hashtable] $FakeBoundParameters = @{}
,
[Parameter(Mandatory, Position=1, ParameterSetName='NativeCommand')]
[string] $NativeCommand
,
[Parameter(Position=2, ParameterSetName='NativeCommand')]
[Parameter(Position=3, ParameterSetName='PS')]
[string] $WordToComplete = ''
)
if ($PSCmdlet.ParameterSetName -eq 'NativeCommand')
{
$Tokens = $null
$Errors = $null
$ast = [System.Management.Automation.Language.Parser]::ParseInput($NativeCommand, [ref] $Tokens, [ref] $Errors)
$commandAst = $ast.EndBlock.Statements[0].PipelineElements[0]
$command = $commandAst.GetCommandName()
$completer = $tabExpansionOptions.NativeArgumentCompleters[$command]
if (-not $Completer)
{
throw "No argument completer registered for command '$Command' (from $NativeCommand)"
}
& $completer $WordToComplete $commandAst
}
else {
$completer = $tabExpansionOptions.CustomArgumentCompleters["${CommandName}:$ParameterName"]
if (-not $Completer)
{
throw "No argument completer registered for '${CommandName}:$ParameterName'"
}
& $completer $CommandName $ParameterName $WordToComplete $commandAst $FakeBoundParameters
}
}
#############################################################################
#
# .SYNOPSIS
# Retrieves a list of argument completers that have been loaded into the
# PowerShell session.
#
# .PARAMETER Name
# The name of the argument complete to retrieve. This parameter supports
# wildcards (asterisk).
#
# .EXAMPLE
# Get-ArgumentCompleter -Name *Azure*;
function Get-ArgumentCompleter
{
[CmdletBinding()]
param([string[]]$Name = '*')
if (!$updatedTypeData)
{
# Define the default display properties for the objects returned by Get-ArgumentCompleter
[string[]]$properties = "Command", "Parameter"
Update-TypeData -TypeName 'TabExpansionPlusPlus.ArgumentCompleter' -DefaultDisplayPropertySet $properties -Force
$updatedTypeData = $true
}
function WriteCompleters
{
function WriteCompleter($command, $parameter, $native, $scriptblock)
{
foreach ($n in $Name)
{
if ($command -like $n)
{
$c = $command
if ($command -and $parameter) { $c += ':' }
$description = $tabExpansionDescriptions["${c}${parameter}${native}"]
$completer = [pscustomobject]@{
Command = $command
Parameter = $parameter
Native = $native
Description = $description
ScriptBlock = $scriptblock
File = if ($scriptblock.File) { Split-Path -Leaf -Path $scriptblock.File }
}
$completer.PSTypeNames.Add('TabExpansionPlusPlus.ArgumentCompleter')
Write-Output $completer
break
}
}
}
foreach ($pair in $tabExpansionOptions.CustomArgumentCompleters.GetEnumerator())
{
if ($pair.Key -match '^(.*):(.*)$')
{
$command = $matches[1]
$parameter = $matches[2]
}
else
{
$parameter = $pair.Key
$command = ""
}
WriteCompleter $command $parameter $false $pair.Value
}
foreach ($pair in $tabExpansionOptions.NativeArgumentCompleters.GetEnumerator())
{
WriteCompleter $pair.Key '' $true $pair.Value
}
}
WriteCompleters | Sort -Property Native,Command,Parameter
}
#############################################################################
#
# .SYNOPSIS
# Register a ScriptBlock to perform argument completion for a
# given command or parameter.
#
# .DESCRIPTION
#
# .PARAMETER Option
#
# The name of the option.
#
# .PARAMETER Value
#
# The value to set for Option. Typically this will be $true.
#
function Set-TabExpansionOption
{
param(
[ValidateSet('ExcludeHiddenFiles',
'RelativePaths',
'LiteralPaths',
'IgnoreHiddenShares',
'AppendBackslash')]
[string]
$Option,
[object]
$Value = $true)
$tabExpansionOptions[$option] = $value
}
#endregion Exported functions
#region Internal utility functions
#############################################################################
#
# This function checks if an attribute argument's name can be completed.
# For example:
# [Parameter(<TAB>
# [Parameter(Po<TAB>
# [CmdletBinding(DefaultPa<TAB>
#
function TryAttributeArgumentCompletion
{
param(
[System.Management.Automation.Language.Ast]$ast,
[int]$offset
)
$results = @()
$matchIndex = -1
try
{
# We want to find any NamedAttributeArgumentAst objects where the Ast extent includes $offset
$offsetInExtentPredicate = {
param($ast)
return $offset -gt $ast.Extent.StartOffset -and
$offset -le $ast.Extent.EndOffset
}
$asts = $ast.FindAll($offsetInExtentPredicate, $true)
$attributeType = $null
$attributeArgumentName = ""
$replacementIndex = $offset
$replacementLength = 0
$attributeArg = $asts | Where-Object { $_ -is [System.Management.Automation.Language.NamedAttributeArgumentAst] } | Select-Object -First 1
if ($null -ne $attributeArg)
{
$attributeAst = [System.Management.Automation.Language.AttributeAst]$attributeArg.Parent
$attributeType = $attributeAst.TypeName.GetReflectionAttributeType()
$attributeArgumentName = $attributeArg.ArgumentName
$replacementIndex = $attributeArg.Extent.StartOffset
$replacementLength = $attributeArg.ArgumentName.Length
}
else
{
$attributeAst = $asts | Where-Object { $_ -is [System.Management.Automation.Language.AttributeAst] } | Select-Object -First 1
if ($null -ne $attributeAst)
{
$attributeType = $attributeAst.TypeName.GetReflectionAttributeType()
}
}
if ($null -ne $attributeType)
{
$results = $attributeType.GetProperties('Public,Instance') |
Where-Object {
# Ignore TypeId (all attributes inherit it)
$_.Name -like "$attributeArgumentName*" -and $_.Name -ne 'TypeId' } |
Sort-Object -Property Name |
ForEach-Object {
$propType = [Microsoft.PowerShell.ToStringCodeMethods]::Type($_.PropertyType)
$propName = $_.Name
New-CompletionResult $propName -ToolTip "$propType $propName" -CompletionResultType Property
}
return [PSCustomObject]@{
Results = $results
ReplacementIndex = $replacementIndex
ReplacementLength = $replacementLength
}
}
}
catch {}
}
#############################################################################
#
# This function completes native commands options starting with - or --
# works around a bug in PowerShell that causes it to not complete
# native command options starting with - or --
#
function TryNativeCommandOptionCompletion
{
param(
[System.Management.Automation.Language.Ast]$ast,
[int]$offset
)
$results = @()
$replacementIndex = $offset
$replacementLength = 0
try{
# We want to find any Command element objects where the Ast extent includes $offset
$offsetInOptionExtentPredicate = {
param($ast)
return $offset -gt $ast.Extent.StartOffset -and
$offset -le $ast.Extent.EndOffset -and
$ast.Extent.Text.StartsWith('-')
}
$option = $ast.Find($offsetInOptionExtentPredicate, $true)
if ($option -ne $null)
{
$command = $option.Parent -as [System.Management.Automation.Language.CommandAst]
if ($command -ne $null)
{
$nativeCommand = [System.IO.Path]::GetFileNameWithoutExtension($command.CommandElements[0].Value)
$nativeCompleter = $tabExpansionOptions.NativeArgumentCompleters[$nativeCommand]
if ($nativeCompleter)
{
$results = @(& $nativeCompleter $option.ToString() $command)
if ($results.Count -gt 0)
{
$replacementIndex = $option.Extent.StartOffset
$replacementLength = $option.Extent.Text.Length
}
}
}
}
}
catch{}
return [PSCustomObject]@{
Results = $results
ReplacementIndex = $replacementIndex
ReplacementLength = $replacementLength
}
}
#endregion Internal utility functions
#############################################################################
#
# This function is partly a copy of the V3 TabExpansion2, adding a few
# capabilities such as completing attribute arguments and excluding hidden
# files from results.
#
function global:TabExpansion2
{
[CmdletBinding(DefaultParameterSetName = 'ScriptInputSet')]
Param(
[Parameter(ParameterSetName = 'ScriptInputSet', Mandatory, Position = 0)]
[string] $inputScript,
[Parameter(ParameterSetName = 'ScriptInputSet', Mandatory, Position = 1)]
[int] $cursorColumn,
[Parameter(ParameterSetName = 'AstInputSet', Mandatory, Position = 0)]
[System.Management.Automation.Language.Ast] $ast,
[Parameter(ParameterSetName = 'AstInputSet', Mandatory, Position = 1)]
[System.Management.Automation.Language.Token[]] $tokens,
[Parameter(ParameterSetName = 'AstInputSet', Mandatory, Position = 2)]
[System.Management.Automation.Language.IScriptPosition] $positionOfCursor,
[Parameter(ParameterSetName = 'ScriptInputSet', Position = 2)]
[Parameter(ParameterSetName = 'AstInputSet', Position = 3)]
[Hashtable] $options = $null
)
if ($null -ne $options)
{
$options += $tabExpansionOptions
}
else
{
$options = $tabExpansionOptions
}
if ($psCmdlet.ParameterSetName -eq 'ScriptInputSet')
{
$results = [System.Management.Automation.CommandCompletion]::CompleteInput(
<#inputScript#> $inputScript,
<#cursorColumn#> $cursorColumn,
<#options#> $options)
}
else
{
$results = [System.Management.Automation.CommandCompletion]::CompleteInput(
<#ast#> $ast,
<#tokens#> $tokens,
<#positionOfCursor#> $positionOfCursor,
<#options#> $options)
}
if ($results.CompletionMatches.Count -eq 0)
{
# Built-in didn't succeed, try our own completions here.
if ($psCmdlet.ParameterSetName -eq 'ScriptInputSet')
{
$ast = [System.Management.Automation.Language.Parser]::ParseInput($inputScript, [ref]$tokens, [ref]$null)
}
else
{
$cursorColumn = $positionOfCursor.Offset
}
# workaround PowerShell bug that case it to not invoking native completers for - or --
# making it hard to complete options for many commands
$nativeCommandResults = TryNativeCommandOptionCompletion -ast $ast -offset $cursorColumn
if ($null -ne $nativeCommandResults)
{
$results.ReplacementIndex = $nativeCommandResults.ReplacementIndex
$results.ReplacementLength = $nativeCommandResults.ReplacementLength
if ($results.CompletionMatches.IsReadOnly)
{
# Workaround where PowerShell returns a readonly collection that we need to add to.
$collection = new-object System.Collections.ObjectModel.Collection[System.Management.Automation.CompletionResult]
$results.GetType().GetProperty('CompletionMatches').SetValue($results, $collection)
}
$nativeCommandResults.Results | ForEach-Object {
$results.CompletionMatches.Add($_) }
}
$attributeResults = TryAttributeArgumentCompletion $ast $cursorColumn
if ($null -ne $attributeResults)
{
$results.ReplacementIndex = $attributeResults.ReplacementIndex
$results.ReplacementLength = $attributeResults.ReplacementLength
if ($results.CompletionMatches.IsReadOnly)
{
# Workaround where PowerShell returns a readonly collection that we need to add to.
$collection = new-object System.Collections.ObjectModel.Collection[System.Management.Automation.CompletionResult]
$results.GetType().GetProperty('CompletionMatches').SetValue($results, $collection)
}
$attributeResults.Results | ForEach-Object {
$results.CompletionMatches.Add($_) }
}
}
if ($options.ExcludeHiddenFiles)
{
foreach ($result in @($results.CompletionMatches))
{
if ($result.ResultType -eq [System.Management.Automation.CompletionResultType]::ProviderItem -or
$result.ResultType -eq [System.Management.Automation.CompletionResultType]::ProviderContainer)
{
try
{
$item = Get-Item -LiteralPath $result.CompletionText -ErrorAction Stop
}
catch
{
# If Get-Item w/o -Force fails, it is probably hidden, so exclude the result
$null = $results.CompletionMatches.Remove($result)
}
}
}
}
if ($options.AppendBackslash -and
$results.CompletionMatches.ResultType -contains [System.Management.Automation.CompletionResultType]::ProviderContainer)
{
foreach ($result in @($results.CompletionMatches))
{
if ($result.ResultType -eq [System.Management.Automation.CompletionResultType]::ProviderContainer)
{
$completionText = $result.CompletionText
$lastChar = $completionText[-1]
$lastIsQuote = ($lastChar -eq '"' -or $lastChar -eq "'")
if ($lastIsQuote)
{
$lastChar = $completionText[-2]
}
if ($lastChar -ne '\')
{
$null = $results.CompletionMatches.Remove($result)
if ($lastIsQuote)
{
$completionText =
$completionText.Substring(0, $completionText.Length - 1) +
'\' + $completionText[-1]
}
else
{
$completionText = $completionText + '\'
}
$updatedResult = New-Object System.Management.Automation.CompletionResult `
($completionText, $result.ListItemText, $result.ResultType, $result.ToolTip)
$results.CompletionMatches.Add($updatedResult)
}
}
}
}
if ($results.CompletionMatches.Count -eq 0)
{
# No results, if this module has overridden another TabExpansion2 function, call it
# but only if it's not the built-in function (which we assume if function isn't
# defined in a file.
if ($oldTabExpansion2 -ne $null -and $oldTabExpansion2.File -ne $null)
{
return (& $oldTabExpansion2 @PSBoundParameters)
}
}
return $results
}
#############################################################################
#
# Main
#
Add-Type @"
using System;
using System.Management.Automation;
public class NativeCommandTreeNode
{
private NativeCommandTreeNode(NativeCommandTreeNode[] subCommands)
{
SubCommands = subCommands;
}
public NativeCommandTreeNode(string command, NativeCommandTreeNode[] subCommands)
: this(command, null, subCommands)
{
}
public NativeCommandTreeNode(string command, string tooltip, NativeCommandTreeNode[] subCommands)
: this(subCommands)
{
this.Command = command;
this.Tooltip = tooltip;
}
public NativeCommandTreeNode(string command, string tooltip, bool argument)
: this(null)
{
this.Command = command;
this.Tooltip = tooltip;
this.Argument = true;
}
public NativeCommandTreeNode(ScriptBlock completionGenerator, NativeCommandTreeNode[] subCommands)
: this(subCommands)
{
this.CompletionGenerator = completionGenerator;
}
public string Command { get; private set; }
public string Tooltip { get; private set; }
public bool Argument { get; private set; }
public ScriptBlock CompletionGenerator { get; private set; }
public NativeCommandTreeNode[] SubCommands { get; private set; }
}
"@
# Custom completions are saved in this hashtable
$tabExpansionOptions = @{
CustomArgumentCompleters = @{}
NativeArgumentCompleters = @{}
}
# Descriptions for the above completions saved in this hashtable
$tabExpansionDescriptions = @{}
# And private data for the above completions cached in this hashtable
$completionPrivateData = @{}
Export-ModuleMember Get-ArgumentCompleter, Register-ArgumentCompleter,
Set-TabExpansionOption, Test-ArgumentCompleter, New-CompletionResult,
Get-CommandWithParameter, Set-CompletionPrivateData, Get-CompletionPrivateData,
Get-CompletionWithExtension, New-CommandTree, Get-CommandTreeCompletion
foreach ($file in dir $PSScriptRoot\*.ArgumentCompleters.ps1)
{
. $file.FullName
}