casey / just

🤖 Just a command runner
https://just.systems
Creative Commons Zero v1.0 Universal
21.17k stars 470 forks source link

Add recipes to generated completion scripts #573

Open casey opened 4 years ago

casey commented 4 years ago

In #572, I'll be landing a --completions flag to make just output completion scripts for various shells. However, these completion scripts should also allow completing recipe names (like build or test). I'll need to add this functionality separately.

See #223 for completion scripts that others have written.

Add recipe completions to:

gotcha commented 4 years ago

@casey If the just.bash file (see https://github.com/casey/just/pull/572/files#diff-1dcc75cc227d1a56d11d839f20f6a50c) is generated with clap, how do you plan to maintain it if I fiddle to merge the recipe completion I have in https://github.com/gotcha/just-bash-completion ?

casey commented 4 years ago

@gotcha Good question!

I was thinking that I would first write the completion script to a string while generating it in config.rs, and then modifying it before printing it. (Either by appending more stuff to the end, or doing a search and replace if it's more involved than that.)

If you can show me what should be added to the completion script (either in a gist, or a PR), then I can make the changes in rust to add it when it's generated too.

gergelyk commented 3 years ago
In case of bash, I do this dirty trick to replace `...` with (fine tuned) output of `just --list`. ```sh source <(OPTS=$(just --list | tail -n +2 | sed 's/ / /g' | tr -d '\n'); just --completions bash | sed "s/ ... /$OPTS/g") ``` Do you think it would make sense to include similar solution in default script generated by `just --completions bash`? Probably some dedicated variant of `just --list` would be applicable here.
casey commented 3 years ago

That's an awesome trick! I'd be worried about adding it into the default script, just because it adds dependencies on tail, sed, and tr. But if you can modify the bash completion script to include recipe names without using those additional commands, I can modify the scripts when they're generated, here: https://github.com/Insomniak47/just/blob/master/src/subcommand.rs

gergelyk commented 3 years ago

I agree. tail, sed, tr is probably what we wand to avoid. As an alternative I would suggest extending functionality of --list. E.g. --list-inline would give the same output as --list but in a single line, without header line and space-separated. This way completion script could be modified so that instead of <ARGUMENTS>... we have $(just --list-inline).

casey commented 3 years ago

Ah, okay, I misunderstood. Actually, that's already available if you do just --summary, it'll print a space-separated list of recipe names on a single line.

gergelyk commented 3 years ago

Great! I confirm, the following does the trick:

source <(just --completions bash | sed 's/ <ARGUMENTS>... /$(just --summary)/g')

We will appreciate making it default in just --completions bash.

casey commented 3 years ago

Actually, can you check to see if you're using the latest version of Just? I think recipe completions may have already been added to the latest version of the bash completions script:

https://github.com/casey/just/blob/c647efa200c71bf8b73511c47e88a1cca0f87c0e/completions/just.bash#L28

gergelyk commented 3 years ago

You are correct. just of mine wasn't up to date. After upgrading to v0.8.4 I got it working. The only remark I can make is that it works only for the first task that I try to execute. For instance here only foo is completed and bar, baz aren't:

just foo bar baz
casey commented 3 years ago

After upgrading to v5.4 I got it working.

Ah, great. I don't use bash myself, so I forgot that it already had completions.

The only remark I can make is that it works only for the first task that I try to execute.

That's definitely an unfortunate limitation of the completion script. I opened #756 to track it.

8tm commented 3 years ago

This solution does not support aliases which are sometimes made up of several characters

just --summary

To support aliases and other section we can use this command:

just --list --list-heading "" | awk '{print $1}'

You can also add checking for justfile to expand completions only when file exists:

if [ -f justfile ]; then
    options=$(just --list --list-heading "" | awk '{print $1}')
fi

Is it possible to add autocomplete that would match only sections and aliases from justfile? Without auto-completion for just? In case when we would like use only auto-completion for our project we could simplify bash completions to:

_just()
{
    COMPREPLY=()
    options=()

    if [ -f justfile ]; then
        options=$(just --list --list-heading "" | awk '{print $1}')
    fi

    COMPREPLY=( $(compgen -W "${options}" -- ${COMP_WORDS[COMP_CWORD]}) )
}

complete -F _just j

j is simple wrapper function for just:

function j()
{
    if [ $# -eq 0 ]
    then
        just --list
    else
        just "$@"
    fi
}

In this case I would like to request new command something like --project-only or something like that.

And it resolving limitation of the completion script so You can press 2 xTAB, use option, use again 2 x TAB, use next option etc etc. So it helps with auto-completion for:

just foo bar baz

Like:

obraz

It also works dynamically for each of project, so You will have other auto-completions for each of justfile (without changing anything). Like:

obraz

casey commented 3 years ago

This solution does not support aliases which are sometimes made up of several characters.

I wasn't sure whether completion scripts should complete aliases, as well as full recipe names. My thought was completions scripts should only complete full recipe names, since aliases are often made to be shorter to type, but might clutter completion results. I'm curious how others feel about this though.

You can also add checking for justfile to expand completions only when file exists:

if [ -f justfile ]; then
    options=$(just --list --list-heading "" | awk '{print $1}')
fi

Unfortunately this only works if the justfile is in the current directory, but not if it's in a parent directory.

Is it possible to add autocomplete that would match only sections and aliases from justfile? Without auto-completion for just?

Do you mean auto-complete that only completes recipe names and aliases? Is this because it's simpler, or because it's undesirable to complete flags and options?

gergelyk commented 3 years ago

I wasn't sure whether completion scripts should complete aliases, as well as full recipe names.

I don't have sharp opinion, but probably yes, I would include aliases too.

8tm commented 3 years ago

Solution for Bash with parent directories (fixed):

#!/usr/bin/env bash

_just()
{
    COMPREPLY=()
    options=""
    nearest_existing_justfile_path=""
    path=""
    DEFAULT_IFS="${IFS}"
    IFS="/";

    for current_folder_name in $(pwd);
    do
        IFS="${DEFAULT_IFS}"
        path="${path}/${current_folder_name}"

        if [ -f "${path}/justfile" ]; then
            nearest_existing_justfile_path="${path}/justfile"
        fi;

        IFS="/"
    done;

    IFS="${DEFAULT_IFS}"

    if [ -f "${nearest_existing_justfile_path}" ]; then
        options=$(just --list --list-heading "" --justfile "${nearest_existing_justfile_path}" | awk '{print $1}')
    fi

    COMPREPLY=($(compgen -W "${options}" -- "${COMP_WORDS[COMP_CWORD]}"))
}

complete -F _just j