@@ -5,37 +5,63 @@ Register-ArgumentCompleter -Native -CommandName @('yarn', 'yarn.cmd') -ScriptBlo
55 . $PSScriptRoot \utils.ps1
66
77 $searchBlock = { $_ -like " $wordToComplete *" }
8+
89 $completions = @ ()
9- $secondPart = if ($commandAst.CommandElements [1 ]) { $commandAst.CommandElements [1 ].Value } else { $null } # Main command
10- $thirdPart = if ($commandAst.CommandElements [2 ]) { $commandAst.CommandElements [2 ].Value } else { $null } # Sub-command
11- $fourthPart = if ($commandAst.CommandElements [3 ]) { $commandAst.CommandElements [3 ].Value } else { $null } # Sub-command's option
10+
11+ # Main command
12+ $mainCommand = if ($commandAst.CommandElements [1 ]) { $commandAst.CommandElements [1 ].Value } else { $null }
13+ # Sub-command or command's options
14+ $subCommandOrOption = if ($commandAst.CommandElements [2 ]) { $commandAst.CommandElements [2 ].Value } else { $null }
15+ # Sub-command's option or option's value of main command
16+ $subCommandOptionOrOptionValue = if ($commandAst.CommandElements [3 ]) { $commandAst.CommandElements [3 ].Value } else { $null }
1217
1318 # If word to complete is equal to main command, suggest all commands & options of `install` command
14- if (AreEqual $secondPart $wordToComplete ) {
19+ if (AreEqual $mainCommand $wordToComplete ) {
1520 $completions += $commands | Where-Object $searchBlock | ForEach-Object {
1621 [System.Management.Automation.CompletionResult ]::new($_ , $_ , ' Command' , $_ )
1722 }
18- $completions += $options [' install' ] | Where-Object $searchBlock | ForEach-Object {
23+ $completions += $options [' install' ].Keys | Where-Object $searchBlock | ForEach-Object {
1924 [System.Management.Automation.CompletionResult ]::new($_ , $_ , ' ParameterName' , $_ )
2025 }
2126 }
22- # If word to complete is equal to sub-command, suggest sub-commmands
23- if ((-not $ (AreEqual $secondPart $wordToComplete )) -and $ (AreEqual $thirdPart $wordToComplete )) {
24- $completions += $subCommands [$secondPart ].Keys | Where-Object $searchBlock | ForEach-Object {
25- [System.Management.Automation.CompletionResult ]::new($_ , $_ , ' Command' , $_ )
27+ # If word to complete is equal to sub-command/command's options, suggest sub-commmands and command's options
28+ elseif (AreEqual $subCommandOrOption $wordToComplete ) {
29+ # Sub-commands
30+ if ($subCommands [$mainCommand ]) {
31+ $completions += $subCommands [$mainCommand ].Keys | Where-Object $searchBlock | ForEach-Object {
32+ [System.Management.Automation.CompletionResult ]::new($_ , $_ , ' Command' , $_ )
33+ }
34+ }
35+ # Main command's options
36+ if ($options [$mainCommand ]) {
37+ $completions += $options [$mainCommand ].Keys | Where-Object $searchBlock | ForEach-Object {
38+ [System.Management.Automation.CompletionResult ]::new($_ , $_ , ' ParameterName' , $_ )
39+ }
2640 }
2741 }
28- # If word to complete is equal to sub-command's options, suggest sub-command's options
29- if (
30- (-not $ (AreEqual $secondPart $wordToComplete )) -and
31- (-not $ (AreEqual $thirdPart $wordToComplete )) -and
32- $ (AreEqual $fourthPart $wordToComplete )
33- ) {
34- $subCommand = $subCommands [$secondPart ][$thirdPart ]
35- $subCommandOptions = if ($subCommand.options ) { $subCommand.options } else { $null }
36-
37- $completions += $subCommandOptions | Where-Object $searchBlock | ForEach-Object {
38- [System.Management.Automation.CompletionResult ]::new($_ , $_ , ' ParameterName' , $_ )
42+ # If word to complete is equal to main command option's values/sub-command's options,
43+ # suggest main command option's values or sub-command's options
44+ elseif (AreEqual $subCommandOptionOrOptionValue $wordToComplete ) {
45+ # Main command option's value
46+ if ($options [$mainCommand ][$subCommandOrOption ]) {
47+ $optionValues = $options [$mainCommand ][$subCommandOrOption ]
48+
49+ $completions += $optionValues | Where-Object $searchBlock | ForEach-Object {
50+ [System.Management.Automation.CompletionResult ]::new($_ , $_ , ' ParameterValue' , $_ )
51+ }
52+
53+ # Immediately return if it's command's value completion
54+ return $completions
55+ }
56+
57+ # Sub-command's options
58+ if ($subCommands [$mainCommand ][$subCommandOrOption ]) {
59+ $subCommand = $subCommands [$mainCommand ][$subCommandOrOption ]
60+ $subCommandOptionOrOptionValues = if ($subCommand.options ) { $subCommand.options.Keys } else { $null }
61+
62+ $completions += $subCommandOptionOrOptionValues | Where-Object $searchBlock | ForEach-Object {
63+ [System.Management.Automation.CompletionResult ]::new($_ , $_ , ' ParameterName' , $_ )
64+ }
3965 }
4066 }
4167
0 commit comments