Skip to content

Commit 82098f6

Browse files
committed
♻️ Update completion script for new complete structure
1 parent 962bd84 commit 82098f6

File tree

2 files changed

+52
-21
lines changed

2 files changed

+52
-21
lines changed

src/completion.ps1

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,48 @@
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)
43

5-
$subCommand = $commandAst.CommandElements[1].Value
4+
. $PSScriptRoot\commands.ps1
5+
. $PSScriptRoot\utils.ps1
66

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
1312

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', $_)
1917
}
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 }
2036

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', $_)
2745
}
46+
47+
return $completions
2848
}

src/utils.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function AreEqual($a, $b) {
2+
try {
3+
return (
4+
([string]::IsNullOrEmpty($a) -and [string]::IsNullOrEmpty($b)) -or
5+
($a -eq $b)
6+
)
7+
}
8+
catch {
9+
return $a -eq $b
10+
}
11+
}

0 commit comments

Comments
 (0)