kelleyma49 / PSFzf

A PowerShell wrapper around the fuzzy finder fzf
MIT License
787 stars 34 forks source link

tab completion errors #96

Closed cstrahan closed 2 years ago

cstrahan commented 2 years ago

When hitting tab after Get-Module -:

PS C:\Users\cstrahan> Get-Module -
> -                                                                                 ╭─────────────────────────────────────────────────────────────────────────────────╮
  22/22                                                                             │ PsFzfTabExpansion-Preview.ps1: A parameter cannot be found that matches paramet │
> -All                                                                              │                                                                                 │
  -Name                                                                             │                                                                                 │
  -Debug                                                                            │                                                                                 │
  -Refresh                                                                          │                                                                                 │
  -Verbose                                                                          │                                                                                 │
  -PSEdition                                                                        │                                                                                 │
  -PSSession                                                                        │                                                                                 │
  -OutBuffer                                                                        │                                                                                 │
  -CimSession                                                                       ╰─────────────────────────────────────────────────────────────────────────────────╯
PS C:\Users\cstrahan> Get-Module -
> -                                                                                 ╭─────────────────────────────────────────────────────────────────────────────────╮
  22/22                                                                             │ InvalidOperation: C:\Users\cstrahan\Documents\PowerShell\Modules\PSFzf\2.3.1/40 │
  -All                                                                              │ Line |                                                                          │
  -Name                                                                             │    6 |  $Item = $Item.Trim("'").Trim('"')                                       │
> -Debug                                                                            │      |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                       │
  -Refresh                                                                          │      | You cannot call a method on a null-valued expression.                    │
  -Verbose                                                                          │                                                                                 │
  -PSEdition                                                                        │                                                                                 │
  -PSSession                                                                        │     Directory: C:\Users\cstrahan                                                │
  -OutBuffer                                                                        │ Mode                 LastWriteTime         Length Name                          │
  -CimSession                                                                       ╰─────────────────────────────────────────────────────────────────────────────────╯

Seems to fail here: https://github.com/kelleyma49/PSFzf/blob/0843f35b0e77101f0718e89bb075e20e4e211d23/helpers/PsFzfTabExpansion-Preview.ps1#L6

This is on the latest alpha of PSFzF:

PS C:\Users\cstrahan> Get-Module -Name PSFzf

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     2.3.1      alpha      PSFzf                               {Enable-PsFzfAliases, Invoke-FuzzyEdit, Invoke-FuzzyFasd, Invoke-FuzzyGitStatus…}

(also occurs with latest stable release)

FWIW, this is with the latest version of FZF installed through chocolatey:

C:\Windows\system32>fzf --version
0.28.0 (e4c3ecc)

Anything I can do to help resolve this? This module otherwise seems awesome!

cstrahan commented 2 years ago

I wonder if that's because this line:

https://github.com/kelleyma49/PSFzf/blob/0843f35b0e77101f0718e89bb075e20e4e211d23/PSFzf.TabExpansion.ps1#L261

Should be more like:

        $additionalCmd = @{ Preview=$("pwsh -NoProfile -NonInteractive -File \""$previewScript\"" -- \""" + $path + "\"" {}") } 

(note the double hyphen; see https://stackoverflow.com/a/12198146)

belotn commented 2 years ago

As the cmdlet is passed directly to PsFzfTabExpansion-Preview.ps1, powershell try yo bind it as a parameters and then generate the error as the parameter is not declare (named parameter)

Get it working :

image

We can detect that completion is a ParameterName


        if( $completionMatches[0].ResultType -eq 'ParameterName'){
            $Command = $Line.Substring(0, $Line.indexof(' '))
            $previewScript = $(Join-Path $PsScriptRoot 'helpers/PsFzfTabExpansion-Parameter.ps1')
            $additionalCmd = @{ Preview=$("$PowerShellCMD -NoProfile -NonInteractive -File \""$previewScript\"" $Command {}") } 

        } else{ 
            $previewScript = $(Join-Path $PsScriptRoot 'helpers/PsFzfTabExpansion-Preview.ps1')
            $additionalCmd = @{ Preview=$("$PowerShellCMD -NoProfile -NonInteractive -File \""$previewScript\"" \""" + $path + "\"" {}") } 
        }

and invoke a script without named parameters

################################################################################
# Could not use named parameters                                               #
################################################################################

$command = $args[0]
$parameter = $args[1]
$parameter = $parameter.replace('-','')
Get-Help -Name $Command -Parameter $parameter
kelleyma49 commented 2 years ago

Thanks for the suggestion @belotn ! I've used in my PR #121