Пример #1
0
def get_extended_class(start_line=None):
    # Figure out if we're in an inner class and return its extended type if so.
    if not start_line:
        start_line = util.get_cursor_line_num()
    start_indent = util.get_indent(start_line)
    if start_indent > 0:
        for decl in iter_decls(start_line, -1, FUNC_DECLS | CLASS_DECLS):
            indent = util.get_indent(decl.line)
            if indent == start_indent:
                continue
            decl_type = type(decl)
            if decl_type is FuncDecl:
                start_indent = indent
            elif decl_type is ClassDecl:
                if decl.extends:
                    return decl.extends
                else:
                    return None
            if indent == 0:
                break

    # Search for 'extends' at the top of the file.
    for lnum in range(1, util.get_line_count()):
        line = util.get_line(lnum)
        m = re.match("extends\s+(\w+)", line)
        if m:
            return m.group(1)
        # Only 'tool' can appear before 'extends', so stop searching if any other
        # text is encountered.
        elif not re.match("tool\s+$", line):
            return None
Пример #2
0
def complete_dot():
    line_num = util.get_cursor_line_num()
    line = util.get_line(line_num)
    col_num = util.get_cursor_col_num()
    token_chain = script.get_token_chain(line, line_num, col_num - 2)
    if token_chain:
        last_token = token_chain[-1]
        last_token_type = type(last_token)

        # Complete statically accessible items in classes.
        if last_token_type is script.ClassToken:
            # Class is defined by Godot.
            if last_token.line == -1:
                c = classes.get_class(last_token.name)
                # Manually add an entry for 'new()' for core types.
                if not c.is_built_in():
                    new_func = classes.GodotMethod("new", c.get_name(), [],
                                                   None)
                    append_completion(build_completion(new_func, c.get_name()))
                _add_class_items(c, _CONSTANTS)
            else:
                for decl in script.iter_static_decls(last_token.line,
                                                     script.ANY_DECLS):
                    append_completion(build_completion(decl))
            return

        # Treat 'self' like we're accessing script variables, but exclude globals.
        if last_token_type is script.VariableToken and last_token.name == "self":
            complete_script(include_globals=False)
            return

        # Complete enum values.
        if last_token_type is script.EnumToken:
            values = script.get_enum_values(last_token.line)
            if values:
                for value in values:
                    append_completion(build_completion(value))
            return

        c_name = None
        flags = None
        if len(token_chain
               ) == 1 and last_token_type is script.SuperAccessorToken:
            c_name = script.get_extended_class(line_num)
            flags = _METHODS
        elif last_token_type is script.MethodToken:
            c_name = last_token.returns
        elif last_token_type is script.VariableToken:
            c_name = last_token.type
        if c_name:
            _add_class_items(classes.get_class(c_name), flags)
Пример #3
0
def complete_script(include_globals):
    # Complete user decls.
    down_search_start = 1
    for decl in script.iter_decls(util.get_cursor_line_num(), direction=-1):
        decl_type = type(decl)
        if decl_type == script.ClassDecl:
            down_search_start = decl.line
        elif decl_type != script.FuncDecl:
            append_completion(build_completion(decl))
    for decl in script.iter_decls(down_search_start, direction=1):
        append_completion(build_completion(decl))

    # Complete extended class.
    c = classes.get_class(script.get_extended_class())
    _add_class_items(c)

    # Complete global scope.
    if include_globals:
        _add_class_items(classes.get_global_scope())
Пример #4
0
def echodoc_search():
    util.clear_cache()

    text = vim.eval("a:text")
    text_len = len(text)
    if text_len == 0:
        return

    m = re.match("\w+", text)
    if not m:
        return
    method_name = m.group(0)

    chain_start = util.get_cursor_col_num() - text_len - 1
    line_num = util.get_cursor_line_num()
    line = util.get_line(line_num)[:chain_start]
    line = "{}{}()".format(line, method_name)

    method_args = None
    tokens = script.get_token_chain(line, line_num, len(line))
    if tokens and type(tokens[-1]) is script.MethodToken:
        method_args = tokens[-1].args
    else:
        return

    hl_identifier = vim.eval("g:echodoc#highlight_identifier")
    hl_arguments = vim.eval("g:echodoc#highlight_arguments")
    arg_hl_index = 0
    paren_count = 0
    for char in text[len(m.group(0)) + 1:]:
        if char == "(":
            paren_count += 1
        elif char == ")":
            paren_count -= 1
        elif char == "," and paren_count <= 0:
            arg_hl_index += 1

    echodoc = [{
        "text": method_name,
        "highlight": hl_identifier
    }, {
        "text": "("
    }]

    arg_count = len(method_args)
    for (i, arg) in enumerate(method_args):
        if arg.type:
            echodoc.append({
                "text": "{} ".format(arg.type),
                "highlight": "gdClass"
            })
        d = {"text": arg.name}
        if arg_hl_index == i:
            d["highlight"] = hl_arguments
        echodoc.append(d)
        if arg_count - 1 > i:
            echodoc.append({"text": ", "})
    if tokens[-1].qualifiers and "vararg" in tokens[-1].qualifiers:
        if arg_count > 0:
            echodoc.append({"text": ", "})
        d = {"text": "..."}
        if arg_hl_index >= arg_count:
            d["highlight"] = hl_arguments
        echodoc.append(d)
    echodoc.append({"text": ")"})

    vim.command("let echodoc_search_result = {}".format(str(echodoc)))