Skip to content

Commit 186c85b

Browse files
authored
Better handling of commands with spaces
1 parent cf622e7 commit 186c85b

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

sfdx-autocomplete.ps1

+22-16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ $sfdxCommandsFileCreateBlock = {
1515
<# executes the above script in the background so user is not waiting for the shell to start #>
1616
$sfdxCommandsFileCreateJob = Start-Job -ScriptBlock $sfdxCommandsFileCreateBlock -argumentlist $global:sfdxCommandsFile
1717

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+
1832
<# script block for autocomplete. looks up matching commands from the file created above #>
1933
$scriptBlock = {
2034
param($wordToComplete, $commandAst, $cursorPosition)
@@ -27,37 +41,29 @@ $scriptBlock = {
2741
$script:sfdxCommands = Receive-Job -Wait -Job $sfdxCommandsFileCreateJob
2842
}
2943
}
30-
3144
if ($commandAst.CommandElements.Count -eq 1) {
3245
<# List all commands #>
3346
$script:sfdxCommands | ForEach-Object {
3447
[System.Management.Automation.CompletionResult]::new($_.id, $_.id, 'Method', $_.description??'No description available')
3548
}
3649
}
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)
3852
<# 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 #>
4054
$script:sfdxCommands | Where-Object id -match $commandPattern | ForEach-Object {
4155
[System.Management.Automation.CompletionResult]::new($_.id, $_.id, 'Method', $_.description??'No description available')
4256
}
4357
}
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)
5762
($script:sfdxCommands | Where-Object id -eq $commandToMatch).flags.PsObject.Properties | Where-Object Name -like $parameterToMatch | ForEach-Object {
5863
[System.Management.Automation.CompletionResult]::new("--" + $_.Value.name, $_.Value.name, 'ParameterName', $_.Value.description??'No description available')
5964
}
6065
}
6166
}
67+
6268
<# register the above script to fire when tab is pressed following the sfdx command#>
6369
Register-ArgumentCompleter -Native -CommandName sfdx -ScriptBlock $scriptBlock

0 commit comments

Comments
 (0)