Closed mondy closed 9 months ago
I have the same problem and tab completion stopped working for the Choco package manager too, I still have no idea why.
I have the same issue, other completion module like posh-git still work.
All other like npm-completion, posh-git, or gh works fine but choco and scoop stopped after upgrading to PS 7.4, I think it may relate.
I have the same issue too.
For background information, see https://github.com/PowerShell/PowerShell/issues/20930#issuecomment-1897472499
Here's a solution that works in v7.4+:
For those looking to apply an immediate fix, they can apply it as follows:
Open the relevant file for editing:
Invoke-Item ([IO.Path]::ChangeExtension((Get-Module -List scoop-completion).Path, '.psm1'))
Paste the code below at the end, save, and then start a new session.
To fix the problem at the source:
Add the code below to https://github.com/Moeologist/scoop-completion/blob/master/src/scoop-completion.psm1
If PowerShell versions prior to Windows PowerShell v5.1 do not need to be supported anymore:
Export-ModuleMember -Function @()
FunctionsToExport = @('TabExpansion')
with FunctionsToExport = @()
in https://github.com/Moeologist/scoop-completion/blob/master/src/scoop-completion.psd1If pre-v5.1 support is still needed:
if ($PSVersionTable.PSVersion -ge '7.4') { ... }
function script:Get-AliasNames($exe) {
@($exe, "$exe.ps1", "$exe.cmd") + @(Get-Alias | Where-Object { $_.Definition -eq $exe } | Select-Object -Exp Name)
}
Register-ArgumentCompleter -Native -CommandName (Get-AliasNames scoop) -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).
# * The command name itself - assumed to contain no spaces - is removed, so that only the
# list of arguments is passed to ScoopTabExpansion.
# * 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 ScoopTabExpansion recognizes the start of a new argument.
# .TrimStart() ensures that if there are no arguments yet at all, the empty string is passed,
# which is what ScoopTabExpansion expects.
$ownCommandLine = [string] $commandAst
$ownCommandLine = $ownCommandLine.Substring(0, [Math]::Min($ownCommandLine.Length, $cursorColumn))
$argList = (($ownCommandLine -replace '^\S+\s*') + ' ' * ($cursorColumn - $ownCommandLine.Length)).TrimStart()
ScoopTabExpansion $argList
}
I've fixed a problem in the code in the previous comment, which now makes it support intra-line expansions properly too - from what I can tell, this makes it suitable for a fix.
However, if you want to avoid having to modify the module, I've posted a polyfill that overrides the TabExpansion2
function only, and, if added to $PROFILE
, should work with all modules that still rely on the legacy TabExpansion
function - see https://github.com/PowerShell/PowerShell/issues/20930#issuecomment-1899191414
有关背景信息,请参阅PowerShell/PowerShell#20930(评论)
这是一个适用于 v7.4+ 的解决方案:
对于那些希望_立即_修复的人,他们可以按如下方式应用:
- 打开相关文件进行编辑:
Invoke-Item ([IO.Path]::ChangeExtension((Get-Module -List scoop-completion).Path, '.psm1'))
- 将以下代码粘贴到末尾,保存,然后启动新会话。
要从源头上解决问题:
将以下代码添加到https://github.com/Moeographer/scoop-completion/blob/master/src/scoop-completion.psm1
_如果不再_需要支持Windows PowerShell v5.1 之前的 PowerShell 版本:
删除以下当时已过时的代码:
- https://github.com/Moeological/scoop-completion/blob/dffb5f558431d586522fc636b9e9a3ea260e861f/src/scoop-completion.psm1#L359C1-L393
- 将其替换为
Export-ModuleMember -Function @()
- 替换
FunctionsToExport = @('TabExpansion')
为https://github.com/Moeographer/scoop-completion/blob/master/src/scoop-completion.psd1FunctionsToExport = @()
__如果仍需要_v5.1 之前的支持:
将以下代码括在
if ($PSVersionTable.PSVersion -ge '7.4') { ... }
function script:Get-AliasNames($exe) { @($exe, "$exe.ps1", "$exe.cmd") + @(Get-Alias | Where-Object { $_.Definition -eq $exe } | Select-Object -Exp Name) } Register-ArgumentCompleter -Native -CommandName (Get-AliasNames scoop) -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). # * The command name itself - assumed to contain no spaces - is removed, so that only the # list of arguments is passed to ScoopTabExpansion. # * 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 ScoopTabExpansion recognizes the start of a new argument. # .TrimStart() ensures that if there are no arguments yet at all, the empty string is passed, # which is what ScoopTabExpansion expects. $ownCommandLine = [string] $commandAst $ownCommandLine = $ownCommandLine.Substring(0, [Math]::Min($ownCommandLine.Length, $cursorColumn)) $argList = (($ownCommandLine -replace '^\S+\s*') + ' ' * ($cursorColumn - $ownCommandLine.Length)).TrimStart() ScoopTabExpansion $argList }
@mklement0 , This solution seems to be a good Register-ArgumentCompleter impl. If you don't mind, I will apply this fix to repo
@Moeologist: Sounds great; please do.
Fixed on v0.3.0
Thx for fixing this
scoop-completion does not work in PowerShell version 7.4.0 scoop-completion works in PowerShell version 7.3.0. Can I use scoop-completion with PowerShell version 7.4.0?