|
1 |
| -@('yarn', 'yarn.cmd') | ForEach-Object { |
2 |
| - Register-ArgumentCompleter -Native -CommandName $_ -ScriptBlock { |
3 |
| - param($wordToComplete, $commandAst, $cursorPosition) |
| 1 | +Register-ArgumentCompleter -Native -CommandName @('yarn', 'yarn.cmd') -ScriptBlock { |
| 2 | + param($wordToComplete, $commandAst, $cursorPosition) |
4 | 3 |
|
5 |
| - $subCommand = $commandAst.CommandElements[1].Value |
| 4 | + . $PSScriptRoot\commands.ps1 |
| 5 | + . $PSScriptRoot\utils.ps1 |
6 | 6 |
|
7 |
| - switch ($subCommand) { |
8 |
| - 'global' { |
9 |
| - . $PSScriptRoot\sub-commands\global.ps1 |
10 |
| - } |
11 |
| - Default { |
12 |
| - . $PSScriptRoot\commands.ps1 |
| 7 | + $searchBlock = { $_ -like "$wordToComplete*" } |
| 8 | + $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 |
13 | 12 |
|
14 |
| - # Don't complete any word after sub-command when it isn't one of above commands. |
15 |
| - if ($subCommand -in $cmds) { |
16 |
| - $cmds = @() |
17 |
| - } |
18 |
| - } |
| 13 | + # If word to complete is equal to main command, suggest all commands & options of `install` command |
| 14 | + if (AreEqual $secondPart $wordToComplete) { |
| 15 | + $completions += $commands | Where-Object $searchBlock | ForEach-Object { |
| 16 | + [System.Management.Automation.CompletionResult]::new($_, $_, 'Command', $_) |
19 | 17 | }
|
| 18 | + $completions += $options['install'] | Where-Object $searchBlock | ForEach-Object { |
| 19 | + [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterName', $_) |
| 20 | + } |
| 21 | + } |
| 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', $_) |
| 26 | + } |
| 27 | + } |
| 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 } |
20 | 36 |
|
21 |
| - $cmds | |
22 |
| - Where-Object { $_ -like "$wordToComplete*" } | |
23 |
| - Sort-Object | |
24 |
| - ForEach-Object { |
25 |
| - [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) |
26 |
| - } |
| 37 | + $completions += $subCommandOptions | Where-Object $searchBlock | ForEach-Object { |
| 38 | + [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterName', $_) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + # Always suggest global options |
| 43 | + $completions += $globalOptions | Where-Object $searchBlock | ForEach-Object { |
| 44 | + [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterName', $_) |
27 | 45 | }
|
| 46 | + |
| 47 | + return $completions |
28 | 48 | }
|
0 commit comments