Repository: facebookresearch/hydra

Issue:
Fish completion is not working
Fish completion does not seem to work:

```
$ python my_app.py -sc install=fish | source
$ python my_app.py <TAB> string split: Unknown option “-n”
- (line 2):                        
string split -n ' ' $COMP_LINE
^                          
in command substitution             
        called on line 0 of file -
                                
in function “hydra_fish_completion”
        called on standard input
                      
in command substitution
        called on standard input
                                                                     

       stringstring - manipulate strings
        -                          

   Synopsis                        
       string escape [(-n | --no-quoted)] [--style=xxx] [STRING...]
       string join [(-q | --quiet)] SEP [STRING...]
       string length [(-q | --quiet)] [STRING...]
       string lower [(-q | --quiet)] [STRING...]
       string match [(-a | --all)] [((-e | --entire)] [(-i | --ignore-case)] [(-r | --regex)]
                    [(-n | --index)] [(-q |
```
Fish completion is not working
Fish completion does not seem to work:

```
$ python my_app.py -sc install=fish | source
$ python my_app.py <TAB> string split: Unknown option “-n”
- (line 2):                        
string split -n ' ' $COMP_LINE
^                          
in command substitution             
        called on line 0 of file -
                                
in function “hydra_fish_completion”
        called on standard input
                      
in command substitution
        called on standard input
                                                                     

       stringstring - manipulate strings
        -                          

   Synopsis                        
       string escape [(-n | --no-quoted)] [--style=xxx] [STRING...]
       string join [(-q | --quiet)] SEP [STRING...]
       string length [(-q | --quiet)] [STRING...]
       string lower [(-q | --quiet)] [STRING...]
       string match [(-a | --all)] [((-e | --entire)] [(-i | --ignore-case)] [(-r | --regex)]
                    [(-n | --index)] [(-q |
```


Discussion:
@binshengliu, any idea why this is happening?

I am using (for test) this fish from ubuntu 18.4:
fish, version 2.7.1
The option [`-n`](https://fishshell.com/docs/current/cmds/string-split.html) seems to be introduced in [`3.0.0`](https://github.com/fish-shell/fish-shell/releases/tag/3.0.0) (search `--no-empty`).

This is useful when users type multiple spaces like `python[ ][ ]foo.py`. Piping to grep `set -l parts (string split ' ' $COMP_LINE | grep -v '^$')` should have the same effect with the compatibility of older versions. I can submit a PR if you are OK with this.
Yes. that will work.
if strings is faster than forking grep maybe it's best to do it only for < 3.0.0 (you can base it on the FISH_VERSION environment variable).

What are you splitting here?
is this something that the fish plugin in Python is outputting?
can we just filter empty strings there instead?
It splits the current command line `python foo.py db.name=` into an array which mimics https://github.com/facebookresearch/hydra/blob/04b792b965178474c51fd9204b9b321b6fa4b764/hydra/_internal/core_plugins/bash_completion.py#L25

I found an easier fix. `commandline` has an option `-o` to return the tokenized command line. So `set -l parts (commandline -cpo)` works for older versions as well.
@binshengliu, any idea why this is happening?

I am using (for test) this fish from ubuntu 18.4:
fish, version 2.7.1
The option [`-n`](https://fishshell.com/docs/current/cmds/string-split.html) seems to be introduced in [`3.0.0`](https://github.com/fish-shell/fish-shell/releases/tag/3.0.0) (search `--no-empty`).

This is useful when users type multiple spaces like `python[ ][ ]foo.py`. Piping to grep `set -l parts (string split ' ' $COMP_LINE | grep -v '^$')` should have the same effect with the compatibility of older versions. I can submit a PR if you are OK with this.
Yes. that will work.
if strings is faster than forking grep maybe it's best to do it only for < 3.0.0 (you can base it on the FISH_VERSION environment variable).

What are you splitting here?
is this something that the fish plugin in Python is outputting?
can we just filter empty strings there instead?
It splits the current command line `python foo.py db.name=` into an array which mimics https://github.com/facebookresearch/hydra/blob/04b792b965178474c51fd9204b9b321b6fa4b764/hydra/_internal/core_plugins/bash_completion.py#L25

I found an easier fix. `commandline` has an option `-o` to return the tokenized command line. So `set -l parts (commandline -cpo)` works for older versions as well.

Resolving patch:
diff --git a/hydra/_internal/core_plugins/fish_completion.py b/hydra/_internal/core_plugins/fish_completion.py
--- a/hydra/_internal/core_plugins/fish_completion.py
+++ b/hydra/_internal/core_plugins/fish_completion.py
@@ -16,7 +16,7 @@ def install(self) -> None:
     set -lx COMP_LINE (commandline -cp)
 
     # Find out how to call the underlying script
-    set -l parts (string split -n ' ' $COMP_LINE)
+    set -l parts (commandline -cpo)
     if test "$parts[1]" = "python" -o "$parts[1]" = "python3"
         set cmd "$parts[1] $parts[2]"
         if not grep -q "@hydra.main" $parts[2]


Test patch: