|
1 | 1 | Register-ArgumentCompleter -Native -CommandName @('yarn', 'yarn.cmd') -ScriptBlock {
|
2 | 2 | param($wordToComplete, $commandAst, $cursorPosition)
|
3 | 3 |
|
4 |
| - . $PSScriptRoot\commands.ps1 |
| 4 | + . $PSScriptRoot\lib.ps1 |
5 | 5 | . $PSScriptRoot\utils.ps1
|
6 | 6 |
|
7 | 7 | $searchBlock = { $_ -like "$wordToComplete*" }
|
8 |
| - |
9 | 8 | $completions = @()
|
10 | 9 |
|
11 | 10 | # Main command
|
12 |
| - $mainCommand = if ($commandAst.CommandElements[1]) { $commandAst.CommandElements[1].Value } else { $null } |
| 11 | + $command = if ($commandAst.CommandElements[1]) { $commandAst.CommandElements[1].Value } else { $null } |
13 | 12 | # Command's value or sub-command or command's options
|
14 |
| - $valueOrsubCommandOrOption = if ($commandAst.CommandElements[2]) { $commandAst.CommandElements[2].Value } else { $null } |
| 13 | + $commandSubPart = if ($commandAst.CommandElements[2]) { $commandAst.CommandElements[2].Value } else { $null } |
15 | 14 | # Sub-command's option or option's value of main command
|
16 |
| - $subCommandOptionOrOptionValue = if ($commandAst.CommandElements[3]) { $commandAst.CommandElements[3].Value } else { $null } |
| 15 | + $command2ndSubPart = if ($commandAst.CommandElements[3]) { $commandAst.CommandElements[3].Value } else { $null } |
17 | 16 |
|
18 |
| - # If word to complete is equal to main command, suggest all commands & options of `install` command |
19 |
| - if (AreEqual $mainCommand $wordToComplete) { |
20 |
| - $completions += $commands | Where-Object $searchBlock | ForEach-Object { |
21 |
| - [System.Management.Automation.CompletionResult]::new($_, $_, 'Command', $_) |
22 |
| - } |
23 |
| - $completions += $options['install'].Keys | Where-Object $searchBlock | ForEach-Object { |
24 |
| - [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterName', $_) |
25 |
| - } |
| 17 | + # If word to complete is equal to command part, suggest all commands & options of `install` command |
| 18 | + if (Compare-CommandElement $command $wordToComplete) { |
| 19 | + $completions += Get-1stCompletions -WordToComplete $wordToComplete |
26 | 20 | }
|
27 |
| - # If word to complete is equal to command's value/sub-command/command's options, suggest command's values and sub-commmands and command's options |
28 |
| - elseif (AreEqual $valueOrsubCommandOrOption $wordToComplete) { |
29 |
| - # Main command's values |
30 |
| - if ($commandValues[$mainCommand]) { |
31 |
| - $completions += Invoke-Command -ScriptBlock $commandValues[$mainCommand] | ForEach-Object { |
32 |
| - [System.Management.Automation.CompletionResult]::new($_, $_, 'DynamicKeyword', $_) |
33 |
| - } |
34 |
| - } |
35 |
| - # Sub-commands |
36 |
| - if ($subCommands[$mainCommand]) { |
37 |
| - $completions += $subCommands[$mainCommand].Keys | Where-Object $searchBlock | ForEach-Object { |
38 |
| - [System.Management.Automation.CompletionResult]::new($_, $_, 'Command', $_) |
39 |
| - } |
40 |
| - } |
41 |
| - # Main command's options |
42 |
| - if ($options[$mainCommand]) { |
43 |
| - $completions += $options[$mainCommand].Keys | Where-Object $searchBlock | ForEach-Object { |
44 |
| - [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterName', $_) |
45 |
| - } |
46 |
| - } |
| 21 | + # If word to complete is equal to command sub-part, suggest command's values and sub-commmands and command's options |
| 22 | + elseif (Compare-CommandElement $commandSubPart $wordToComplete) { |
| 23 | + $completions += Get-2ndCompletions -WordToComplete $wordToComplete -Command $command |
47 | 24 | }
|
48 |
| - # If word to complete is equal to main command option's values/sub-command's options, |
| 25 | + # If word to complete is equal to second sub-part, |
49 | 26 | # suggest main command option's values or sub-command's options
|
50 |
| - elseif (AreEqual $subCommandOptionOrOptionValue $wordToComplete) { |
51 |
| - # Main command option's value |
52 |
| - if ($options[$mainCommand][$valueOrsubCommandOrOption]) { |
53 |
| - $optionValues = $options[$mainCommand][$valueOrsubCommandOrOption] |
54 |
| - |
55 |
| - $completions += $optionValues | Where-Object $searchBlock | ForEach-Object { |
56 |
| - [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) |
57 |
| - } |
58 |
| - |
59 |
| - # Immediately return if it's command's value completion |
60 |
| - return $completions |
61 |
| - } |
62 |
| - |
63 |
| - # Sub-command's options |
64 |
| - if ($subCommands[$mainCommand][$valueOrsubCommandOrOption]) { |
65 |
| - $subCommand = $subCommands[$mainCommand][$valueOrsubCommandOrOption] |
66 |
| - $subCommandOptionOrOptionValues = if ($subCommand.options) { $subCommand.options.Keys } else { $null } |
67 |
| - |
68 |
| - $completions += $subCommandOptionOrOptionValues | Where-Object $searchBlock | ForEach-Object { |
69 |
| - [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterName', $_) |
70 |
| - } |
71 |
| - } |
| 27 | + elseif (Compare-CommandElement $command2ndSubPart $wordToComplete) { |
| 28 | + $completions += Get-3rdCompletions -WordToComplete $wordToComplete -Command $command -CommandSubPart $commandSubPart |
72 | 29 | }
|
73 | 30 |
|
74 | 31 | # Always suggest global options
|
75 |
| - $completions += $globalOptions | Where-Object $searchBlock | ForEach-Object { |
| 32 | + $completions += Get-GlobalOptionCompletions | Where-Object $searchBlock | ForEach-Object { |
76 | 33 | [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterName', $_)
|
77 | 34 | }
|
78 | 35 |
|
|
0 commit comments