@@ -5,37 +5,63 @@ Register-ArgumentCompleter -Native -CommandName @('yarn', 'yarn.cmd') -ScriptBlo
5
5
. $PSScriptRoot \utils.ps1
6
6
7
7
$searchBlock = { $_ -like " $wordToComplete *" }
8
+
8
9
$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 }
12
17
13
18
# 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 ) {
15
20
$completions += $commands | Where-Object $searchBlock | ForEach-Object {
16
21
[System.Management.Automation.CompletionResult ]::new($_ , $_ , ' Command' , $_ )
17
22
}
18
- $completions += $options [' install' ] | Where-Object $searchBlock | ForEach-Object {
23
+ $completions += $options [' install' ].Keys | Where-Object $searchBlock | ForEach-Object {
19
24
[System.Management.Automation.CompletionResult ]::new($_ , $_ , ' ParameterName' , $_ )
20
25
}
21
26
}
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
+ }
26
40
}
27
41
}
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
+ }
39
65
}
40
66
}
41
67
0 commit comments