def run_complete()

in nubia_complete/completer.py [0:0]


def run_complete(args):
    model_file = args.command_model_path
    logging.info("Command model: %s", model_file)
    comp_line = os.getenv("COMP_LINE")
    comp_point = int(os.getenv("COMP_POINT", "0"))
    comp_type = os.getenv("COMP_TYPE")
    comp_shell = os.getenv("COMP_SHELL", "bash")
    if not comp_line:
        logger.error("$COMP_LINE is unset, failing!")
        return 1
    if not comp_point:
        logger.error("$COMP_POINT is unset, failing!")
        return 1
    # Fix the disparity between zsh and bash for COMP_POINT
    if comp_shell == "zsh":
        comp_point -= 1
    # We want to trim the remaining of the line because we don't care about it
    comp_line = comp_line[:comp_point]
    # We want to tokenize the input using these rules:
    # - Separate by space unless there it's we are in " or '
    try:
        tokens = shlex.split(comp_line)
        if len(tokens) < 1:
            return 1
        # drop the first word (the executable name)
        tokens = tokens[1:]
    except ValueError:
        logger.warning("We are in an open quotations, cannot suggestion completions")
        return 0
    logger.debug("COMP_LINE: @%s@", comp_line)
    logger.debug("COMP_POINT: %s", comp_point)
    logger.debug("COMP_TYPE: %s", comp_type)
    logger.debug("COMP_SHELL: %s", comp_shell)
    # we want to know if the cursor is on a space or a word. If it's on a space,
    # then we expect a completion of (command, option, or value).
    current_token = None
    if comp_line[comp_point - 1] not in string.whitespace:
        current_token = tokens[-1]
        tokens = tokens[:-1]
    logger.debug("Input Tokens: %s", tokens)
    logger.debug("Current token: %s", current_token)
    # loading the model
    with open(model_file, "r") as f:
        model = json.load(f)

    completions = get_completions(model, tokens, current_token, comp_shell)
    for completion in completions:
        logger.debug("Completion: @%s@", completion)
        print(completion)