Skip to content

Commit bde402b

Browse files
committed
✨ Add options value completion
1 parent 7aebe88 commit bde402b

22 files changed

+188
-149
lines changed

src/completion.ps1

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/options/add.ps1

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ if (-not $options) {
33
}
44

55
# Options for `yarn add`
6-
$options['add'] = @(
7-
'-W',
8-
'--ignore-workspace-root-check',
9-
'-D',
10-
'--dev',
11-
'-P',
12-
'--peer',
13-
'-O',
14-
'--optional',
15-
'-E',
16-
'--exact',
17-
'-T',
18-
'--tilde',
19-
'-A',
20-
'--audit'
21-
)
6+
$options['add'] = [ordered] @{
7+
'-W' = @();
8+
'--ignore-workspace-root-check' = @();
9+
'-D' = @();
10+
'--dev' = @();
11+
'-P' = @();
12+
'--peer' = @();
13+
'-O' = @();
14+
'--optional' = @();
15+
'-E' = @();
16+
'--exact' = @();
17+
'-T' = @();
18+
'--tilde' = @();
19+
'-A' = @();
20+
'--audit' = @()
21+
}

src/options/audit.ps1

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@ if (-not $options) {
33
}
44

55
# Options for `yarn audit`
6-
$options['audit'] = @(
7-
'--summary',
8-
'--groups',
9-
'--level'
10-
)
6+
$options['audit'] = [ordered] @{
7+
'--summary' = @();
8+
'--groups' = @(
9+
'devDependencies',
10+
'dependencies',
11+
'optionalDependencies'
12+
);
13+
'--level' = @(
14+
'info',
15+
'low',
16+
'moderate',
17+
'high',
18+
'critical'
19+
);
20+
}

src/options/autoclean.ps1

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ if (-not $options) {
33
}
44

55
# Options for `yarn autoclean`
6-
$options['autoclean'] = @(
7-
'-I',
8-
'--init',
9-
'-F',
10-
'--force'
11-
)
6+
$options['autoclean'] = [ordered] @{
7+
'-I' = @();
8+
'--init' = @();
9+
'-F' = @();
10+
'--force' = @();
11+
}

src/options/cache.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ if (-not $options) {
33
}
44

55
# Options for `yarn cache`
6-
$options['cache'] = @(
7-
'--pattern'
8-
)
6+
$options['cache'] = [ordered] @{
7+
'--pattern' = @();
8+
}

src/options/check.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ if (-not $options) {
33
}
44

55
# Options for `yarn check`
6-
$options['check'] = @(
7-
'--integrity',
8-
'--verify-tree'
9-
)
6+
$options['check'] = [ordered] @{
7+
'--integrity' = @();
8+
'--verify-tree' = @()
9+
}

src/options/generate-lock-entry.ps1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ if (-not $options) {
33
}
44

55
# Options for `yarn generate-lock-entry`
6-
$options['generate-lock-entry'] = @(
7-
'--use-manifest',
8-
'--resolved',
9-
'--registry'
10-
)
6+
$options['generate-lock-entry'] = [ordered] @{
7+
'--use-manifest' = @();
8+
'--resolved' = @();
9+
'--registry' = @();
10+
}

src/options/global.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ if (-not $options) {
33
}
44

55
# Options for `yarn global`
6-
$options['global'] = @(
7-
'--prefix'
8-
)
6+
$options['global'] = [ordered] @{
7+
'--prefix' = @();
8+
}

src/options/init.ps1

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ if (-not $options) {
33
}
44

55
# Options for `yarn init`
6-
$options['init'] = @(
7-
'-y',
8-
'--yes',
9-
'-p',
10-
'--private'
11-
)
6+
$options['init'] = [ordered] @{
7+
'-y' = @();
8+
'--yes' = @();
9+
'-p' = @();
10+
'--private' = @();
11+
}

src/options/install.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ if (-not $options) {
33
}
44

55
# Options for `yarn install`
6-
$options['install'] = @(
7-
'-A',
8-
'--audit'
9-
)
6+
$options['install'] = [ordered] @{
7+
'-A' = @();
8+
'--audit' = @();
9+
}

0 commit comments

Comments
 (0)