Skip to content

Commit

Permalink
Merge pull request #3459 from yan12125/fix-issue3364
Browse files Browse the repository at this point in the history
(#3364) Fix broken tab completion (expansion) in PowerShell v7.4+
  • Loading branch information
corbob authored Nov 4, 2024
2 parents f65afd1 + 53dc7e0 commit 4947075
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
55 changes: 40 additions & 15 deletions src/chocolatey.resources/helpers/ChocolateyTabExpansion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -264,27 +264,52 @@ if ($PowerTab_RegisterTabExpansion) {
return
}

if (Test-Path Function:\TabExpansion) {
Rename-Item Function:\TabExpansion TabExpansionBackup
}

function TabExpansion($line, $lastWord) {
$lastBlock = [System.Text.RegularExpressions.Regex]::Split($line, '[|;]')[-1].TrimStart()
# PowerShell up to v5.x: use a custom TabExpansion function.
if ($PSVersionTable.PSVersion.Major -lt 5) {
if (Test-Path Function:\TabExpansion) {
Rename-Item Function:\TabExpansion TabExpansionBackup
}

switch -regex ($lastBlock) {
# Execute Chocolatey tab completion for all choco-related commands
"^$(Get-AliasPattern choco) (.*)" {
ChocolateyTabExpansion $lastBlock
}
function TabExpansion($line, $lastWord) {
$lastBlock = [System.Text.RegularExpressions.Regex]::Split($line, '[|;]')[-1].TrimStart()


# Fall back on existing tab expansion
default {
if (Test-Path Function:\TabExpansionBackup) {
TabExpansionBackup $line $lastWord
switch -regex ($lastBlock) {
# Execute Chocolatey tab completion for all choco-related commands
"^$(Get-AliasPattern choco) (.*)" {
ChocolateyTabExpansion $lastBlock
}

# Fall back on existing tab expansion
default {
if (Test-Path Function:\TabExpansionBackup) {
TabExpansionBackup $line $lastWord
}
}
}
}
}
else { # PowerShell v5+: use the Register-ArgumentCompleter cmdlet (PowerShell no longer calls TabExpansion after 7.4, but this available from 5.x)
function script:Get-AliasNames($exe) {
@($exe) + @(Get-Alias | Where-Object { $_.Definition -eq $exe } | Select-Object -Exp Name)
}

Register-ArgumentCompleter -Native -CommandName (Get-AliasNames choco) -ScriptBlock {
param($wordToComplete, $commandAst, $cursorColumn)

# NOTE:
# * The stringified form of $commandAst is the command's own command line (irrespective of
# whether other statements are on the same line or whether it is part of a pipeline).
# * However, trailing whitespace is trimmed in the string representation of $commandAst.
# Therefore, when the actual command line ends in space(s), they must be added back
# so that ChocolateyTabExpansion recognizes the start of a new argument.
$ownCommandLine = [string] $commandAst
$ownCommandLine = $ownCommandLine.Substring(0, [Math]::Min($ownCommandLine.Length, $cursorColumn))
$ownCommandLine += ' ' * ($cursorColumn - $ownCommandLine.Length)

ChocolateyTabExpansion $ownCommandLine
}
}

# SIG # Begin signature block
# MIInKwYJKoZIhvcNAQcCoIInHDCCJxgCAQExDzANBglghkgBZQMEAgEFADB5Bgor
Expand Down
5 changes: 4 additions & 1 deletion src/chocolatey.resources/helpers/chocolateyProfile.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ Import-Module "$thisDirectory\Chocolatey.PowerShell.dll" -Cmdlet "Get-Environmen

Set-Alias refreshenv Update-SessionEnvironment

Export-ModuleMember -Alias refreshenv -Cmdlet 'Update-SessionEnvironment' -Function 'TabExpansion'
Export-ModuleMember -Alias refreshenv -Cmdlet 'Update-SessionEnvironment'
if ($PSVersionTable.PSVersion.Major -lt 5) {
Export-ModuleMember -Function 'TabExpansion' # Legacy TabExpansion function is only used up to PowerShell v5.x
}

# SIG # Begin signature block
# MIInKwYJKoZIhvcNAQcCoIInHDCCJxgCAQExDzANBglghkgBZQMEAgEFADB5Bgor
Expand Down

0 comments on commit 4947075

Please sign in to comment.