Moeologist / scoop-completion

scoop tab completion, work with powershell
MIT License
173 stars 12 forks source link

Completion does not work in PowerShell version 7.4.0 #35

Closed mondy closed 9 months ago

mondy commented 11 months ago

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?

silverqx commented 11 months ago

I have the same problem and tab completion stopped working for the Choco package manager too, I still have no idea why.

ghost commented 11 months ago

I have the same issue, other completion module like posh-git still work.

silverqx commented 11 months ago

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.

mulmer663 commented 11 months ago

I have the same issue too.

mklement0 commented 9 months ago

For background information, see https://github.com/PowerShell/PowerShell/issues/20930#issuecomment-1897472499

Here's a solution that works in v7.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 commented 9 months ago

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

Moeologist commented 9 months ago

有关背景信息,请参阅PowerShell/PowerShell#20930(评论)

这是一个适用于 v7.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

mklement0 commented 9 months ago

@Moeologist: Sounds great; please do.

Moeologist commented 9 months ago

Fixed on v0.3.0

silverqx commented 9 months ago

Thx for fixing this