@@ -15,6 +15,20 @@ $sfdxCommandsFileCreateBlock = {
15
15
<# executes the above script in the background so user is not waiting for the shell to start #>
16
16
$sfdxCommandsFileCreateJob = Start-Job - ScriptBlock $sfdxCommandsFileCreateBlock - argumentlist $global :sfdxCommandsFile
17
17
18
+ $getFullCommand = {
19
+ Param ($commandAst )
20
+ # We now have commands separated by spaces instead of colons. Need the loop to figure out where the command ends and the parameters begin
21
+ [System.Collections.ArrayList ]$commandParts = @ ()
22
+ for ($i = 1 ; $i -lt ($commandAst.CommandElements.Count );$i ++ ) {
23
+ if ($commandAst.CommandElements [$i ].Value -clike " -*" ) {
24
+ break
25
+ }
26
+ $commandParts.Add ($commandAst.CommandElements [$i ].Value) > $null
27
+ }
28
+ $commandToMatch = $commandParts -join " "
29
+ return $commandToMatch.Trim ()
30
+ }
31
+
18
32
<# script block for autocomplete. looks up matching commands from the file created above #>
19
33
$scriptBlock = {
20
34
param ($wordToComplete , $commandAst , $cursorPosition )
@@ -27,37 +41,29 @@ $scriptBlock = {
27
41
$script :sfdxCommands = Receive-Job - Wait - Job $sfdxCommandsFileCreateJob
28
42
}
29
43
}
30
-
31
44
if ($commandAst.CommandElements.Count -eq 1 ) {
32
45
<# List all commands #>
33
46
$script :sfdxCommands | ForEach-Object {
34
47
[System.Management.Automation.CompletionResult ]::new($_.id , $_.id , ' Method' , $_.description ?? ' No description available' )
35
48
}
36
49
}
37
- elseif ($commandAst.CommandElements.Count -eq 2 -and $wordToComplete -ne " " ) {
50
+ elseif ($commandAst.CommandElements.Count -gt 1 -and -Not ($wordToComplete -clike " -*" )) {
51
+ $commandToMatch = (Invoke-Command - ScriptBlock $getFullCommand - ArgumentList $commandAst )
38
52
<# Completing a command #>
39
- $commandPattern = " .*" + $commandAst .CommandElements [ 1 ].Value + " .*" <# Complete if force: is not specified too #>
53
+ $commandPattern = " .*" + $commandToMatch + " .*" <# Complete if force: is not specified too #>
40
54
$script :sfdxCommands | Where-Object id -match $commandPattern | ForEach-Object {
41
55
[System.Management.Automation.CompletionResult ]::new($_.id , $_.id , ' Method' , $_.description ?? ' No description available' )
42
56
}
43
57
}
44
- elseif ($commandAst.CommandElements.Count -gt 2 ) {
45
- <# Completing a parameter #>
46
- $parameterToMatch = $commandAst.CommandElements [-1 ].ToString().TrimStart(" -" ) + " *" ;
47
-
48
- # We now have commands separated by spaces instead of colons. Need the loop to figure out where the command ends and the parameters begin
49
- [System.Collections.ArrayList ]$commandParts = @ ()
50
- for ($i = 1 ; $i -lt ($commandAst.CommandElements.Count - 1 );$i ++ ) {
51
- if ($commandAst.CommandElements [$i ].Value -clike " -*" ) {
52
- break
53
- }
54
- $commandParts.Add ($commandAst.CommandElements [$i ].Value) > $null
55
- }
56
- $commandToMatch = $commandParts -join " "
58
+ elseif ($commandAst.CommandElements.Count -gt 2 -and $wordToComplete -clike " -*" ) {
59
+ <# Completing a parameter #>
60
+ $parameterToMatch = $wordToComplete.TrimStart (" -" ) + " *" ;
61
+ $commandToMatch = (Invoke-Command - ScriptBlock $getFullCommand - ArgumentList $commandAst )
57
62
($script :sfdxCommands | Where-Object id -eq $commandToMatch ).flags.PsObject.Properties | Where-Object Name -like $parameterToMatch | ForEach-Object {
58
63
[System.Management.Automation.CompletionResult ]::new(" --" + $_.Value.name , $_.Value.name , ' ParameterName' , $_.Value.description ?? ' No description available' )
59
64
}
60
65
}
61
66
}
67
+
62
68
<# register the above script to fire when tab is pressed following the sfdx command#>
63
69
Register-ArgumentCompleter - Native - CommandName sfdx - ScriptBlock $scriptBlock
0 commit comments