def get_completions()

in nubia_complete/completer.py [0:0]


def get_completions(model, tokens, current, shell):
    output = []
    options_we_expect = model["options"]
    current_command_list = model.get("commands", [])
    last_option_found = None
    for token in tokens:
        if token.startswith("-"):
            # it's an option, drop it from expected
            current_option = _drop_from_options(options_we_expect, token)
            if current_option and current_option.get("expects_argument"):
                last_option_found = current_option
        else:
            # this is:
            # - Argument to an option (ignore)
            # - Command
            # - Some random free argument
            if last_option_found:
                # does it expect a value?
                logger.debug(
                    "Skipping %s because it's an argument to %s",
                    token,
                    last_option_found.get("name"),
                )
                last_option_found = None
                continue
            last_option_found = None
            for command in current_command_list:
                if token == command.get("name"):
                    logger.debug("We matched command %s", command.get("name"))
                    options_we_expect.extend(command.get("options", []))
                    # for sub-commands
                    current_command_list = command.get("commands", [])
                    break
            else:
                logger.debug(
                    "We didn't find any matching command, ignoring the token %s",
                    token,
                )
                # Now that we know where we are, let's complete the current token:
    if last_option_found:
        # we are expecting a value for this
        output = _get_values_for_option(last_option_found)
    else:
        # If the current token is '--something=' then we should try to
        # autocomplete a value for this
        if current:
            match = option_regex.match(current)
            if match:
                key = match.groupdict()["key"]
                logger.debug("We are in a value-completion inside %s", key)
                # it's true
                option = _drop_from_options(options_we_expect, current, skip_value=True)
                if option:
                    # YES, we have it, let's get the values
                    prefix = ""
                    if shell == "zsh":
                        # in zsh, we need to prepend the completions with the
                        # key
                        prefix = key
                    return _get_values_for_option(option, prefix)

        output.extend(_completions_for_options(options_we_expect))
        output.extend(_completions_for_commands(current_command_list))
    return output