Skip to content

Commit

Permalink
feat: refactor convert function and rename it
Browse files Browse the repository at this point in the history
  • Loading branch information
axiaoan committed Jun 28, 2022
1 parent 372bc70 commit 93a0270
Showing 1 changed file with 37 additions and 32 deletions.
69 changes: 37 additions & 32 deletions src/Register-Completion/Register-Completion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,47 @@
[hashtable]$cache_command_list = @{}
$PSVersion = $PSVersionTable.PSVersion

function Convert-JsonToHash {
Param([PSCustomObject]$json)
try {
ConvertFrom-Json -InputObject $json -AsHashtable
function ConvertTo-Hash {
Param([PSCustomObject]$InputObject)
[hashtable]$hash = @{}

if (!$InputObject) {
return ""
}
catch {
$json

$input_type = $InputObject.getType()

if ($input_type -eq [hashtable]) {
$InputObject.Keys | ForEach-Object { $hash[$_] = ConvertTo-Hash $InputObject[$_] }
}
elseif ($input_type -eq [Object[]]) {
$InputObject | ForEach-Object { $hash += ConvertTo-Hash $_ }
}
else {
try {
$json = ConvertFrom-Json -InputObject $InputObject -AsHashtable
$json_type = $json.getType()
if ($json_type -in [hashtable],[Object[]]) {
$hash = ConvertTo-Hash $json
}
else {
$hash.Add($json, "")
}
}
catch {
$hash.Add($InputObject, "")
}
}
return $hash
}

function Get-CompletionKeys {
# hashtable or array
Param($word, $ast, $hash_list)

if (!$hash_list) {
return @()
}

if ($hash_list.getType -And ($hash_list.getType() -eq [string])) {
$hash_list = Convert-JsonToHash $hash_list
}

$arr = $ast.ToString().Split().ToLower() | Where-Object { $_ -ne $null }

# Empty, need to return children completion keys
Expand All @@ -38,7 +57,7 @@ function Get-CompletionKeys {
}

if (!$cache_all_completion.ContainsKey($key)) {
$map = $hash_list
$map = ConvertTo-Hash $hash_list
$prefix = ""
$key_level | ForEach-Object {
if ($prefix) {
Expand All @@ -49,12 +68,7 @@ function Get-CompletionKeys {
$prefix = $_
}
if (!$cache_all_completion.ContainsKey($prefix)) {
if ($map.Keys) {
$cache_all_completion[$prefix] = $map.Keys
}
else {
$cache_all_completion[$prefix] = $map
}
$cache_all_completion[$prefix] = $map.Keys
}
}
}
Expand All @@ -65,28 +79,19 @@ function Get-CompletionKeys {
}

function Remove-Completion {
Param(
[Parameter(Mandatory)]
[string]$command
)
Param([Parameter(Mandatory)][string]$command)

$cache_command_list.Remove($command)
if ($PSVersion -lt '7.0') {
$($cache_all_completion.Clone().Keys) |
$cache_all_completion.Clone().Keys |
Where-Object { $_.StartsWith("$command.") -or ($_ -eq $command) } |
ForEach-Object { $cache_all_completion.Remove($_) }
}
else {
$cache_all_completion.Keys |
Where-Object { $_.StartsWith("$command.") -or ($_ -eq $command) } |
ForEach-Object { $cache_all_completion.Remove($_) }
}
}

function Register-Completion {
Param($command, $hash_list, [switch]$force = $false)
Param($command, $hash_list, [switch]$Force = $false)

if ($cache_command_list.ContainsKey($command)) {
if ($force) {
if ($Force) {
Remove-Completion $command
}
else {
Expand Down

0 comments on commit 93a0270

Please sign in to comment.