<commit_before>import glob
import os
import shlex

from ..command import command
from ..const import DEFAULT_COMMANDS_MODULE, DEFAULT_CONFIG_FILE
from ..exc import RunnerError
from ..run import run, partition_argv, read_run_args
from ..runner import CommandRunner


@command
def complete(config, command_line, current_token, position, shell: dict(choices=('bash', 'fish'))):
    """Find completions for current command.

    This assumes that we'll handle all completion logic here and that
    the shell's automatic file name completion is disabled.

    Args:
        config: Config
        command_line: Command line
        current_token: Token at cursor
        position: Current cursor position
        shell: Name of shell

    """
    debug = config.run.debug
    position = int(position)
    tokens = shlex.split(command_line[:position])

    all_argv, run_argv, command_argv = partition_argv(tokens[1:])
    run_args = read_run_args(run)
    debug = run_args.get('debug', debug)

    module = run_args.get('commands-module')
    module = extract_from_argv(run_argv, run.args['commands-module'].options) or module
    module = module or DEFAULT_COMMANDS_MODULE
    module = normalize_path(module)

    config_file = run_args.get('config-file')
    config_file = extract_from_argv(run_argv, run.args['config-file'].options) or config_file
    if not config_file and os.path.exists(DEFAULT_CONFIG_FILE):
        config_file = DEFAULT_CONFIG_FILE
    config_file = normalize_path(config_file)

    try:
        runner = CommandRunner(module, config_file=config_file, debug=debug)
    except RunnerError:
        commands = {}
    else:
        commands = runner.commands

    found_command = find_command(commands, tokens)

    if current_token:
        # Completing either a command name, option name, or path.
        if current_token.startswith('-'):
            if current_token not in found_command.option_map:
                print_command_options(found_command, current_token)
        else:
            path = os.path.expanduser(current_token)
            path = os.path.expandvars(path)
            paths = glob.glob('%s*' % path)
            if paths:
                for entry in paths:
                    if os.path.isdir(entry):
                        print('%s/' % entry)
                    else:
                        print(entry)
            else:
                print_commands(commands, shell)
    else:
        # Completing option value. If a value isn't expected, show the
        # options for the current command and the list of commands
        # instead.
        arg = found_command.option_map.get(tokens[-1])

        if arg and arg.takes_value:
            if arg.choices:
                for choice in arg.choices:
                    print(choice)
            elif found_command is run and arg.name == 'env' and os.path.exists(config_file):
                print('\n'.join(config._get_envs(config_file)))
            else:
                for entry in os.listdir():
                    if os.path.isdir(entry):
                        print('%s/' % entry)
                    else:
                        print(entry)
        else:
            print_command_options(found_command)
            print_commands(commands, shell)


def extract_from_argv(argv, options):
    for option in options:
        try:
            i = argv.index(option)
        except ValueError:
            pass<commit_msg>Always show commands when completing non-option args<commit_after>import glob
import os
import shlex

from ..command import command
from ..const import DEFAULT_COMMANDS_MODULE, DEFAULT_CONFIG_FILE
from ..exc import RunnerError
from ..run import run, partition_argv, read_run_args
from ..runner import CommandRunner


@command
def complete(config, command_line, current_token, position, shell: dict(choices=('bash', 'fish'))):
    """Find completions for current command.

    This assumes that we'll handle all completion logic here and that
    the shell's automatic file name completion is disabled.

    Args:
        config: Config
        command_line: Command line
        current_token: Token at cursor
        position: Current cursor position
        shell: Name of shell

    """
    debug = config.run.debug
    position = int(position)
    tokens = shlex.split(command_line[:position])

    all_argv, run_argv, command_argv = partition_argv(tokens[1:])
    run_args = read_run_args(run)
    debug = run_args.get('debug', debug)

    module = run_args.get('commands-module')
    module = extract_from_argv(run_argv, run.args['commands-module'].options) or module
    module = module or DEFAULT_COMMANDS_MODULE
    module = normalize_path(module)

    config_file = run_args.get('config-file')
    config_file = extract_from_argv(run_argv, run.args['config-file'].options) or config_file
    if not config_file and os.path.exists(DEFAULT_CONFIG_FILE):
        config_file = DEFAULT_CONFIG_FILE
    config_file = normalize_path(config_file)

    try:
        runner = CommandRunner(module, config_file=config_file, debug=debug)
    except RunnerError:
        commands = {}
    else:
        commands = runner.commands

    found_command = find_command(commands, tokens)

    if current_token:
        # Completing either a command name, option name, or path.
        if current_token.startswith('-'):
            if current_token not in found_command.option_map:
                print_command_options(found_command, current_token)
        else:
            print_commands(commands, shell)
            path = os.path.expanduser(current_token)
            path = os.path.expandvars(path)
            paths = glob.glob('%s*' % path)
            if paths:
                for entry in paths:
                    if os.path.isdir(entry):
                        print('%s/' % entry)
                    else:
                        print(entry)
    else:
        # Completing option value. If a value isn't expected, show the
        # options for the current command and the list of commands
        # instead.
        arg = found_command.option_map.get(tokens[-1])

        if arg and arg.takes_value:
            if arg.choices:
                for choice in arg.choices:
                    print(choice)
            elif found_command is run and arg.name == 'env' and os.path.exists(config_file):
                print('\n'.join(config._get_envs(config_file)))
            else:
                for entry in os.listdir():
                    if os.path.isdir(entry):
                        print('%s/' % entry)
                    else:
                        print(entry)
        else:
            print_command_options(found_command)
            print_commands(commands, shell)


def extract_from_argv(argv, options):
    for option in options:
        try:
            i = argv.index(option)
        except ValueError:
            pass
{'fraction_non_alphanumeric': 0.08424110384894698, 'fraction_numerical': 0.0005809731299927379, 'mean_word_length': 2.2222742161909217, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4421208', 'n_tokens_mistral': 1893, 'n_tokens_neox': 1834, 'n_words': 512}