Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with Powershell tab completion script #931

Closed
elirnm opened this issue Apr 11, 2017 · 5 comments
Closed

Error with Powershell tab completion script #931

elirnm opened this issue Apr 11, 2017 · 5 comments
Labels
A-completion Area: completion generator C-bug Category: Updating dependencies E-medium Call for participation: Experience needed to fix: Medium / intermediate

Comments

@elirnm
Copy link

elirnm commented Apr 11, 2017

Reposted from BurntSushi/ripgrep#445


Attempting to import the _rg.ps1 tab completion script into Powershell throws an exception:

At C:\PathTools\_rg.ps1:10 char:41
+                 switch ($_.ToString()) {
+                                         ~
Missing condition in switch statement clause.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingSwitchConditionExpression

There's an empty switch statement there that seems like the cause.

$PSVersionTable:

Name                           Value
----                           -----
PSVersion                      5.1.14409.1005
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14409.1005
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

This is with ripgrep version 0.5.1

Here's the ps1 file.

@('rg', './rg', 'rg.exe', '.\rg', '.\rg.exe', './rg.exe') | %{
    Register-ArgumentCompleter -Native -CommandName $_ -ScriptBlock {
        param($wordToComplete, $commandAst, $cursorPosition)

        $command = '_rg'
        $commandAst.CommandElements |
            Select-Object -Skip 1 |
            %{
                switch ($_.ToString()) {

                }
            }

        $completions = @()

        switch ($command) {

            '_ripgrep' {
                $completions = @('-a', '-c', '-F', '-i', '-n', '-N', '-q', '-u', '-v', '-w', '-l', '-H', '-L', '-0', '-o', '-p', '-s', '-S', '-h', '-V', '-e', '-E', '-g', '-t', '-T', '-A', '-B', '-C', '-f', '-m', '-r', '-j', '-M', '--files', '--type-list', '--text', '--count', '--fixed-strings', '--ignore-case', '--line-number', '--no-line-number', '--quiet', '--unrestricted', '--invert-match', '--word-regexp', '--column', '--debug', '--files-with-matches', '--files-without-match', '--with-filename', '--no-filename', '--heading', '--no-heading', '--hidden', '--follow', '--mmap', '--no-messages', '--no-mmap', '--no-ignore', '--no-ignore-parent', '--no-ignore-vcs', '--null', '--only-matching', '--pretty', '--case-sensitive', '--smart-case', '--sort-files', '--vimgrep', '--help', '--version', '--regexp', '--color', '--colors', '--encoding', '--glob', '--type', '--type-not', '--after-context', '--before-context', '--context', '--context-separator', '--file', '--ignore-file', '--max-count', '--max-filesize', '--maxdepth', '--path-separator', '--replace', '--threads', '--max-columns', '--type-add', '--type-clear')
            }

        }

        $completions |
            ?{ $_ -like "$wordToComplete*" } |
            Sort-Object |
            %{ New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', $_ }
    }
}
@kbknapp
Copy link
Member

kbknapp commented Apr 18, 2017

Thanks for reporting! This is because ripgrep has no subcommands. I think simply adding a default will fix this. If someone wants an easy first PR this is one!

So adding a line here:

 default { }

or default { break } if PowerShell doesn't support empty braces (can't remember).

@kbknapp kbknapp added A-completion Area: completion generator D: easy E-medium Call for participation: Experience needed to fix: Medium / intermediate C-bug Category: Updating dependencies labels Apr 18, 2017
@elirnm
Copy link
Author

elirnm commented Apr 18, 2017

Hmm, adding default { } into the script makes it load without throwing exceptions, but I still don't get any tab completion functionality.

@kbknapp
Copy link
Member

kbknapp commented Apr 18, 2017

Ok, I did some testing and it looks like it's a double issue.

  • Adding default { break } works but the second bug prevents the completions...
  • it's using the App name (ripgrep) and not the binary name (rg) which is a bug here (should be using bin_name not name)

@kbknapp
Copy link
Member

kbknapp commented Apr 18, 2017

Here's the excerpt of the generated script using the name, when it should be using the bin name:

switch ($command) {
    '_ripgrep' {
        $completions = @('-a', '-c', '-F', '-i', '-n', '-N', '-q', '-u', '-v', '-w', '-l', '-H', '-L', '-0', '-o', '-p', '-s', '-S', '-h', '-V', '-e', '-E', '-g', '-t', '-T', '-A', '-B', '-C', '-f', '-m', '-r', '-j', '-M', '--files', '--type-list', '--text', '--count', '--fixed-strings', '--ignore-case', '--line-number', '--no-line-number', '--quiet', '--unrestricted', '--invert-match', '--word-regexp', '--column', '--debug', '--files-with-matches', '--files-without-match', '--with-filename', '--no-filename', '--heading', '--no-heading', '--hidden', '--follow', '--mmap', '--no-messages', '--no-mmap', '--no-ignore', '--no-ignore-parent', '--no-ignore-vcs', '--null', '--only-matching', '--pretty', '--case-sensitive', '--smart-case', '--sort-files', '--vimgrep', '--help', '--version', '--regexp', '--color', '--colors', '--encoding', '--glob', '--type', '--type-not', '--after-context', '--before-context', '--context', '--context-separator', '--dfa-size-limit', '--file', '--ignore-file', '--max-count', '--max-filesize', '--maxdepth', '--path-separator', '--replace', '--regex-size-limit', '--threads', '--max-columns', '--type-add', '--type-clear')
    }
}

@homu homu closed this as completed in a8bce55 Apr 19, 2017
@kbknapp
Copy link
Member

kbknapp commented Apr 19, 2017

Fixed in 2.23.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-completion Area: completion generator C-bug Category: Updating dependencies E-medium Call for participation: Experience needed to fix: Medium / intermediate
Projects
None yet
Development

No branches or pull requests

2 participants